From macrakis at alum.mit.edu  Sun Mar 15 17:28:06 2009
From: macrakis at alum.mit.edu (Stavros Macrakis)
Date: Sun, 15 Mar 2009 12:28:06 -0400
Subject: [Rd] Conversion and rounding of POSIXct
Message-ID: <8b356f880903150928j15ef1d24tba7ae84e409deafa@mail.gmail.com>

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.



[Rd] Conversion and rounding of POSIXct
Stavros Macrakis