From p.dalgaard at biostat.ku.dk  Mon Feb  8 22:20:33 2010
From: p.dalgaard at biostat.ku.dk (Peter Dalgaard)
Date: Mon, 08 Feb 2010 22:20:33 +0100
Subject: [Rd] bug in cut.POSIXt (with patch) (PR#14208)
In-Reply-To: <20100208204524.66F90282EF3D@mail.pubhealth.ku.dk>
References: <20100208204524.66F90282EF3D@mail.pubhealth.ku.dk>
Message-ID: <4B708021.6020706@biostat.ku.dk>



> I do not know why my bug report got so oddly encoded, I've reproduced
> it; hopefully it won't get mangled again.

It did, unfortunately. Not your fault, it's just that Jitterbug is 
incapable of undoing Content-Transfer-Encoding on incoming mail, so if 
your mail transfer agent sends in base64, it goes straight through to 
R-devel.

Never mind, the old bug repository will have to die rather soon now, 
hopefully to be replaced by something much better.

Un-base64'ed version follows below, possibly modulo linebreaks.

- Peter D.

------
Summary:

For certain well formed inputs, cut.POSIXt will give an error.

Example:

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

Example output:

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

Patch to fix error:

This patch is for src/library/base/R/datetime.R

Index: datetime.R
===================================================================
--- datetime.R	(revision 51110)
+++ 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)

Discussion:

The bug is triggered when the (earliest) date/time given to cut.POSIXt
falls on the day transitioning from daylight savings time to standard
time, but at a time after the switchover.  In the US, November 1, 2009 was
such a day.

In cut.POSIXt(), the elements of a POSIXlt representation of the date/time
are directly manipulated to set it to the start of the day (since a day
sized spacing was requested).  This puts it before the daylight savings
time to standard time switchover.  When as.POSIXct is later called on this
object, it returns NA which causes the returned error when the NA is passed
as an argument to seq.int().

The motivation for the patch is the trunc.POSIXt function in the same
file.  It correctly handles this case:

trunc(as.POSIXct("2009-11-01 04:00:00", tz="America/Los_Angeles"), "day")
# [1] "2009-11-01 America/Los_Angeles"
as.POSIXct(trunc(as.POSIXct("2009-11-01 04:00:00", 
tz="America/Los_Angeles"), "day"))
# [1] "2009-11-01 PDT"

In that function, when truncating to days, the isdst element of the POSIXlt
is set to -1L.  That is the modification to cut.POSIXt() that I made.

--please do not edit the information below--

Version:
  platform = i386-pc-mingw32
  arch = i386
  os = mingw32
  system = i386, mingw32
  status =
  major = 2
  minor = 10.1
  year = 2009
  month = 12
  day = 14
  svn rev = 50720
  language = R
  version.string = R version 2.10.1 (2009-12-14)

Windows XP (build 2600) Service Pack 3

Locale:
LC_COLLATE=English_United States.1252;LC_CTYPE=English_United 
States.1252;LC_MONETARY=English_United 
States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252

Search Path:
  .GlobalEnv, package:stats, package:graphics, package:grDevices, 
package:utils, package:datasets, package:methods, Autoloads, package:base

-- 
    O__  ---- Peter Dalgaard             ?ster Farimagsgade 5, Entr.B
   c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
  (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907



[Rd] bug in cut.POSIXt (with patch) (PR#14208)
Peter Dalgaard