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

Brian Diggs diggsb at ohsu.edu
Wed Feb 3 16:48:57 CET 2010


On 2/1/2010 3:57 PM, Brian Diggs wrote:
> 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?

I realized I forgot my session info:

> sessionInfo()
R version 2.10.1 (2009-12-14) 
i386-pc-mingw32 

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     


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



More information about the R-devel mailing list