Operating System - HP-UX
1837800 Members
9397 Online
110120 Solutions
New Discussion

how to set timezone variable in gettimeofday

 
inactive account
Frequent Advisor

how to set timezone variable in gettimeofday

I cant get a correct time when calling gettimeofday because I don't know how to initialise timezone with the value of TZ environment variable.

Who can help me ?

Thanx

Laurent
8 REPLIES 8
Animesh Chakraborty
Honored Contributor

Re: how to set timezone variable in gettimeofday

Hi,
Edit /etc/TIMEZONE file as per your location
TZ=SST-8
export TZ
then in /etc/profile

# Set the TIMEZONE

if [ -r /etc/TIMEZONE ]
then
. /etc/TIMEZONE
else
TZ=MST7MDT # change this for local time.
export TZ
fi
Did you take a backup?
Dietmar Konermann
Honored Contributor

Re: how to set timezone variable in gettimeofday

Hi, Laurent!

"Newer" libc revisions support the timezone default file /etc/default/tz. They use this timezone, whenever no TZ variable is set.

Check if your libc is capable:

# strings /usr/lib/libc.* |grep default/tz
/etc/default/tz
/etc/default/tz
/etc/default/tz

Then this should work:

# unset TZ
# date
Wed Oct 2 09:58:30 EDT 2002

# echo MET-1METDST > /etc/default/tz
# date
Wed Oct 2 15:58:48 METDST 2002

Regards...
Dietmar.
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
inactive account
Frequent Advisor

Re: how to set timezone variable in gettimeofday

All you suggest is already done, my problem is that I don't see how the system will use what's in TZ rather than the content of timezone variable...
Dietmar Konermann
Honored Contributor

Re: how to set timezone variable in gettimeofday

If the question is _how_ TZ is interpreted by the system, then you should look at the man page environ(5).
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
Dietmar Konermann
Honored Contributor

Re: how to set timezone variable in gettimeofday

Of course, looking at /usr/lib/tztab is also a good idea.

Regards...
Dietmar.
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
inactive account
Frequent Advisor

Re: how to set timezone variable in gettimeofday

I change the question:

I have a TZ variable on my system set to MET-1METDST
that's ok and I get correct time when I use the command date.
The problem I have is that when I use gettimeofday with or without timezone variable, the hour I get is wrong (it seems that it does not take into account the summer time (GMT -2) in France . So the time is one hour too small (GMT-1)...

Is that clearer?

Thanx again to those who take time to answer!
Dietmar Konermann
Honored Contributor

Re: how to set timezone variable in gettimeofday

Hi, Laurent!

One moment... you are really using gettimeofday(2)? This is a system call... and the man page says:

The gettimeofday() function obtains the current time, expressed as
seconds and microseconds since 00:00 Coordinated Universal Time (UTC),
January 1, 1970, and stores it in the timeval structure pointed to by
tp. The resolution of the system clock is unspecified.

So this is of course NOT depending in TZ.

Regards...
Dietmar.
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
inactive account
Frequent Advisor

Re: how to set timezone variable in gettimeofday

It also says:
SYNOPSIS
#include

int gettimeofday(struct timeval *tp,
struct timezone *tzp);

DESCRIPTION
The structures pointed to by tp and tzp are defined in as:

struct timeval {
unsigned long tv_sec; /* seconds since Jan. 1, 1970 */
long tv_usec; /* and microseconds */
};

struct timezone {
int tz_minuteswest; /* of UTC */
int tz_dsttime; /* type of DST correction to apply */
};

The timezone structure indicates the local time zone (measured in
minutes of time westward from UTC), and a flag that, if nonzero,
indicates that Daylight Savings Time applies locally during the
appropriate part of the year. Programs should use this time zone
information only in the absence of the TZ environment variable.

Security Restrictions
Only a user with appropriate privileges can set the time of day.

RETURN VALUE
gettimeofday() return the following values:

0 Successful completion.
-1 Failure. errno is set to indicate the error.

ERRORS
gettimeofday() fails, errno is set to the following value.

[EFAULT] An argument address referenced invalid memory.
The reliable detection of this error is
implementation dependent.

and I use this call to store the current timestamp in some file.
When I read the file and intrepret the timestamp, with mktime and localtime_r
I have a delta of 1 hour