一般我用C写unix时间戳是这样子的
#include<stdio.h> #include<time.h> void printfDateTimeStr(struct tm *stm){ char weekday[][4]={"天","一","二","三","四","五","六"}; printf("timestr=%04d-%02d-%02d %02d:%02d:%02d 星期%s\n",stm->tm_year+1900,stm->tm_mon+1,stm->tm_mday,stm->tm_hour,stm->tm_min,stm->tm_sec,weekday[stm->tm_wday]); } int main(){ time_t unix_timestamp=1429641418; unix_timestamp = time(NULL); struct tm *tmdate=localtime(&unix_timestamp); printf("unix_timestamp 现在的时间戳是=%d\n", unix_timestamp); printf("Local Time is :asctime=%s", asctime(tmdate)); printf("格林威治GMT Time is :gmtime=%s", asctime(gmtime(&unix_timestamp))); printfDateTimeStr(tmdate); printf("========================================\n"); printf("input your unix_timestamp:"); scanf("%d",&unix_timestamp); struct tm *stm=localtime(&unix_timestamp); printf("你的 unix_timestamp 时间戳是=%d\n",unix_timestamp); printf("你的 Local Time is :asctime=%s",asctime(stm)); printf("你的 格林威治GMT Time is :gmtime=%s",asctime(gmtime(&unix_timestamp))); printfDateTimeStr(stm); char c; scanf("%c",&c); return 0; }
运行结果:
unix_timestamp 现在的时间戳是=1429787951
Local Time is :asctime=Thu Apr 23 19:19:11 2015
格林威治GMT Time is :gmtime=Thu Apr 23 11:19:11 2015
timestr=2015-04-23 11:19:11 星期四 dst[0]
========================================
input your unix_timestamp:0
你的 unix_timestamp 时间戳是=0
你的 Local Time is :asctime=Thu Jan 01 08:00:00 1970
你的 格林威治GMT Time is :gmtime=Thu Jan 01 00:00:00 1970
timestr=1970-01-01 00:00:00 星期四 dst[0]
time_t =0 的时候 其实是 1970-01-01 00:00:00 到UTC 0时区的秒数,而不是 我们北京时间UTC+8;所以gmtime比localtime 小8小时。
在大多数的UNIX系统中UNIX时间戳存储为32位,这样会引发2038年问题或Y2038。
早晚替换为64位。
时间: 2024-10-13 08:06:04