Thread: localtime(), time(). What happened?

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    37

    localtime(), time(). What happened?

    This is an example from my textbook. I was studying for an exam and after compiling a program I got surprising results. The book didn't mention it at all. Can anybody explain what happened?

    The output is:


    currentdate is Mon Dec 09 21:25:26 2002
    Tomorrow is 12/10/20102.
    0.05 seconds used by the processor.
    Press any key to continue
    Where did the 20102 come from??? What I don't know about time() then?

    And the code is:
    Code:
    #include <time.h>                                         
    #include <stdio.h>
    
    int main( void )
    {
        time_t t1;                                 
        struct tm *tptr;
        clock_t ticks;                             
        char *s;
    
        if ( ( t1 = time(( time_t * ) 0 )) != ( time_t )-1 ) {
            s = ctime( &t1 );
            printf( "currentdate is %s", s );
            tptr = localtime( &t1 );
            printf( "Tomorrow is %d/%d/20%d.\n",              
                                           tptr->tm_mon+1, 
                                           tptr->tm_mday+1,
                                           tptr->tm_year );
        }
        else
            printf( "Error with the time() function\n" );
    
                                                              
        if (( ticks = clock() ) != ( clock_t )-1 )
            printf( "%4.2f seconds used by the processor.\n",
                                      (double)ticks/CLK_TCK ); 
        else
            printf( "Error with the clock() function\n" );
        return 0;
    }
    Thanks

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Anyone can correct me if i'm wrong, but this reguards y2k. 99+1 = 100.

  3. #3
    drmauro223
    Guest
    It looks like the tm_year value represents number of years after 1900. Change it to:

    printf( "Tomorrow is %d/%d/%d.\n",
    tptr->tm_mon+1,
    tptr->tm_mday+1,
    1900 + tptr->tm_year );

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    tm_year is the number of years since 1900. so change 20%d to %d and instead of tm_year do 1900+tm_year

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    37
    So you're saying that my textbook wasn't really updated (new edition 2002 and they didn't correct the code in the book... OK, thanks for explanation!

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Maybe it is made for another OS?
    (I am not sure if that is just for *NIX or if Windows stores time like that too (does windows even use time_t and struct tm?))

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    That code is windows compatible if that is what you were asking.

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Originally posted by master5001
    That code is windows compatible if that is what you were asking.
    But does windows store tm_year has 1900 + tm_year?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Representing floats with color?
    By DrSnuggles in forum C++ Programming
    Replies: 113
    Last Post: 12-30-2008, 09:11 AM
  3. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM
  4. Checking parts of a structure
    By DocDroopy in forum C Programming
    Replies: 11
    Last Post: 08-05-2002, 07:45 AM