Search code examples
rdateposixlt

R why does difference between POSIXlt has a decimal part?


Why does difference between POSIXlt has a decimal part whereas difference between Date hasn't?

Example:

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

returns 64.04167

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

returns 64 (correct value)

Why the different behavior?


Solution

  • These decimals amount to exactly one hour, which occurred during the switch from DST.

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

    Specify a time zone without DST and you get this:

    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