[Rd] strptime bug (PR#1155)

rmh@surfer.sbm.temple.edu rmh@surfer.sbm.temple.edu
Wed, 31 Oct 2001 07:07:29 +0100 (MET)


## 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.
## 
## 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"

## Including the year in the format gets the right answer, with two
## different days appearing in the result.
     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

## 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")

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._