大佬教程收集整理的这篇文章主要介绍了为什么 std::localtime 会给出与 UTC 不同的偏移量?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在英国,所以我的时区目前与 UTC 相同。在下面的代码中,如果我将不同的时间戳传递给 localtime,我会从 UTC 获得不同的偏移量(通过 gmtime)。为什么是这样?我的时区不会因调用而改变,所以我没想到偏移量会受到给定时间戳的影响。
#include <ctime>
#include <iostream>
int main()
{
for (time_t t = 0; t < 300000000; t+=10000000)
{
std::tm *timeInfo;
// Get UTC time
timeInfo = std::gmtime(&t);
auto gm_hours = timeInfo->tm_hour;
// Get localtime
timeInfo = std::localtime(&t);
// Print offset from UTC
std::cout << "timestamp: " << t << " Hours ahead of UTC : " << (timeInfo->tm_hour - gm_hours) << std::endl;
}
return 0;
}
输出:
timestamp: 0 Hours ahead of UTC : 1
timestamp: 10000000 Hours ahead of UTC : 1
timestamp: 20000000 Hours ahead of UTC : 1
timestamp: 30000000 Hours ahead of UTC : 1
timestamp: 40000000 Hours ahead of UTC : -23
timestamp: 50000000 Hours ahead of UTC : 1
timestamp: 60000000 Hours ahead of UTC : 0
timestamp: 70000000 Hours ahead of UTC : 1
timestamp: 80000000 Hours ahead of UTC : 1
timestamp: 90000000 Hours ahead of UTC : 0
timestamp: 100000000 Hours ahead of UTC : 0
timestamp: 110000000 Hours ahead of UTC : 1
timestamp: 120000000 Hours ahead of UTC : 1
timestamp: 130000000 Hours ahead of UTC : 0
timestamp: 140000000 Hours ahead of UTC : 1
timestamp: 150000000 Hours ahead of UTC : 1
timestamp: 160000000 Hours ahead of UTC : 0
timestamp: 170000000 Hours ahead of UTC : 1
timestamp: 180000000 Hours ahead of UTC : 1
timestamp: 190000000 Hours ahead of UTC : 0
timestamp: 200000000 Hours ahead of UTC : 1
timestamp: 210000000 Hours ahead of UTC : 1
timestamp: 220000000 Hours ahead of UTC : 0
timestamp: 230000000 Hours ahead of UTC : 1
timestamp: 240000000 Hours ahead of UTC : 1
timestamp: 250000000 Hours ahead of UTC : 0
timestamp: 260000000 Hours ahead of UTC : 1
timestamp: 270000000 Hours ahead of UTC : 1
timestamp: 280000000 Hours ahead of UTC : 0
timestamp: 290000000 Hours ahead of UTC : 0
英国实行夏令时,在夏季改用英国夏令时。因此,如果您传入一个属于英国夏令时生效年份的时间戳,您将看到您的当地时间比 UTC 早一小时。
以上是大佬教程为你收集整理的为什么 std::localtime 会给出与 UTC 不同的偏移量?全部内容,希望文章能够帮你解决为什么 std::localtime 会给出与 UTC 不同的偏移量?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。