Operating System - Linux
1748186 Members
4500 Online
108759 Solutions
New Discussion юеВ

time() function dependent on timezone?

 
SOLVED
Go to solution
Mohanasundaram_1
Honored Contributor

time() function dependent on timezone?

Hi Folks,

I got this query from my programming team. This is a rp3440 system with HP-UX 11.11.

Subject: Unix System clock

Description: In HP-UX the "man 3 time" function call gives following explanation.
Time() returns the time since 00:00:00 GMT, Jan. 1, 1970, measured in seconds. This is the value of the UNIX system clock.

This means number of seconds elapsed since 1/1/1970 00:00:00 irrespective of the timezone /daylight saving.

When I execute the following program HP-UX machine :
main()
{
time_t t;
struct tm ptm;

time(&t)
printf("Timetick %d\n", t);

gmtime_r(&t, &ptm);
printf( "\n 0 gtime %04d-%02d-%02d %02d:%02d:%02d pre-isdst=0\n", ptm.tm_year + 1900,
ptm.tm_mon + 1, ptm.tm_mday, ptm.tm_hour, ptm.tm_min, ptm.tm_sec, ptm.tm_isdst );

localtime_r(&t, &ptm);
printf( "\n 0 local %04d-%02d-%02d %02d:%02d:%02d pre-isdst=0\n", ptm.tm_year + 1900,
ptm.tm_mon + 1, ptm.tm_mday, ptm.tm_hour, ptm.tm_min, ptm.tm_sec, ptm.tm_isdst );
return;
}

Output is when TZ=GMT+4
timetick 1208790892
0 gtime 2008-04-21 15:14:52 pre-isdst=0
0 local 2008-04-21 11:14:52 pre-isdst=0

Output is when TZ=TEST3:30
timetick 1208789056
0 gtime 2008-04-21 14:44:16 pre-isdst=0
0 local 2008-04-21 11:14:16 pre-isdst=0
Information Required:
from the above example time() function does depend on the timezone. Is it TRUE ? Is there any other function which will return number of seconds elapsed since 1970 at GMT+0/UTC. Which should not use the timezone or the daylight configurations.

Useful answers will be rewarded with points.

With regards,
Mohan.
Attitude, Not aptitude, determines your altitude
12 REPLIES 12
Steven E. Protter
Exalted Contributor

Re: time() function dependent on timezone?

Shalom Mohan,

The time() function does not have any relation to timezone setting.

Time is calculated by the number of seconds since January 1, 1970. Time zone merely controls display.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Steven Schweda
Honored Contributor

Re: time() function dependent on timezone?

If you want to know what time() does, print
the output from time() (in this case, "t"),
not the output from gmtime_r() or
localtime_r().
Mohanasundaram_1
Honored Contributor

Re: time() function dependent on timezone?

Hi Steven,

Thanks for your reply. In such a case is it not possible to see the time irrespective of the timezone?

From the question I got, I understand that the programmer wants to display the time without the Timezone affecting its display. In other words, how can we display time irrespective of timezone, if possible.

This topic is a bit alien to me and am not sure if I am asking the correct question here.

With regards,
Mohan.
Attitude, Not aptitude, determines your altitude
Steven Schweda
Honored Contributor

Re: time() function dependent on timezone?

gmtime[_r]() should be independent of any
time zone settings. I'd expect
localtime[_r]() to depend on the time zone.

man localtime

localtime() Correct for the time zone and any summer time zone
adjustments (such as Daylight Savings Time in the
USA), according to the contents of the TZ
environment variable (see Environment Variables
below). [...]
Mohanasundaram_1
Honored Contributor

Re: time() function dependent on timezone?

Hi Steven Schweda,

It is a bit of co-incidence that two Stevens replied to me :-)

I have given your first reply to the people who raised the query. I will post an update based on their response.

I will assign points to you during this update. Thanks for your time.

With regards,
Mohan.
Attitude, Not aptitude, determines your altitude
Steven Schweda
Honored Contributor
Solution

Re: time() function dependent on timezone?

After some editing of your source, I don't
see a problem with the time() or gmtime_r()
results:

td176> echo $TZ ; ./tt ; export TZ=CST6CDT ; echo $TZ ; ./tt
EST5EDT
Timetick 1209031863
gmtime 2008-04-24 10:11:03 pre-isdst=0
local 2008-04-24 06:11:03 pre-isdst=1
CST6CDT
Timetick 1209031863
gmtime 2008-04-24 10:11:03 pre-isdst=0
local 2008-04-24 05:11:03 pre-isdst=1
Mohanasundaram_1
Honored Contributor

Re: time() function dependent on timezone?

Hi Steven Schweda,

Your Last post is encouraging. May I ask you what kind of edit was done on the source? This may solve my problem altogether.

With regards,
Mohan.
Attitude, Not aptitude, determines your altitude
Dennis Handly
Acclaimed Contributor

Re: time() function dependent on timezone?

>In HP-UX the "man 3 time"

That's time(2), it is a system call. ctime(3) are a libc functions.

Some minor performance tweaks: time(&t)
Don't do this, it makes the kernel sweat. Instead do:
t = time(NULL);

>printf("Timetick %d\n", t);

The type of time_t is long, so use:
printf("Timetick %ld\n", t);

>how can we display time irrespective of timezone, if possible.

You can't. All times must be relative to some time zone, possibly UTC, unless you want to see the raw time_t decimal number.
Steven Schweda
Honored Contributor

Re: time() function dependent on timezone?

> [...] what kind of edit [...]?

Enough to get it through the compiler.

td192> cat tt.c
#include
#include

main()
{
time_t t;
struct tm ptm;

time(&t);
printf("Timetick %d\n", t);

gmtime_r(&t, &ptm);
printf( "gmtime %04d-%02d-%02d %02d:%02d:%02d pre-isdst=%d\n",
ptm.tm_year + 1900, ptm.tm_mon + 1, ptm.tm_mday,
ptm.tm_hour, ptm.tm_min, ptm.tm_sec, ptm.tm_isdst );

localtime_r(&t, &ptm);
printf( "local %04d-%02d-%02d %02d:%02d:%02d pre-isdst=%d\n",
ptm.tm_year + 1900, ptm.tm_mon + 1, ptm.tm_mday,
ptm.tm_hour, ptm.tm_min, ptm.tm_sec, ptm.tm_isdst );

return 0;
}