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.
setenv and tzset not working
Re: setenv and tzset not working
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.
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.
Slava.
-
rtphilipson
- Posts: 3
- Joined: Mon Mar 09, 2026 12:09 pm
Re: setenv and tzset not working
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.
Thanks for responding.
-
MicroController
- Posts: 2669
- Joined: Mon Oct 17, 2022 7:38 pm
- Location: Europe, Germany
Re: setenv and tzset not working
doesn't look right, indicating the 8th week of March. You probably want "...M3.2.0..." instead.Code: Select all
"...M3.8.0..."
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
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.
Re: setenv and tzset not working
esp32 is multitask and multicore environment. means - use _r versions of libc functions and don't use versions with internal static buffersOK, 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.
Thanks!
Slava.
Slava.
Who is online
Users browsing this forum: ChatGPT-User and 7 guests