Frage

I discovered a rather unexpected behavior of lubridate. The following R code returns NAs instead of '2010-10-17 08:00:00':

library(lubridate);
as.POSIXct("2010-10-17 07:59:01") + seconds(59);
as.POSIXct("2010-10-17 07:59:30") + seconds(30);

Whereas the following snippet returns the expected value ('2010-10-17 07:59:00'):

as.POSIXct("2010-10-17 07:58:01") + seconds(59);
as.POSIXct("2010-10-17 07:58:30") + seconds(30);

Is this a bug in lubridate or do I simply have to RTFM more thoroughly?! :)

UPDATE: Used software: Gnu R v 3.0.2, lubridate 1.3.3

UPDATE 2: Using dseconds instead of seconds solves this issue.

Correct result is calculated by:

as.POSIXct("2010-10-17 07:58:01") + dseconds(59);
as.POSIXct("2010-10-17 07:58:30") + dseconds(30);
War es hilfreich?

Lösung

Final Edit: known bug: https://github.com/hadley/lubridate/issues/188

I found the bug, as it were: Apparently the POSIXct-class object will allow you to modify its seconds portion, but there's no "carry" function, so if you force the total seconds to exceed 59, it gives up.

Rgames> as.POSIXct("2010-10-17 07:59:30")->dfoo
Rgames> dfoo
[1] "2010-10-17 07:59:30 EDT"

Rgames> second(dfoo)
[1] 30
Rgames> second(dfoo)<-second(dfoo)+10
Rgames> dfoo
[1] "2010-10-17 07:59:40 EDT"
Rgames> second(dfoo)<-second(dfoo)+seconds(10)
Rgames> dfoo
[1] "2010-10-17 07:59:50 EDT"
Rgames> second(dfoo)<-second(dfoo)+seconds(10)
Rgames> dfoo
[1] NA

What the intended use of the lubridate::seconds and second functions are, I dunno, but clearly this ain't the way to go :-( .

EDIT: I kept playing, with dfoo as above and dbar <- as.POSIXct("2010-10-17 07:59:30",tz='GMT') . I could add arbitrary seconds to dbar w/o problem. Further, I used with_tz to switch the time zones for both dfoo and dbar, and regardless of the zone assigned, could successfully add arbitrary seconds. This leads me to wonder just what the [redacted] as.POSIXct does with the default tz='' argument!

EDIT 2: hey, look:

Rgames> as.POSIXct("2010-10-17 07:59:30")->dfoo
Rgames> dfoo + seconds(55)
[1] "2010-10-17 08:00:25 EDT"
Rgames> dfoo + seconds(30)
[1] NA
Rgames> dfoo + seconds(31)
[1] "2010-10-17 08:00:01 EDT"
Rgames> dfoo + seconds(29)
[1] "2010-10-17 07:59:59 EDT"
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top