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

Don MacQueen macq at llnl.gov
Mon Apr 8 23:13:00 CEST 2002

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 

>  Sys.getenv('TZ')
>  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
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 
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