[LCP]time

ianezz at sodalia.it ianezz at sodalia.it
Mon Jan 21 20:34:35 UTC 2002


Joachim Bauernberger, pigiando tasti a caso sul citofono, ha scritto:

 > Can somebody be so kind and tell me what I am doing wrong (maybe it's time 
 > for me to get some sleep .... :))


 > 
 > 
 > Here is the code:
 > 
 > #include <time.h>
 > #include <stdio.h>
 > 
 > int main( int argc, char **argv) {
 > 
 >     time_t          now;
 >     struct tm       *tt;
 >     char *c;
 > 
       /* Initialize ``now'' with the current time */
       /* now = time(NULL) is another possibility */
       time(&now);

 >     tt = localtime(&now);
 >     //c = ctime(&now);
 > 
 >     fprintf(stdout,"%d %d %d %d %d %d %d\n",tt->tm_sec, tt->tm_min, 
 > tt->tm_hour, tt->tm_mday, tt->tm_mon, tt->tm_year, tt->tm_wday);

The tm_year member of struct tm hold the number of years since 1900,
so you have to add 1900 to it in order to get the current year.

Also, you have to add 1 to tm_mon, since January is 0, February is 1,
etc.

That said, strftime() may be a better choice to get a textual
representation of timestamps.

-- 
UNIX diapers by Pannolini USPTO 2039887  http://www.uspto.gov
Matteo Ianeselli      ianezz AT sodalia.it  (+39) 0461 316452
Visita il LinuxTrent:            http://www.linuxtrent.it



More information about the linuxCprogramming mailing list