This is the mail archive of the
newlib@sourceware.org
mailing list for the newlib project.
Re: How to set timezone for localtime()
- From: Brian Inglis <Brian dot Inglis at SystematicSw dot ab dot ca>
- To: newlib at sourceware dot org
- Date: Sat, 31 Mar 2018 00:45:46 -0600
- Subject: Re: How to set timezone for localtime()
- References: <CAOntYw0MqDAOd-ZDb6wVEE0uGGBkEr3HjoZwXxpFNO0cVzzo=A@mail.gmail.com>
- Reply-to: Brian dot Inglis at SystematicSw dot ab dot ca
On 2018-03-31 00:16, Giuseppe Modugno wrote:
> I have the number of seconds since epoch 1970, in UTC. I'd like to convert
> it into broken-down time (year, month, day, hours, ...). I live in Italy
> and I need the broken-down in *local* time, so taking into account timezone
> (+1 hour) *and* daylight savings.
>
> My platform is a Cortex-M3 MCU from NXP. I'm using MCUXpresso IDE to build
> my project and I'm using newlib-nano variant.
>
> I looked at the code and it seems there is some instructions in localtime()
> that take into account the timezone. However I don't know how to set
> Italian timezone on my platform (somewhere I read I can set TZ environment
> variable... but I don't have an environment at all).
>
> Any help?
Does your platform at least support putenv(3)?
If so, then on a hosted platform with the latest zoneinfo package, you can run:
$ tail -1 /usr/share/zoneinfo/Europe/Rome
CET-1CEST,M3.5.0,M10.5.0/3
which shows the encoded EU rules for that time zone: STD CET == UTC+1 with
opposite POSIX sign, DST CEST == UTC+2 from March last Sunday at 02.00, until
October last Sunday at 03.00.
In your program:
#include <stdlib.h>
putenv("TZ=CET-1CEST,M3.5.0,M10.5.0/3");
#include <time.h>
tzset();
time_t tt = time(&tt);
struct tm *stmp = localtime(&tt);
should give you the correct broken down time if tzset(3) and localtime(3)
support using TZ.
--
Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada