【发布时间】:2020-06-12 16:44:32
【问题描述】:
我正在尝试在今年年初获取 EPOCH 时间(没有地区差异的通用时间)。以下是我写的:
time_t getEpochTimeAtSOY(){
/* Get the current year */
time_t currTime = time(NULL);
struct tm *aTime = localtime(&currTime);
int currentYear = aTime->tm_year; // This is (actualCurrentYear - 1900), so 2020 - 1900 = 120
/* Now find out EPOCH time for start of currentYear */
struct tm t;
time_t timeSinceEpoch = 0;
memset (&t, 0, sizeof(t)); // Initalize to all 0's
t.tm_year = currentYear;
t.tm_mday = 1;
timeSinceEpoch = timegm(&t); // EPOCH time of 1st Jan <currentYear> 00:00
return (timeSinceEpoch);
}
这会在 x86 或 x86_64 上返回正确的结果(即 1577836800),但目标板(交叉编译器)不支持 timegm() 函数。如果我使用 mktime() 代替 timegm(),则会导致区域时差。有人可以建议一个替代的便携式解决方案吗?也许使用 std::chrono 或其他东西?
【问题讨论】: