gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 23:12:30 25 4
gpt4 key购买 nike

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

最佳答案

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com