[R] %OSn in time formats: is it only valid for formatting, but invalid for parsing?

Brent yhbrent at yahoo.com
Wed Jun 10 06:14:47 CEST 2015


Consider this R code:
time = as.POSIXct(1433867059, origin = "1970-01-01")
print(time)
print( as.numeric(time) )

timeFormat = "%Y-%m-%d %H:%M:%OS3"
tz = "EST"

timestamp = format(time, format = timeFormat, tz = tz)
print(timestamp)

timeParsed = as.POSIXct(timestamp, format = timeFormat, tz = tz)
print(timeParsed)
print( as.numeric(timeParsed) )


If I paste that into Rgui on my Windows bos, which is running the latest (3.2.0) stable release, I get this:
> time = as.POSIXct(1433867059, origin = "1970-01-01")
> print(time)
[1] "2015-06-09 12:24:19 EDT"
> print( as.numeric(time) )
[1] 1433867059
> 
> timeFormat = "%Y-%m-%d %H:%M:%OS3"
> tz = "EST"
> 
> timestamp = format(time, format = timeFormat, tz = tz)
> print(timestamp)
[1] "2015-06-09 11:24:19.000"
> 
> timeParsed = as.POSIXct(timestamp, format = timeFormat, tz = tz)
> print(timeParsed)
[1] NA
> print( as.numeric(timeParsed) )
[1] NA

Notice how the time format, which ends with %OS3, produces the correct time stamp (a 3 digit millisecond resolution).

However, that same time format FAILS IN THE OPPOSITE DIRECTION: it cannot parse that time stamp back into the original POSIXct value; it barfs and parses NA.

Anyone know what is going on?

A web search found this link 
https://stackoverflow.com/questions/19062178/how-to-convert-specific-time-format-to-timestamp-in-r
where one of the commenters, Waldir Leoncio, in the first answer, appears to describe the same parsing bug with %OS3 that I do:
"use, for example, strptime(y, "%d.%m.%Y %H:%M:%OS3"), but it doesn't work for me. Henrik noted that the function's help page, ?strptime states that the %OS3 bit is OS-dependent. I'm using an updated Ubuntu 13.04 and using %OS3 yields NA."

The help page mentioned in the quote above likely is
https://stat.ethz.ch/R-manual/R-devel/library/base/html/strptime.html
which is unfortunately terse, merely saying
"Specific to R is %OSn, which for output gives the seconds truncated 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. Note that %S ignores (and not rounds) fractional parts on output. "

That final senetence about strptime (i.e. parsing) is subtle: it says "for strptime %OS".  Note the absence of an 'n': it says %OS instead of %OSn.

Does that mean that %OSn can NOT be used for parsing, only for formatting?

That is what I have empirically found, but is it expected behavior or a bug?

Very annoying if expected behavior, since that means that I need different time formats for formatting and parsing.  Have never seen that before in any other language's date API.



More information about the R-help mailing list