为什么 std::localtime 给出与 UTC 不同的偏移量?
Why does std::localtime give varying offset from UTC?
我在英国,所以我的时区目前与 UTC 相同。在下面的代码中,如果我将不同的时间戳传递给本地时间,我将获得与 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 早一个小时。
我在英国,所以我的时区目前与 UTC 相同。在下面的代码中,如果我将不同的时间戳传递给本地时间,我将获得与 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 早一个小时。