zoukankan      html  css  js  c++  java
  • unix时间戳time_t与UTC时区的关系

    一般我用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
    ",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
    ", unix_timestamp);
        printf("Local Time is :asctime=%s", asctime(tmdate));
        printf("格林威治GMT Time is   :gmtime=%s", asctime(gmtime(&unix_timestamp)));
        printfDateTimeStr(tmdate);
        
        printf("========================================
    ");
        printf("input your unix_timestamp:");
        scanf("%d",&unix_timestamp);
        
        struct tm *stm=localtime(&unix_timestamp);
        printf("你的 unix_timestamp 时间戳是=%d
    ",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位。

  • 相关阅读:
    童鞋,[HttpClient发送文件] 的技术实践请查收
    有关[Http持久连接]的一切,卷给你看
    浅谈MemoryCache的原生插值方式
    HTTP1.1 KeepAlive到底算不算长连接?
    C2 hits the assertion assert(base>is_AddP()) failed: should be addp but is Phi
    C2 EA
    OOM Hook
    C2 Loop predicate
    C2 Build IR
    C2 CCP
  • 原文地址:https://www.cnblogs.com/ayanmw/p/4451486.html
Copyright © 2011-2022 走看看