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

Dirk Eddelbuettel edd at debian.org
Wed Jun 13 00:50:02 CEST 2007


On 12 June 2007 at 16:54, Earl F. Glynn wrote:
| 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"

I usually get good results with just '%OS' for fractional seconds.  First,
make sure you do display fractional seconds which I do via
'options("digits.secs"=7) in ~/.Rprofile:

	> getOption("digits.secs")			
	[1] 7

Then a simple example:

	> now <- Sys.time()
	> strptime(now, "%Y-%m-%d %H:%M:%OS")
	[1] "2007-06-12 17:44:16.779577"
	>
 
As per help("strptime"), what you tried should work

     Specific to R is '%OSn', which for output gives the seconds to '0
     <= n <= 6' decimal places (and if '%OS' is not followed by a
     digit, it uses the setting of 'getOption("digits.secs")', or if
     that is unset, 'n = 3'). Further, for 'strptime' '%OS' will input
     seconds including fractional seconds.

but doesn't for me either:

	> strptime(now, "%Y-%m-%d %H:%M:%OS6")
	[1] NA
	> strptime(now, "%Y-%m-%d %H:%M:%OS3")
	[1] NA
	> 

Looks like a bug.  But easy enough to circumvent if you just drop the 'n'.

Hth, Dirk

-- 
Hell, there are no rules here - we're trying to accomplish something. 
                                                  -- Thomas A. Edison



More information about the R-help mailing list