星期几算法的日期?

jma*_*erx 12 c++ algorithm

什么算法,给定一天,一个月和一年,返回一周中的一天?

Pot*_*ter 23

这可以使用std::mktimestd::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';
Run Code Online (Sandbox Code Playgroud)

  • +1 upvoted.还投票重新开启这个问题.也许它是一个堆栈溢出重复,我不知道.但这是一个真正的C++问题(关闭并不是一个真正的问题),这是一个很好的答案. (3认同)