【问题标题】:Print current month as calendar using current system date C++使用当前系统日期 C++ 将当前月份打印为日历
【发布时间】:2015-06-15 22:06:36
【问题描述】:

第一次发帖,如有错误请见谅。

我对整个 C++ 编程相当陌生,但我想知道是否可以打印出某个月份(当前月份)的日历,例如今天是 2015 年 6 月,所以我想用 C++ 打印出 2015 年 6 月的月历

如果有人对如何使这成为可能有任何建议,那将非常有帮助。我知道如何使用用户输入发布当前月份,但我希望我的程序查看系统日期。

提前致谢。

【问题讨论】:

标签: c++ calendar timestamp


【解决方案1】:

使用 time(0) 得到一个 time_t,它也是自 xxxoopsxxxxx => epoch: Wed Dec 31 19:00:00 1969, so 1/1/1970 以来的秒数

time_t linearTime = time(0);

使用localtime_r将linearTime转换为

struct tm timeinfo = *localtime_r(&linearTime, &timeinfo);

使用 struct tm 解码故障时间(在 Linux 中,位于 /usr/include/time.h)并根据需要生成日历。

玩得开心。

编辑 2

调查了chrono。弄清楚如何衡量我们的持续时间。

但是,以下 chrono 的内容与之前的内容可疑地相似。

std::time_t t0 = std::time(nullptr);
std::tm* localtime(const std::time_t* t0); // std::tm has the fields you expect

// convenient to text
std::cout << std::asctime(std::localtime(&t0)); // prints the calendar info for 'now'

我怀疑并没有太大的不同。

编辑 3

我有疑问。我认为之前可疑的熟悉位来自 ctime,而不是 chrono。所以我打算在时间上再挖掘一些。

【讨论】:

  • 嗯,这不是真正的标准 c++ ...我宁愿坚持 std::chrono
  • 同意..我还没有学习std::chrono。我小心翼翼地不为他做这项工作……这将是 OP 学习这两种技术的好习惯。希望我也能抽出时间;)
  • 非常感谢。我确实不是在寻找完整的答案。谢谢你们提供的答案。我会调查的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多