【问题标题】:C++: How do you a print a date a specific amount of time away from the current time?C ++:如何在距当前时间特定时间后打印日期?
【发布时间】:2015-02-01 17:58:42
【问题描述】:

我正在尝试使用ctime,但我一生都无法弄清楚如何打印距当前日期 X 天的日期。到目前为止,这是我的程序:

#include "header.h"

int main()
{
    time_t current;
    struct tm * timedata;

    time(&current);
    timedata = localtime(&current);

    cout << "The current date and time is: " << asctime(timedata) << endl << endl;

    return 0;
}

如果我是正确的,time(&amp;current) 会返回从 2000 年 1 月 1 日开始的秒数,但我得到的数字似乎太小了,不能这样。感谢所有帮助。

【问题讨论】:

  • 那么你得到了什么数字?你在哪里添加Xdays?

标签: c++ time ctime


【解决方案1】:

time(&current) 返回自 1970 年 1 月 1 日,UTC -0000 以来的秒数。

添加 n*86400,获取当前时间前后 n 天的时间。

当然,假设您不会跨过夏季/冬季时间调整,如果您碰巧生活在遭受这种奇怪概念困扰的世界部分...

【讨论】:

  • “假设”是没有意义的。您必须严格定义“n 天”对于任何给定项目的含义。
  • 是的,那是我的失败。在我发布之后,我就知道了“Unix epoch”...... derp。不幸的是,我来自世界的那个地方。 :(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-13
  • 1970-01-01
  • 1970-01-01
  • 2017-11-02
  • 1970-01-01
相关资源
最近更新 更多