setenv and tzset not working

rtphilipson
Posts: 3
Joined: Mon Mar 09, 2026 12:09 pm

setenv and tzset not working

Postby rtphilipson » Mon Mar 09, 2026 12:35 pm

I'm using SNTP to set the system time and it's working fine. But then I try to set the local time;

time(&now);
setenv("TZ", "CST6CDT,M3.8.0/2,M11.1.0/2", 1);
tzset();
localtime_r(&now, &timeinfo);
strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
cli_print_msg("Local Time is: %s", strftime_buf);
gmt = gmtime(&now);
if(gmt) {
cli_print_msg("UTC time: %u/%u/%u %u:%u:%u", (gmt->tm_mon + 1),
gmt->tm_mday,
(gmt->tm_year - 100),
gmt->tm_hour,
gmt->tm_min,
gmt->tm_sec);
}

And it just returns GMT in the local time string. Is there some setup for setenv to work that I might be missing. When I look at the sample, it seems that everything I need is already setup by the time I have SNTP working.

vvb333007
Posts: 71
Joined: Wed Jul 31, 2024 5:53 am
Location: Thailand
Contact:

Re: setenv and tzset not working

Postby vvb333007 » Wed Mar 11, 2026 6:57 am

For me it works perfectly but I use a bit simplier TZ values. Try something like "UTC-7:00", to see if it works.

This code works, so your should also.

Code: Select all

 char buf[128];    // buffer for strftime. enough space for multibyte locales
  //char buftz[8];
  struct tm tm_adj; // adjusted (according to a timezone) time
  time_t now;

  now = time(NULL);
  if (!now)
    return CMD_FAILED;
  //gmtime_r(&now, &tm_adj);
  localtime_r(&now, &tm_adj);
    
  if (strftime(buf, sizeof(buf), "%e of %B (%A) <i>%H:%M</>:%S ,year %Y", &tm_adj) == 0)
    return CMD_FAILED
Thanks!
Slava.

rtphilipson
Posts: 3
Joined: Mon Mar 09, 2026 12:09 pm

Re: setenv and tzset not working

Postby rtphilipson » Wed Mar 11, 2026 3:49 pm

OK, it works. I rarely use local time in my systems and LibC internally uses the same variable for storing time structs, so my multiple calls to gmtime , localtime was overwriting. So, it even works for the more complicated POSIX formats.

Thanks for responding.

MicroController
Posts: 2669
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: setenv and tzset not working

Postby MicroController » Wed Mar 11, 2026 8:36 pm

Code: Select all

"...M3.8.0..."
doesn't look right, indicating the 8th week of March. You probably want "...M3.2.0..." instead.

May want to just take one TZ value from e.g. here.

rtphilipson
Posts: 3
Joined: Mon Mar 09, 2026 12:09 pm

Re: setenv and tzset not working

Postby rtphilipson » Thu Mar 12, 2026 12:50 pm

Does the forum allow deleting a post and going somewhere to hang one's head in shame? I honestly thought it was supposed to be day of month.

vvb333007
Posts: 71
Joined: Wed Jul 31, 2024 5:53 am
Location: Thailand
Contact:

Re: setenv and tzset not working

Postby vvb333007 » Thu Mar 12, 2026 8:59 pm

OK, it works. I rarely use local time in my systems and LibC internally uses the same variable for storing time structs, so my multiple calls to gmtime , localtime was overwriting. So, it even works for the more complicated POSIX formats.

Thanks for responding.
esp32 is multitask and multicore environment. means - use _r versions of libc functions and don't use versions with internal static buffers
Thanks!
Slava.

Who is online

Users browsing this forum: trendictionbot and 7 guests