Open SESjo opened 1 year ago
I have run into this issue too and I came here to ask the same question. Due to the edge case of dropping the HH:MM:SS
component for midnight timestamps the ymd_hms
function fails to be idempotent, i.e. ymd_hms(ymd_hms(x)) != ymd_hms(x)
for all $x$. This would be a nice property to have and holds at all non-midnight times.
However, I have tracked down the source of this issue and it turns out it comes from a call to the base R function .POSIXct
here:
https://github.com/tidyverse/lubridate/blob/11d1c9aeefeaa568edcefda09bc477116ffd300e/R/parse.r#L745
For example:
> x <- 1640995200
> base::.POSIXct(x,tz="UTC")
[1] "2022-01-01 UTC"
> base::.POSIXct(x+1,tz="UTC")
[1] "2022-01-01 00:00:01 UTC"
I think this demonstrates that lubridate is being consistent with base R. It is just a property of how the .POSIXct
class behaves and thus this issue is upstream of lubridate, although I personally wish it behaved like ymd_hms("2022-01-01 00:00:00 UTC") = "2022-01-01 00:00:00"
.
I just wanted to ping this issue to say that this problem persists (perhaps unsurprisingly since it it an issue with base R's treatment of this class). It may be possible to implement a solution similar to what @SESjo provides in the functions that contain time stamps (ymd_hms, ymd_hm, ... ). I am happy to give this a go
lubridate::ymd_hms(format(as.POSIXct(x), format = "%Y-%m-%d %T %Z"))
Thank you all for your hard work
Dear maintainers,
I'm not sure it's a proper issue considering it happens when parsing from a Posicx (I know it's not what the function is for), but the way the
ymd_hms
function handles the00:00:00
time seems inconsistent (?)The workaround I found is to convert the Posicx into a character, while being explicit about the time:
SeesionInfo