Operating System - HP-UX
1827293 Members
2695 Online
109717 Solutions
New Discussion

Re: Timezone info in C++ on HP-UX...

 
Allan Stirrett
Occasional Advisor

Timezone info in C++ on HP-UX...

Hi,

Much of our code depeneds on current time AND timezone/daylight information. We use the ftime() routine to tell us what timezone we're in (as an offset from GMT) as well as whether daylight savings time is currently in effect.

The problem is that our code doesn't work as expected on HP-UX 11i, compiled with aCC 03.30. We are in EST (5 hours behind GMT), yet the ftime() results on HP-UX show that we are 7 hours behind GMT, AND that daylight savings time is in effect NOW.

I've created a small test app which works correctly on Win32 and Linux, but gives the wrong time from ftime() on HP-UX (though the global timezone variable appears to have the right value, strangely enough).

Any thoughts? Anything I have to pre-initialize before calling ftime()? Anything in the OS that needs setting before these will work (the TZ environment variable is set to EST5EDT, and the global "timezone" variable works right...)? Thanks in advance for any insight or tips you can pass along.

Allan Stirrett.
3 REPLIES 3
James R. Ferguson
Acclaimed Contributor

Re: Timezone info in C++ on HP-UX...

Hi Allan:

Instead of using 'ftime()' use 'ctime{}'. 'ftime()' leverages the deprecated kernel parameter 'timezone' (and 'dst') instead of the TZ environmental variable.

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: Timezone info in C++ on HP-UX...

Hi Allan:

ftime() is one of those archaic functions which should never be used in a modern program.
The function you should be using is ctime() and it is available in Linux and Microsoft C++.
If you also need the high resolution that ftime provides you can use gettimeofday() or use ftime() only for the millitm values and time values. You can then send the time value into ctime() to get the time properly decoded.
If it ain't broke, I can fix that.
Allan Stirrett
Occasional Advisor

Re: Timezone info in C++ on HP-UX...

Hi,

Thanks for the replies. I think I found the answer in a man page that pointed in a different direction.

It looks like what I really want to use on ALL platforms is localtime(). It sets the global "timezone" value correctly on all 4 of our platforms, and the "struct tm" that it returns contains a tm_isdst flag which CONSISTENTLY tells us whether we're CURRENTLY in daylight savings time (the dstflag from ftime() was interpretted differently in different OS's).

This gives us the info we need to convert time_t's to/from a foreign date/time format. Thanks again for the suggestions.

Allan Stirrett.