tmp = gmtime(&time_current);
printf("gmtime: the time is:
%d%d%d/%d:%d:%d
",tmp->tm_year-100+2000,tmp->tm_mon+1,tmp->tm_mday,tmp->tm_hour,tmp->tm_min,tmp->tm_sec);
tmp = localtime(&time_current);
printf("localtime: the time is:
%d%d%d/%d:%d:%d
",tmp->tm_year-100+2000,tmp->tm_mon+1,tmp->tm_mday,tmp->tm_hour,tmp->tm_min,tmp->tm_sec);
return 0; }
代码运行结果:
Linux源码中的mktime算法从CMOS中读出来的系统时间并不是time_t类型的,而是类似于struct tm那样,年月日时分秒都是分开存储的。想要把它转化成系统便于处理的time_t类型,就需要算法进行转换。Linux的源代码用了短短的几行代码就爱完成了这个复杂的转换(Gauss算法)。源代码:include/linux/time.hstaticinlineunsignedlongmktime(unsignedint year,unsignedint mon, unsignedint day,unsignedint hour, unsignedintmin,unsignedint sec) { if(0 >=(int)(mon -= 2)){/**//*
1..12 -> 11,12,1..10 */ mon += 12;/**//*
Puts Feb last since it has leap day */ year -= 1; } return((( (unsignedlong)(year/4 - year/100 + year/400 + 367*mon/12 + day)+ year*365 - 719499 )*24 + hour /**//*
now have hours */ )*60 +min/**//*
now have minutes */ )*60 + sec;/**//*
finally seconds */ } 源码解析参考网址:http://blog.chinaunix.net/uid-20532339-id-1931780.html