rposixct

How does the printing of POSIXct times in R work? How can I make the print method print the correct time?


Have a look at the following code:

options(digits.secs = 6)
options(digits = 22)
structure(1743070066.75, class = c("POSIXct", "POSIXt"))
structure(1743070066.76, class = c("POSIXct", "POSIXt"))

which prints out

[1] "2025-03-27 11:07:46.75 CET"
[1] "2025-03-27 11:07:46.75 CET"

You can see that the result is the same which is obviously wrong. How can I change this, so the correct result is printed?


Solution

  • This has been a known bug that quite a few people were involved, both on

    R's bugzilla https://bugs.r-project.org/show_bug.cgi?id=17350 and also

    at the "R dev Days" (mentioned at https://bugs.r-project.org/show_bug.cgi?id=17350#c9), thanks to @hturner, see https://github.com/r-devel/r-dev-day/issues/83

    It has been fixed last December (by me) in "R-devel" (the development version of R), and that has been released as R 4.5.0.

    Note that the truncation @r2evans mentions has been added on purpose to fix another bug (a long while ago): https://bugs.r-project.org/show_bug.cgi?id=14579. Also that it truncates rather than rounding is documented in ?strptime = ?format.POSIXlt. Yes, that is somewhat debatable, but AFAIR, part of the POSIX standard.

    Last but not least, in R >= 4.5.0, your examples give (for me, in Zurich time)

    > .POSIXct(1743070066.75)
    [1] "2025-03-27 11:07:46.75 CET"
    > .POSIXct(1743070066.76)
    [1] "2025-03-27 11:07:46.759999 CET"
    

    We (the R-core team) would posit that the current behavior is no longer buggy, the truncation being part of the documented design.