The output for localtime() is not correct.

D

david

I used the localtime() function in the following code snippet:

....
tm = localtime(&ps->date);
printf("---------------------\n");

printf("Date: %d/%02d/%02d %02d:%02d:%02d\n",
1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);
....

The printout listed the time one hour off from GMT (probably due to
daylight saving..) instead in local time which is EDT.

The timezone for the platform (Linux Redhat8.0) seems to be setup
correctly. I try to set the TZ with different values, but the results
are for some reason not stable.

Any idea?

Thanks a lot!

David

(e-mail address removed)
 
R

Robert B. Clark

tm = localtime(&ps->date);
printf("---------------------\n");

printf("Date: %d/%02d/%02d %02d:%02d:%02d\n",
1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);
...
The printout listed the time one hour off from GMT (probably due to
daylight saving..) instead in local time which is EDT.

The timezone for the platform (Linux Redhat8.0) seems to be setup
correctly. I try to set the TZ with different values, but the results
are for some reason not stable.

On the several platforms I have to hand at the moment (none of which are RH
8.0), daylight time offsets appear to be applied to the GMT, not to the
local time.

You may want to check a newsgroup devoted to your compiler and/or platform,
as tzset(), while a popular function, is not part of ANSI/ISO C.

That being said, compare this code with TZ=EST5EDT and TZ=EST5:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
time_t t;
struct tm *gmt, *lat;

tzset();

printf("Local timezone is TZ=%s\n\n", getenv("TZ"));

t = time(NULL);

lat = localtime(&t);
printf("Local time is : %s", asctime(lat));

gmt = gmtime(&t);
printf("GMT is : %s", asctime(gmt));

return 0;
}

Output:

C:\>set tz=EST5EDT

C:\>ltime
Local timezone is TZ=EST5EDT

Local time is : Tue Aug 26 12:27:19 2003
GMT is : Tue Aug 26 16:27:19 2003

C:\>set tz=EST5

C:\>ltime
Local timezone is TZ=EST5

Local time is : Tue Aug 26 12:27:26 2003
GMT is : Tue Aug 26 17:27:26 2003

C:\>
 
V

Villy Kruse

Reply-To: nobody
Followup-To:

I used the localtime() function in the following code snippet:

...
tm = localtime(&ps->date);
printf("---------------------\n");

printf("Date: %d/%02d/%02d %02d:%02d:%02d\n",
1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);
...

The printout listed the time one hour off from GMT (probably due to
daylight saving..) instead in local time which is EDT.

The timezone for the platform (Linux Redhat8.0) seems to be setup
correctly. I try to set the TZ with different values, but the results
are for some reason not stable.

Any idea?

Your timezone and/or system time isn't set properly, very common. How
to set this is RedHat or linux specific and therefore not a topic
in a C newsgroup.


Villy
 
V

Villy Kruse

On the several platforms I have to hand at the moment (none of which are RH
8.0), daylight time offsets appear to be applied to the GMT, not to the
local time.


UTC time is never influenced by summertime or daylight time, ever.
The local wall clock time may be. BTW GMT is the former name for UTC
and is now used as the name for British standard time.

Whether the system time is running wall clock time or UTC time is
system dependent, and the zone offset is either subtracted from
the local time to get UTC time, or added to the UTC time to get
localtime. This is all system dependent and not a C issue.


Villy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top