[LCP]time

Neeraj neerajk at cse.iitk.ac.in
Sat Jan 19 22:15:32 UTC 2002


=> [bjdev: C]$ ./time
=> 12 41 10 23 11 35 1
=> Mon Dec 23 10:41:12 1935
=> 
=> The actual time however is:
=> [bjdev: C]$ date
=> Fre Jan 18 20:35:29 CET 2002
=> 
=> 
=> Can somebody be so kind and tell me what I am doing wrong (maybe it's time 
=> for me to get some sleep .... :))

Sure why not! 

=> #include <time.h>
=> #include <stdio.h>
=> 
=> int main( int argc, char **argv) {
=> 
=>     time_t          now;
Initialise now with the current time in number of seconds elapsed since jan
1,1970. 

=>     tt = localtime(&now);

So, you will get tt with the value present in 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);

Now you will get the right value in tt. See man of "localtime".

The ctime(), gmtime() and localtime() functions all take an argument of data
type time_t which represents calendar time.  When interpreted as an absolute
time value, it represents the number of seconds elapsed since 00:00:00 on
January 1, 1970, Coordinated Universal Time (UTC). 





More information about the linuxCprogramming mailing list