[R] Can strptime handle milliseconds or AM/PM?

Earl F. Glynn efg at stowers-institute.org
Tue Jun 12 23:54:49 CEST 2007


I'm trying to proess date/time fields from files that were given to me to 
analyze.

Any clues what I'm doing wrong with strptime?   This seems to fail the same 
way under Linux or Windows.

 For ?strptime would it make sense to explain %OS3 somewhere besides the 
Examples?

> # Why does %OS3 work here?
> format(Sys.time(), "%H:%M:%S")
[1] "16:45:19"
> format(Sys.time(), "%H:%M:%OS3")
[1] "16:45:19.477"

> # Why doesn't %OS3 work here?
> EventLog.Start <- "17:49:33.779"
> strptime(EventLog.Start, "%H:%M:%S")
[1] "2007-06-12 17:49:33"
> strptime(EventLog.Start, "%H:%M:%OS3")
[1] NA


> # This works OK without milliseconds or AM/PM
> x <- c("5:49:33 6/9/2007", "5:49:36 6/9/2007", "5:49:37 6/9/2007")
> strptime(x, "%I:%M:%S")   # unclear why this inserts today's date?
[1] "2007-06-12 05:49:33" "2007-06-12 05:49:36" "2007-06-12 05:49:37"
> strptime(x, "%I:%M:%S %m/%d/%Y")
[1] "2007-06-09 05:49:33" "2007-06-09 05:49:36" "2007-06-09 05:49:37"


> # How to handle milliseconds and AM/PM?  Why doesn't this work?
> y <- c("5:49:33.795 PM 6/9/2007", "5:49:36.184 PM 6/9/2007", "5:49:37.808 
> PM 6/9/2007")
> strptime(y, "%I:%M:%S")               # works except for milliseconds but 
> wrong date
[1] "2007-06-12 05:49:33" "2007-06-12 05:49:36" "2007-06-12 05:49:37"
> strptime(y, "%I:%M:%OS3")             # doesn't work
[1] NA NA NA
> strptime(y, "%I:%M:%S %p %m/%d/%Y")   # AM/PM doesn't work
[1] NA NA NA
> strptime(y, "%I:%M:%S %p %m/%d/%Y")   # AM/PM with dates doesn't work
[1] NA NA NA
> strptime("5:49:33 PM 6/9/2007", "%I:%M:%S %p")  # doesn't get date right
[1] "2007-06-12 17:49:33"

### Windows ###

> Sys.getlocale(category = "LC_TIME")
[1] "English_United States.1252"
> R.version
               _
platform       i386-pc-mingw32
arch           i386
os             mingw32
system         i386, mingw32
status
major          2
minor          5.0
year           2007
month          04
day            23
svn rev        41293
language       R
version.string R version 2.5.0 (2007-04-23)


### Linux ###

> Sys.getlocale(category = "LC_TIME")
[1] "C"
> R.version
               _
platform       x86_64-unknown-linux-gnu
arch           x86_64
os             linux-gnu
system         x86_64, linux-gnu
status
major          2
minor          4.1
year           2006
month          12
day            18
svn rev        40228
language       R
version.string R version 2.4.1 (2006-12-18)


efg

Earl F. Glynn
Stowers Institute for Medical Research



More information about the R-help mailing list