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

1. ## 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. Anyone can correct me if i'm wrong, but this reguards y2k. 99+1 = 100.

3. 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. 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. 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. 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. That code is windows compatible if that is what you were asking.

8. 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?