[Rd] strptime bug (PR#1155)

ripley@stats.ox.ac.uk ripley@stats.ox.ac.uk
Wed, 31 Oct 2001 10:50:58 +0100 (MET)


On Wed, 31 Oct 2001 rmh@surfer.sbm.temple.edu wrote:

> ## rw1031  Version 1.3.1  (2001-08-31)
>
> ## > version
> ##          _
> ## platform i386-pc-mingw32
> ## arch     x86
> ## os       Win32
> ## system   x86, Win32
> ## status
> ## major    1
> ## minor    3.1
> ## year     2001
> ## month    08
> ## day      31
> ## language R
>
> ## The beav1 example in  *help[R](plot.POSIXct)* shows two bugs.
> ## The first is a fundamental error in strptime.

In your OS's implementation of strptime, not the R code ....

> ##
> ## The second is two details in the construction of the example.
> ##
> ## strptime is not using the "%j" format correctly.  This has the effect
> ## of wrapping the time in the beav1 example.  Here is a simplified
> ## example in which we see that the day wraps.
>
>      library(MASS)
>      data(beav1)
>      tmp <- beav1[90:93,]
>      tmp
>      attach(tmp)
>      paste(day, time %/% 100, time %% 100)
>      strptime(paste(day, time %/% 100, time %% 100),
>               "%j %H %M")
>
> ## note that the times wrap and the times on both days 346 and 347 are
> ## mapped to "1900-01-01"

Well, I don't get either of those:

[1] "2001-12-12 23:40:00" "2001-12-12 23:50:00" "2001-12-13 00:00:00"
[4] "2001-12-13 00:10:00"

It would be helpful to give the incorrect output that your system gives.
In the output below I see no evidence of `the times wrap'.  Using the
wrong day seems to be a bug in glibc's strptime, but you need to unclass
the strptime result to see this.  (I do see the strptime bug on RH6.2
Linux, where the day field is ignored.)


> ## Including the year in the format gets the right answer, with two
> ## different days appearing in the result.

Yes, but that was not the intention in the original (real) example,
where the year was not specified.


>      strptime(paste(1990, day, time %/% 100, time %% 100),
>               "%Y %j %H %M")
>
>      detach("tmp")
>
> ## >      tmp <- beav1[90:93,]
> ## >      tmp
> ##    day time  temp activ
> ## 90 346 2340 36.93     0
> ## 91 346 2350 36.83     0
> ## 92 347    0 36.93     0
> ## 93 347   10 36.83     0
> ## >      attach(tmp)
> ## >      paste(day, time %/% 100, time %% 100)
> ## [1] "346 23 40" "346 23 50" "347 0 0"   "347 0 10"
> ## >      strptime(paste(day, time %/% 100, time %% 100),
> ## +               "%j %H %M")
> ## [1] "1900-01-01 23:40:00" "1900-01-01 23:50:00" "1900-01-01 00:00:00"
> ## [4] "1900-01-01 00:10:00"
> ## >      strptime(paste(1990, day, time %/% 100, time %% 100),
> ## +               "%Y %j %H %M")
> ## [1] "1990-12-12 23:40:00" "1990-12-12 23:50:00" "1990-12-13 00:00:00"
> ## [4] "1990-12-13 00:10:00"
> ## >      detach("tmp")
>
>
> ## I think this would be better for the example
> ## I am making two changes.
>
> ## 1. Add the year 1990

(You used 1900 below!)
>
> ## 2. use temporary variable name instead of "time".  "time" persists
> ## after detaching "beav1", hence it had precedence when I reattached
> ## beav1 and the arithmetic using time generated an error.
>
>      attach(beav1)
>      tmp.time <- strptime(paste(1900, day, time %/% 100, time %% 100),
>                       "%Y %j %H %M")
>      plot(tmp.time, temp, type="l") # axis at 4-hour intervals.
>      detach("beav1")

Again, the need was to use `time' after the detach.  This only
occurred because you did not remove the variable your system created
incorrectly.

I will change this to work around a system-specific bug.  I'll pass on to
the Windows maintainer fixing the strptime code used on that platform.

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._