I have a problem with results from difftime being 1 hour different than expected. 2 examples are given below:
datetime <- matrix(data=rbind(c("2012-03-31 21:00:00", "2012-04-01 00:00:00", "2012-04-01 03:00:00", "2012-04-01 06:00:00"),
c("2012-10-06 21:00:00", "2012-10-07 00:00:00", "2012-10-07 03:00:00", "2012-10-07 06:00:00")), ncol=4, nrow=2)
for (row in 1:2) {
x <- datetime[row,]
x1=x[1:length(x)-1]
x2=x[2:length(x)]
xd=difftime(x2,x1)
print (x)
print (xd)
}
[1] "2012-03-31 21:00:00" "2012-04-01 00:00:00" "2012-04-01 03:00:00" "2012-04-01 06:00:00"
Time differences in hours
[1] 3 4 3
attr(,"tzone")
[1] ""
[1] "2012-10-06 21:00:00" "2012-10-07 00:00:00" "2012-10-07 03:00:00" "2012-10-07 06:00:00"
Time differences in hours
[1] 3 2 3
attr(,"tzone")
[1] ""
Accurate results would be 3 3 3 (hours) in both cases.
The problem occurs randomly, but only around midnight.
All the dates between "2012-04-01 06:00:00" and "2012-10-06 21:00:00" are fine.
I have seen postings about problems with "POSIXlt" and "POSIXct" accuracy
because of truncation rather than rounding of floating point representations.
The response was that it was expected behaviour.
So my question is this expected behaviour, have I used difftime incorrectly, and if not, is there a set of date/time functions and classes that use
integer representation to give more accurate results? I am new to R and I may well be using the wrong set of functions.
Garry
[[alternative HTML version deleted]]