问题由来css
时间戳转换(时间戳:自 1970 年1月1日(00:00:00 )至当前时间的总秒数。)linux
#include <stdio.h> #include <time.h> int main(int argc, const char * argv[]) { time_t t; struct tm *p; t=1408413451; p=gmtime(&t); char s[80]; strftime(s, 80, "%Y-%m-%d %H:%M:%S", p); printf("%d: %s\n", (int)t, s); }
结果函数
1408413451 2014-08-19 01:57:1408384651
但是利用命令在linux终端计算的结果不一spa
[###t]$ date -d @1408413451
Tue Aug 19 09:57:31 CST 2014
经过比较发现,二者正好差8个小时,CST表示格林尼治时间,经过strftime()函数能够输出时区,改正以下code
#include <stdio.h> #include <time.h> int main(int argc, const char * argv[]) { time_t t; struct tm *p; t=1408413451; p=gmtime(&t); char s[80]; strftime(s, 80, "%Y-%m-%d %H:%M:%S::%Z", p); printf("%d: %s\n", (int)t, s); }
结果blog
1408413451: 2014-08-19 01:57:31::GMT
深究io
GMT(Greenwich Mean Time)表明格林尼治标准时间。十七世纪,格林威治皇家天文台为了海上霸权的扩张计画而进行天体观测。1675年旧皇家观测所正式成立,经过格林威治的子午线做为划分地球东西两半球的经度零度。观测所门口墙上有一个标志24小时的时钟,显示当下的时间,对全球而言,这里所设定的时间是世界时间参考点,全球都以格林威治的时间做为标准来设定时间,这就是咱们耳熟能详的「格林威治标准时间」(Greenwich Mean Time,简称G.M.T.)的由来。class
CST却同时能够表明以下 4 个不一样的时区:date
Central Standard Time (USA) UT-6:00 Central Standard Time (Australia) UT+9:30 China Standard Time UT+8:00 Cuba Standard Time UT-4:00
可见,CST能够同时表示美国,澳大利亚,中国,古巴四个国家的标准时间。终端
好了二者差8个小时(CST比GMT晚/大8个小时),GMT+8*3600=CST,代码以下
#include <stdio.h> #include <time.h> int main(int argc, const char * argv[]) { time_t t; struct tm *p; t=1408413451; p=gmtime(&t); char s[80]; strftime(s, 80, "%Y-%m-%d %H:%M:%S::%Z", p); printf("%d: %s\n", (int)t, s); t=1408413451 + 28800; p=gmtime(&t); strftime(s, 80, "%Y-%m-%d %H:%M:%S", p); printf("%d: %s\n", (int)t, s); return 0; }
结果
1408413451: 2014-08-19 01:57:31::GMT 1408442251: 2014-08-19 09:57:31
linux平台
Tue Aug 19 09:57:31 CST 2014