[Rd] Error with cut.POSIXt and daylight savings time switchover dates

Brian Diggs diggsb at ohsu.edu
Tue Feb 2 00:57:30 CET 2010


The following code:

cut(as.POSIXct("2009-11-01 04:00:00", tz="America/Los_Angeles"), "1 day")

gives the error:

Error in seq.int(0, to - from, by) : 'to' must be finite

This is related to November 1st, 2009 being the switchover date from daylight savings time to standard time in the America/Los_Angeles time zone.  In particular, in cut.POSIXt, the starting time (start) is converted to a POSIXlt, and the individual members are manipulated.  Because a spacing of "1 day" is requested, the hour, minute, and second are manually set to 0.  In doing so, the represented time is now before the 2:00am PDT->PST change.  This value is passed to seq.int (as the argument from), which dispatches to seq.POSIXt.  seq.POSIXt eventually does from <- unclass(as.POSIXct(from)) which evaluates to NA because as.POSIXct(from) is NA.  The seq.int call in the next line then passes NA as the "to" argument, causing the output error (which comes from the C-code of seq.int).

Bringing it all together, the sequence of commands that causes the problems is:

tm <- as.POSIXlt("2009-11-01 04:00:00", tz="America/Los_Angeles")
tm$hour <- 0
as.POSIXct(tm)
# [1] NA

Is there a timezone/daylight savings time safe way to get to the beginning of the day in cut.POSIXt so that invalid dates are not sent to the other functions?  Alternatively, can as.POSIXct.POSIXlt be made to handle these manually manipulated dates correctly?

--
Brian Diggs, Ph.D.
Senior Research Associate, Department of Surgery, Oregon Health & Science University




More information about the R-devel mailing list