在this answer我提议 marihikari使用 mktime
的标准功能而不是尝试实现他自己的公历系统。
我编写这个函数是为了演示如何使用 mktime
来完成此任务:
bool leap_year(int year) {
tm bar = { 0, 0, 0, 29, 1, year - 1900 };
mktime(&bar);
return bar.tm_mday == 29 && bar.tm_mon == 1 && bar.tm_year == year - 1900;
}
测试这个:
cout << "2000: " << leap_year(2000) << "\n2001: " << leap_year(2001) << "\n2004: " << leap_year(2004) << "\n1900: " << leap_year(1900) << "\n2100: " << leap_year(2100) << endl;
在 Clang 3.7.0 中产生了正确的结果:
2000: 1
2001: 0
2004: 1
1900: 0
2100: 0
但是 gcc 5.1.0 中的结果不正确:
2000: 1
2001: 0
2004: 1
1900: 1
2100: 1
Visual Studio 2015 中的结果不正确:
2000: 1
2001: 0
2004: 1
1900: 1
2100: 0
我认为这是 gcc 5.1.0 和 Visual Studio 2015 中的错误?
最佳答案
mktime
:
Converts local calendar time to a time since epoch as a
time_t
object.time->tm_wday
andtime->tm_yday
are ignored. The values in time are permitted to be outside their normal ranges.
...
If the conversion is successful, the time object is modified. All fields of time are updated to fit their proper ranges.
mktime
将返回:
Time since epoch as a
time_t
object on success or -1 if time cannot be represented as atime_t
object.
但未指定实现必须做出哪些努力才能转换 tm
.所以只要转换时间或者static_cast<time_t>(-1)
返回 mktime
的要求已满。
意思是下面的函数will work on all platforms正确支持 mktime
:
bool leap_year(int year) {
tm bar = { 0, 0, 0, 29, 1, year - 1900 };
return static_cast<time_t>(-1) != mktime(&bar) && bar.tm_mday == 29 && bar.tm_mon == 1 && bar.tm_year == year - 1900;
}
关于c++ - mktime 仅处理 Clang 上的闰年?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33477379/