[R] Problem(?) in strptime() -- short version -- followup

Don MacQueen macq at llnl.gov
Mon Apr 15 16:49:30 CEST 2002


Sorry to take so long with the followup; I needed to work with this 
idea for a little while.

ggrothendieck at yifan.net made this suggestion:
>library(chron)
>dts <- dates(c("04/07/02","04/07/02"))
>tms <- times(c("01:30:00","02:30:00"))
>x <- chron(dts,tms)
>y <- as.POSIXct(x,tz="GMT")
>y   # returns date/times
>y[2]-y[1]   # returns a difference of 1 hour


What I found was that (1) it works, (2) it's slower [I sometimes have 
vectors of ~250,000 dates], and (3) it introduces a small rounding 
error by converting from seconds to partial days and then converting 
back to seconds, with the result that sometimes the POSIXct value is 
displayed 1 second off from the value that was supplied.

I solved (3) by using the chron() approach on only the date portion, 
converting it to POSIXct, and then adding the times in seconds to 
these. This also improved the speed. So, thank you, ggrothendieck.

-Don

At 2:13 PM -0700 4/8/02, Don MacQueen wrote:
>I decided my earlier email on this topic was rather long and wordy; 
>here's a condensed version.
>
>I am sitting at a Solaris computer in the US/Pacific timezone.
>I have a file of data having times that includes the following three values
>
>   2002-4-7 1:30:00 GMT
>   2002-4-7 2:30:00 GMT
>   2002-4-7 3:30:00 GMT
>
>I have not been able to find a way to correctly convert these to 
>either of the POSIX datetime classes with the OS timezone set to 
>US/Pacific. And I've tried everything I could think of. The bottom 
>line appears to be the fact that strptime() always uses the local 
>timezone.
>
>>  Sys.getenv('TZ')
>           TZ
>"US/Pacific"
>>
>>  gdat <- c('2002-4-7 1:30:00 GMT',
>+           '2002-4-7 2:30:00 GMT',
>+           '2002-4-7 3:30:00 GMT')
>>
>>
>>  as.POSIXct(gdat)
>[1] "2002-04-07 01:30:00 PST" "2002-04-07 01:30:00 PST" "2002-04-07 
>03:30:00 PDT"
>>
>>  as.POSIXct(gdat,tz='GMT')
>[1] "2002-04-06 17:30:00 PST" "2002-04-06 17:30:00 PST" "2002-04-06 
>19:30:00 PST"
>>
>>  strptime(gdat,'%Y-%m-%d %H:%M:%S')
>[1] "2002-04-07 01:30:00" "2002-04-07 01:30:00" "2002-04-07 03:30:00"
>>
>>  strptime(gdat,'%Y-%m-%d %H:%M:%S %Z')
>[1] "NA" "NA" "NA"
>
>The middle element is converted/interpreted incorrectly.
>
>>  version
>          _
>platform sparc-sun-solaris2.7
>arch     sparc
>os       solaris2.7
>system   sparc, solaris2.7
>status
>major    1
>minor    4.1
>year     2002
>month    01
>day      30
>language R
>>
>>  Sys.getlocale()
>[1] "C"
>
>If I setenv TZ GMT before starting R, then the data is converted 
>correctly. However, this is not entirely satisfactory, because 
>ultimately I want to work with the data in my local timezone (for 
>example, make graphs where the time axis is in local time), and that 
>means going through a multiple step process:
>
>   1) setenv TZ GMT
>   2) start R, read the data
>   3) quit R
>   4) setenv TZ US/Pacific
>   5) start R, work with the data
>
>strptime() appears to rely on the operating system's strptime, so 
>perhaps the problem is out of R's hands, so to speak. But it does 
>seem reasonable that I should be able to convert such data no matter 
>where I am. For example, it is my understanding that a world-wide 
>standard among meteorologists is that times are always recorded in 
>GMT.
>--
>--------------------------------------
>Don MacQueen
>Environmental Protection Department
>Lawrence Livermore National Laboratory
>Livermore, CA, USA
>--------------------------------------


-- 
--------------------------------------
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA
--------------------------------------
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help 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-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list