当使用mktime转换为time_t时,为什么我的time_t值会在夏令时给定?

时间:2014-06-02 19:06:31

标签: c++ time

以下似乎很奇怪。我处于夏令时的中央时间,应该与UTC相差-5。当我输出struct tm对象的字符串表示时,它看起来是正确的。但是当我将struct tm转换为time_t并且我查看这些值时,我得到21600的差异,即6小时(21600秒/ 60/60 = 6小时)。我希望它是5个小时。

我在Ubuntu 12.04上运行Gcc 4.8.2。

 9      time_t t1 = time(nullptr);
 10     time_t t2 = t1;
 11     
 12     char buf1[64];
 13     char buf2[64];
 14     
 15     tm utcTm, localTm;
 16     tm* tmp;
 17     
 18     tmp = gmtime(&t1);
 19     utcTm = *tmp;
 20     
 21     tmp = localtime(&t2);
 22     localTm = *tmp;
 23     
 24     strftime(buf1, 64, "%c %Z", &utcTm);
 25     strftime(buf2, 64, "%c %Z", &localTm);
 26     
 27     cout << "UTC: " << buf1 << endl;
 28     cout << "local: " << buf2 << endl;
 29     
 30     cout << "Is DST? " << utcTm.tm_isdst << " " << localTm.tm_isdst << endl;
 31  
 32     time_t t3 = mktime(&utcTm);
 33     time_t t4 = mktime(&localTm);
 34  
 35     cout << "UTC: " << t3 << " Local: " << t4 <<  " Diff: " << (t3 - t4) << endl;
 36     cout << "Orig UTC: " << t3 << " Orig Local: " << t4 <<  " Diff: " << (t3 - t4) << endl;

输出:

UTC:星期一2月2日19:05:23 GMT

local:Mon Jun 2 14:05:23 CDT

是夏令时吗? 0 1

UTC:1401757523 Local:1401735923 Diff:21600

0 个答案:

没有答案