Setting Timezone Seems Broken

Discussion to talk about software related topics only.
Post Reply
ecasey
Posts: 164
Joined: Sat Mar 26, 2011 9:34 pm

Setting Timezone Seems Broken

Post by ecasey »

I am trying to migrate from 2.8.6/7 to 2.9.5 on a MOD5282. Setting the Timezone doesn't work anymore. This code

Code: Select all

putenv("TZ=EST5EDT,M3.2.0/2,M11.1.0/2");
Now_t = time(NULL);
localtime_r(&Now_t,&Now_tm);
localtime(&Now_t);
cout << "Timezone: " << getenv("TZ") << endl;
cout << "Time: "<< put_time(localtime(&Now_t), "%c %Z") << endl;
cout << "Timezone: " << getenv("TZ") << endl;
has always worked, but in 2.9.5 it refuses to set the timezone to EST. Here is the output:

Code: Select all

    Timezone: EST5EDT,M3.2.0/2,M11.1.0/2
    Time: Sat Jun 18 12:16:07 2022 GMT
    Timezone: EST5EDT,M3.2.0/2,M11.1.0/2
I output the "Timezone ..." twice to show that nothing changed by calling localtime() inside the stream.
The revision notes show a change in time_t to a 64-bit value, but nothing about Timezones.

Did I miss something or is this a bug?

Ed
User avatar
TomNB
Posts: 576
Joined: Tue May 10, 2016 8:22 am

Re: Setting Timezone Seems Broken

Post by TomNB »

Hello,

I'm not an expert in the time zone area, but I'll give it try until someone else can join in. Most OS's have a tzset() function. In 2.9.x we have a tzsetchar() function. For example, tzsetchar((char*)"EST5EDT4,M3.2.0/01:00:00,M11.1.0/02:00:00"); for eastern time. There are two examples that might help:

1. \nburn\examples\StandardStack\EFFS\EFFS-HTTP\main.cpp
Shows how to set it as described above

2. \nburn\examples\StandardStack\NTPClient\main.cpp
Uses the timezones.h header file which provides all the time zones in an array you can use. Note that the actual time zone strings are in \nburn\system\timezones.cpp.

Let me know if that helps
ecasey
Posts: 164
Joined: Sat Mar 26, 2011 9:34 pm

Re: Setting Timezone Seems Broken

Post by ecasey »

Well, tzsetchar((char*)"EST5EDT4,M3.2.0/01:00:00,M11.1.0/02:00:00") worked. I never thought to try it. I was expecting the putenv() method to continue to work. I guess it was the move to GCC 8.1 that killed it. All's good with Timezones now; just have to make the switch to tzset() in all my applications.

Thanks Tom!

Ed
User avatar
pbreed
Posts: 1088
Joined: Thu Apr 24, 2008 3:58 pm

Re: Setting Timezone Seems Broken

Post by pbreed »

Wow I never realized that the putenv ever work.
I learned something.
Post Reply