[Rd] Conversion and rounding of POSIXct

Stavros Macrakis macrakis at alum.mit.edu
Sun Mar 15 17:28:06 CET 2009


POSIXct/lt supports fractional seconds (see Sub-second Accuracy
section of man page), but there seem to be some inconsistencies in
their handling.

Converting to POSIXlt and back does not give back the same time for
times before the origin:

> t0 <- as.POSIXct('1934-01-05 23:59:59.00001')
> t0
[1] "1934-01-06 00:00:00 EST"      # rounding issue, see below
> as.POSIXlt(t0)
[1] "1934-01-06 00:00:00 EST"
> as.POSIXct(as.POSIXlt(t0))
[1] "1934-01-06 00:00:01 EST"   # ???
> as.POSIXct(as.POSIXlt(t0)) - t0
Time difference of 1 secs

Also, POSIXct always rounds up when printing for times before the origin:

> as.POSIXct('1934-01-05 10:10:23')
[1] "1934-01-05 10:10:23 EST"
> as.POSIXct('1934-01-05 10:10:23.00001')
[1] "1934-01-05 10:10:24 EST"

and always rounds down when printing times after the origin:

as.POSIXct('2010-01-05 23:59:59.4')
[1] "2010-01-05 23:59:59 EST"
> as.POSIXct('2010-01-05 23:59:59.6')
[1] "2010-01-05 23:59:59 EST"
> as.POSIXct('2010-01-05 23:59:59.999')
[1] "2010-01-05 23:59:59 EST"

But the Description section says that POSIXct "represent[s] calendar
dates and times (to the nearest second)".  "Nearest" would seem to
imply printing rounding-to-nearest, not rounding-up or rounding-down.



More information about the R-devel mailing list