Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > The output for localtime() is not correct.

Reply
Thread Tools

The output for localtime() is not correct.

 
 
david
Guest
Posts: n/a
 
      08-26-2003
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

http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Robert B. Clark
Guest
Posts: n/a
 
      08-26-2003
On 26 Aug 2003 07:18:46 -0700, (E-Mail Removed) (david) wrote:

> 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:\>


--
Robert B. Clark (email ROT13'ed)
Visit ClarkWehyr Enterprises On-Line at http://www.3clarks.com/ClarkWehyr/
 
Reply With Quote
 
 
 
 
Villy Kruse
Guest
Posts: n/a
 
      08-27-2003
Reply-To: nobody
Followup-To:

On 26 Aug 2003 07:18:46 -0700,
david <(E-Mail Removed)> wrote:


>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
 
Reply With Quote
 
Villy Kruse
Guest
Posts: n/a
 
      08-27-2003
On Tue, 26 Aug 2003 13:18:42 -0500,
Robert B. Clark <(E-Mail Removed)> wrote:


>
>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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FreeRIDE is not displaying the output in the output window juhunu@gmail.com Ruby 1 05-23-2006 01:08 PM
parse output screen ok but cant get desired output new file! chuck amadi Python 1 06-23-2004 02:16 PM
Sony Precision Cinema Progressive Output vs Component 480p Output Otto Pylot DVD Video 1 04-18-2004 09:49 PM
Is Fuji S3000 3.2m/pixel output, or 6 m/pixel interpolated output? Peter H Digital Photography 43 12-04-2003 02:35 PM
Output / Debug window output bug? John Bentley ASP .Net 0 09-10-2003 07:38 AM



Advertisments