Online Expert Day - HPE Data Storage - Live Now
April 24/25 - Online Expert Day - HPE Data Storage - Live Now
Read more
cancel
Showing results for 
Search instead for 
Did you mean: 

Wrong strftime time zone

SOLVED
Go to solution
Modris Bremze
Esteemed Contributor

Wrong strftime time zone

Hi.
The plan is to add timestamps to command history entries on HP-UX 11.23 ia64 machines for non-root users by changing the default shell to bash and exporting appropriate HISTTIMEFORMAT in /etc/profile. Everything went fine until I had to set HISTTIMEFORMAT. Everything I tried gave me the wrong time. After fannying around a bit I found out that strftime somehow lives in a different time zone, because my timestamps were off by exactly 7 hours, and %Z returned EST (east coast US, I believe), not EET (Eastern European time), which is the correct one. The sum of this difference is 7 hours (UTC+2 for EET and UTC-5 for EST). The 'echo $TZ' returns EET-2EETDST as it should be. Where does strftime get that I should be shown time in the US, not the local time?
4 REPLIES
Bill Hassell
Honored Contributor
Solution

Re: Wrong strftime time zone

strftime() calls tzset() (see both man pages) and tzset does this:

tzset() Sets the values of the external variables timezone,
daylight, and tzname according to the contents of
the TZ environment variable (independent of any time
value). The functions localtime(), mktime(),
ctime(), asctime(), and strftime() (see
strftime(3C)) call tzset() and use the values
returned in the external variables described below
for their operations. tzset() can also be called
directly by the user.

When the environment variable TZ is not set, tzset()
checks the default file /etc/default/tz for the
timezone value and sets timezone values based on
that. The file /etc/default/tz contains the
timezone value used by tzset() when the environment
variable TZ is not set. The format for the file is
same as TZ format without the prefix TZ=. Please
check environ(5) for TZ format. The order of TZ
setting is as follows.

1) if the environment variable TZ is present, it
takes precedence.

2) if TZ is not present, then the value contained in
/etc/default/tz is used for the default.

ctime(3C) ctime(3C)

3) if /etc/default/tz is not set then the default
value is equivalent to EST5EDT (Eastern Standard
Time) of the USA.


What do you have in /etc/default/tz? I would always match /etc/TIMEZONE (called by login profiles) and /etc/default/tz (called by library functions) to avoid confusing results.


Bill Hassell, sysadmin
Dennis Handly
Acclaimed Contributor

Re: Wrong strftime time zone

>The 'echo $TZ' returns EET-2EETDST as it should be.

Are you sure you have exported TZ?
Have you exported it BEFORE you invoke bash?
I.e. bash may only check the values of env vars when it starts, not the separate set of variables that's part of the user environment.
(At least that's how a real shell works.)

You may have to have bash exec itself in its .profile.

If your machine only works in one timezone, then as Bill mentioned, setting /etc/default/tz /etc/TIMEZONE may help.
Modris Bremze
Esteemed Contributor

Re: Wrong strftime time zone

Thanks for the replies, people.
Regarding what Dennis said - if I understood You correctly, I believe that $TZ is always exported and has the correct value in both the root shell (sh) and user shell (bash). The /etc/TIMEZONE has the correct TZ set and exportet in it.
Regarding Bill's comment - I checked /etc/defaults/tz, and it had the EST set in it. I changed it to EET-2EETDST and now strftime displays the time correctly. Strange somehow that it was not the $TZ strftime based its time zone on, although it was exported and available. Anyway, the problem is solved now. Thanks again.
Dennis Handly
Acclaimed Contributor

Re: Wrong strftime time zone

>I changed it to EET-2EETDST and now strftime displays the time correctly. Strange somehow that it was not the $TZ strftime based its time zone on, although it was exported and available.

As I said, TZ must be exported BEFORE bash starts. Which is near impossible for a login shell.