【问题标题】:R why does difference between POSIXlt has a decimal part?R为什么POSIXlt之间的差异有小数部分?
【发布时间】:2020-08-21 10:31:22
【问题描述】:

为什么 POSIXlt 之间的差异有小数部分而 Date 之间的差异没有?

例子:

as.POSIXlt("2015-12-10",format = "%Y-%m-%d")-as.POSIXlt("2015-10-07",format = "%Y-%m-%d")

返回64.04167

as.Date("2015-12-10",format = "%Y-%m-%d")-as.Date("2015-10-07",format = "%Y-%m-%d")

返回64(正确值)

为什么会有不同的行为?

【问题讨论】:

    标签: r date posixlt


    【解决方案1】:

    这些小数正好是一小时,发生在从 DST 转换期间。

    0.04167 * 24
    #[1] 1.00008
    #some rounding error
    

    指定一个没有 DST 的时区,你会得到这个:

    as.POSIXlt("2015-12-10",format = "%Y-%m-%d", tz = "GMT")-
      as.POSIXlt("2015-10-07",format = "%Y-%m-%d", tz = "GMT")
    #Time difference of 64 days
    

    【讨论】:

    • 向其他读者澄清:DST = 夏令时。如果您选择两个日期都带有 dst(3 月至 10 月)或没有 dst(11 月至 2 月),则不会有小数部分。
    猜你喜欢
    • 2018-02-21
    • 1970-01-01
    • 1970-01-01
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多