在给定日、月和年的情况下,返回一周中某一天的算法是什么?
最佳答案
这可以使用 std::mktime
来完成和 std::localtime
功能。这些函数不仅仅是 POSIX,它们是 C++ 标准(C++03 §20.5)强制要求的。
#include <ctime>
std::tm time_in = { 0, 0, 0, // second, minute, hour
4, 9, 1984 - 1900 }; // 1-based day, 0-based month, year since 1900
std::time_t time_temp = std::mktime( & time_in );
// the return value from localtime is a static global - do not call
// this function from more than one thread!
std::tm const *time_out = std::localtime( & time_temp );
std::cout << "I was born on (Sunday = 0) D.O.W. " << time_out->tm_wday << '\n';
关于c++ - 日期到星期几算法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5797814/