当我执行以下代码时:
#include <time.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
time_t rawtime = 0;
time_t secs;
struct tm* timeinfo = gmtime(&rawtime);
printf("rawtime : %s\n", asctime(timeinfo));
secs = mktime(timeinfo);
printf("converted time : %s\n", asctime(gmtime(&secs)));
return 0;
}
输出为:
rawtime : Thu Jan 1 00:00:00 1970
converted time : Wed Dec 31 23:00:00 1969
为什么这一个小时有差异?
我正在运行Ubuntu 14.10 64位btw。
因为mktime将本地时间而不是系统时间(gmtime)转换为时间戳。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句