1

I was using the difftime function from the base package in R and based on my data I found a couple of weird return values of this function:

> difftime("2014-10-29", "2014-10-21", units = "days")
Time difference of 8.041667 days
> difftime("2020-4-04", "2020-3-28", units = "days")
Time difference of 6.958333 days

Any idea why those values are not integers? Thanks!

All I see in the doc, relevant to it is: "Note that units = "days" means a period of 24 hours, hence takes no account of Daylight Savings Time. Differences in objects of class "Date" are computed as if in the UTC time zone."

Marius
  • 990
  • 1
  • 14
  • 34

2 Answers2

3

I think you should use as.Date to wrap your date strings, e.g.,

> difftime(as.Date("2014-10-29"), as.Date("2014-10-21"), units = "days")
Time difference of 8 days

> difftime(as.Date("2020-4-04"), as.Date("2020-3-28"), units = "days")
Time difference of 7 days

You can observe the difference with or without as.Date

> (a1 <- as.POSIXct("2014-10-29"))
[1] "2014-10-29 CET"

> (a2 <- as.POSIXct("2014-10-21"))
[1] "2014-10-21 CEST"

> (b1 <- as.POSIXct(as.Date("2014-10-29")))
[1] "2014-10-29 01:00:00 CET"

> (b2 <- as.POSIXct(as.Date("2014-10-21")))
[1] "2014-10-21 02:00:00 CEST"

> c(a1, b1)
[1] "2014-10-29 00:00:00 CET" "2014-10-29 01:00:00 CET"

> c(a2, b2)
[1] "2014-10-21 00:00:00 CEST" "2014-10-21 02:00:00 CEST"
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
  • I see. So it was +/- 1h depending on the OS (as @Antti) also mentioned. Thank you for illustrating it as well. – Marius Nov 01 '22 at 12:27
1

The difftime-function uses as.POSIXct() not as.Date() to convert strings to dates, and this includes the system-specific time-zone (if not otherwise provided). Those pairs of dates contain the change to and from summertime in many time-zones, which may be why the time interval is not an integer.

Antti
  • 91
  • 3
  • Thanks for answering. I chose the other answer as it shows also a concrete example of the behavior. +1 though – Marius Nov 01 '22 at 12:28