c++ - 日期到星期几算法?

标签 c++ algorithm

在给定日、月和年的情况下,返回一周中某一天的算法是什么?

最佳答案

这可以使用 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/

相关文章:

c++ - 如何在 Visual Assist X 中格式化代码

c++ - C++11 类型推断如何决定 float 或 double ?

ios - 有效地在 Objective C iOS 中解析字符串

r - 如何从邻接表中找到循环路径

生成 n 个项目的 k 元组的算法,每个项目至少使用一次 (k>n)

c++ - 如何修复隐式转换警告?

c++ - 如何高效读取 COleSafeArray

c++ - 函数错误的参数太多,即使我有一个参数数量正确的函数

algorithm - 困难的算法 : Optimal Solution to 1

python - DFS 中(非常慢的)深度复制的任何替代方法?