Skip to main content
YSN
Associate III
August 28, 2021
Solved

Function/Library to convert between UTC time and Local Time

Is there any function or library associated with STM32 Device to convert the UTC time to Local Time and Vice versa ?

This topic has been closed for replies.
waclawek.jan
waclawek.janBest answer
Super User
August 28, 2021

Use the standard C-library functions prototyped in <time. h> to convert the date/time to UNIX epoch-based time, add or subtract the timezone-specific amount, then convert back.

You'll still have to supply the rules,it is application-specific how rigorously have they (mainly DST) be observed (e.g. if historical data have to be included, minor/exotic geographical exceptions, etc.)

JW

3 replies

TDK
August 28, 2021

The STM32 has no way of knowing what timezone it's in unless you provide that information. You add or subtract X hours from the time to convert.

As timezones are complex, arbitrary, and subject to change, this complexity is typically left out of the embedded system.

"If you feel a post has answered your question, please click ""Accept as Solution""."
YSN
YSNAuthor
Associate III
August 28, 2021

You are right, I was too expecting embedded C function / library to make conversions !

I understand that this is out of context for MCU, but was mentioning to get some examples if it is already done by somebody else.

waclawek.jan
waclawek.janBest answer
Super User
August 28, 2021

Use the standard C-library functions prototyped in <time. h> to convert the date/time to UNIX epoch-based time, add or subtract the timezone-specific amount, then convert back.

You'll still have to supply the rules,it is application-specific how rigorously have they (mainly DST) be observed (e.g. if historical data have to be included, minor/exotic geographical exceptions, etc.)

JW