In R, is the% OSn time format valid only for formatting and not parsing?

Consider this R code that uses a specific time format string (timeFormat variable below) to format and parse dates:


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 this into Rgui on my windows box that has the latest version (3.2.0) installed, 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 ending in% OS3 produces the correct timestamp (3-digit millisecond resolution).

However, at the same time, the format cannot parse this timestamp into the original POSIXct value; it is barfs and parses NA.

Does anyone know what's going on?

A web search found a StackOverflow question , where one of the commenters, Waldir Leoncio, in the first answer seems to describe the same parsing error with% OS3 I am doing:

"use, for example, strptime (y,"% d.% m.% Y% H:% M:% OS3 "), but that doesn't work for me. Henrik pointed out that the man page is about the function ,? Strptime that bit% OS3 is OS dependent. I am using updated Ubuntu 13.04 and using% OS3 gives NA. "

The help page mentioned in the above quote is most likely this link , which is unfortunately short, just to say

"Specifically for R, there is% OSn, which for output gives truncated seconds to 0 <= n <= 6 decimal places (and if% OS is not followed by a digit, it uses the getOption setting (" digits.secs "), or if it is not given, n = 3). Also, for strptime,% OS will enter seconds, including fractional seconds. Note that% S ignores (rather than rounds) fractional parts in output. "

This final senetence about strptime (ie parsing) is subtle: it says "for strptime% OS". Notice the absence of "n":% OSn is% OSn instead of% OSn.

Does this mean that% OSn CANNOT be used for parsing, only for formatting?

This is what I found empirically, but expected behavior or bug?

It is very annoying if behavior is expected, as that means I need different time formats for formatting and parsing. Never seen this before in any other API language ...

(Also: I know there is another problem, even if you just want to format, with% OSn: R truncates fractional parts instead of rounds. For those not aware of this bad behavior, its dangers are discussed here , here, and here .)

+3


source to share


1 answer


This is expected behavior, not a bug. "%OSn"

for withdrawal. "%OS"

is for input and includes fractional seconds as your second block frame says:

In addition, strptime

%OS

seconds will be entered for, including fractional seconds.



options(digits.secs=6)
as.POSIXct("2015-06-09 11:24:19.002", "America/New_York", "%Y-%m-%d %H:%M:%OS")
# [1] "2015-06-09 11:24:19.002 EDT"

      

Also note that "EST"

is an ambiguous time zone and probably not what you expect. See the "Time Zone Names" section ?timezone

.

+1


source







All Articles