The output for localtime() is not correct.

Discussion in 'C Programming' started by david, Aug 26, 2003.

  1. david

    david Guest

    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

     
    david, Aug 26, 2003
    #1
    1. Advertising

  2. On 26 Aug 2003 07:18:46 -0700, (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/
     
    Robert B. Clark, Aug 26, 2003
    #2
    1. Advertising

  3. david

    Villy Kruse Guest

    Reply-To: nobody
    Followup-To:

    On 26 Aug 2003 07:18:46 -0700,
    david <> 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
     
    Villy Kruse, Aug 27, 2003
    #3
  4. david

    Villy Kruse Guest

    On Tue, 26 Aug 2003 13:18:42 -0500,
    Robert B. Clark <> 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
     
    Villy Kruse, Aug 27, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dan

    correct or not correct?

    Dan, Oct 2, 2003, in forum: HTML
    Replies:
    7
    Views:
    468
  2. Bimal
    Replies:
    1
    Views:
    486
    Tim Prince
    Aug 20, 2003
  3. Udo Melis

    localtime import not working

    Udo Melis, Sep 15, 2004, in forum: Python
    Replies:
    1
    Views:
    304
    Jeff Epler
    Sep 16, 2004
  4. J.Ram
    Replies:
    7
    Views:
    681
  5. Dan Vesma
    Replies:
    2
    Views:
    107
    Brian Wakem
    Oct 7, 2004
Loading...

Share This Page