UTC時間 與本地時間測試

#include <time.h>
#include <stdio.h>

int main()
{

        time_t timeLocal;
        time_t timeUTC;
        struct tm tmLocal;
        struct tm tmUTC;
        char strTime[32] = {};

        time(&timeLocal);       // utc time
        localtime_r(&timeLocal, &tmLocal);

        timeUTC = mktime(&tmLocal);  // utc time
        localtime_r(&timeUTC, &tmUTC); // local time
        strftime(strTime, 32, "%Y-%m-%d_%H:%M:%S", &tmUTC);
        printf("strTime: %s\n", strTime);

        gmtime_r(&timeUTC, &tmUTC);    // utc time
        strftime(strTime, 32, "%Y-%m-%d_%H:%M:%S", &tmUTC);
        printf("strTime: %s\n", strTime);

        printf("timeLocal = %d; timeUTC = %d  local-utc = %d\n",
                timeLocal, timeUTC, (timeLocal-timeUTC)/3600);

        printf(ctime(&timeUTC));
        return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章