[Rd] Error with cut.POSIXt and daylight savings time switchover dates (patch included)
Brian Diggs
diggsb at ohsu.edu
Thu Feb 4 22:39:43 CET 2010
On 2/3/2010 7:48 AM, Brian Diggs wrote:
> 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
[details deleted...]
>> 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
>
A post by Felix Andrews in a different thread pointed out the trunc.POSIXt function. I checked, and it properly handles the daylight savings time issue:
tm <- as.POSIXlt("2009-11-01 04:00:00", tz="America/Los_Angeles")
tm <- trunc(tm, "days")
as.POSIXct(tm)
# [1] "2009-11-01 PDT"
Looking at how trunc.POSIXt does this, I created a patch (included below) for cut.POSIXt so that it won't break either. I've not extensively tested this patch (I don't have the R build environment for Windows installed on my machine), but being analogous to existing base code, it should work generally.
Index: datetime.R
===================================================================
--- datetime.R (revision 51101)
+++ datetime.R (working copy)
@@ -729,7 +729,7 @@
incr <- 1
if(valid > 1L) { start$sec <- 0L; incr <- 59.99 }
if(valid > 2L) { start$min <- 0L; incr <- 3600 - 1 }
- if(valid > 3L) { start$hour <- 0L; incr <- 86400 - 1 }
+ if(valid > 3L) { start$hour <- 0L; start$isdst <- -1L; incr <- 86400 - 1 }
if(valid == 5L) { # weeks
start$mday <- start$mday - start$wday
if(start.on.monday)
More information about the R-devel
mailing list