1) time (&timep); 直接用 syscall从os取时间,注意是取UTC时间
2) gmtime 可以把 time_t 转化为 year, month,day, hour, seconds, 直接转化,不考虑时区
3) ctime 把时间转化为串,但时间转化被 local 时间了
#include<time.h> void main() { time_t timep; time (&timep); printf("%s",ctime(&timep));// ctime already convert it to local time ) // the follow print UTC directly struct tm *p; p=gmtime(&timep); printf("%d-%d-%d%d:%d:%d\n",(1900+p->tm_year), (1+p->tm_mon),p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec); }
运行
./a.out
Fri Sep 14 16:13:59 2012 // ctime 看到的时间
2012-9-14 8:13:59 //gmtime 取得的时间
Fri Sep 14 08:14:04 UTC 2012