【问题标题】:unix timestamp: not getting the correct year value by dividingunix时间戳:通过除法没有得到正确的年份值
【发布时间】:2017-12-14 18:57:48
【问题描述】:

我正在尝试从时间戳中提取年份,如下所示:

const int timestamp = 1499477613;
int hours = timestamp/3600;
int days = hours/24;
int years = days/356;
printf("years=%i\n", years);

但是在执行程序后,我得到一个输出:

年=48

这与预期值2017不同。

代码有什么问题?

【问题讨论】:

    标签: date time timestamp


    【解决方案1】:

    https://en.wikipedia.org/wiki/Unix_time

    Unix 时间(也称为 POSIX 时间或纪元时间)是一个系统 描述时间的瞬间,定义为秒数 自协调世界时 (UTC) 00:00:00 起已过去, 1970 年 1 月 1 日,星期四。

    该结果告诉您,从那时起(大约)已经过去了 48 年。 1970 + 48 = 2018.

    您没有得到准确值的原因是您在每次计算中丢失了精度。您正在使用整数对可能返回浮点的内容进行编码。 (我认为是错字:days/356 应该是 days/365

    【讨论】:

    • 如果考虑闰年,大约有 365.2422 天。
    猜你喜欢
    • 2013-01-28
    • 2016-03-19
    • 1970-01-01
    • 2010-12-15
    • 2015-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多