Bug 17209 - strptime, strftime and %OS symmetry
Summary: strptime, strftime and %OS symmetry
Status: UNCONFIRMED
Alias: None
Product: R
Classification: Unclassified
Component: Language (show other bugs)
Version: R 3.3.*
Hardware: Other Linux
: P5 enhancement
Assignee: R-core
URL:
Depends on:
Blocks:
 
Reported: 2017-01-12 21:52 UTC by Frederick Eaton
Modified: 2017-01-14 05:40 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Frederick Eaton 2017-01-12 21:52:56 UTC
For most of the format specifiers accepted by "strptime" and "strftime", the same specifier works for formatting and parsing. This is convenient because I can use a single variable to store the format some data. The same variable can be used by the code for parsing and generating the data.

However, there is an asymmetry for "%OS". With 'strftime', "%OS" should be followed by a number specifying digits of precision.

> strftime(as.POSIXct(5.284),"%OS6")
[1] "05.284000"

But for 'strptime', following it with a number breaks parsing:
 
> strptime("17_35_14.01234.mp3","%H_%M_%OS6.mp3")$sec
[1] NA
> strptime("17_35_14.01234.mp3","%H_%M_%OS.mp3")$sec
[1] 14.01234

It seems preferable to me to have both functions accept e.g. "%OS6", where 'strptime' could just ignore the "6". I don't think there is any way this would break existing code, because "%OS" in 'strptime' currently gobbles up every digit, so there is no way someone could be currently using "%OS2498" to mean "%OS followed directly by the number 2498".
Comment 1 Frederick Eaton 2017-01-14 05:40:20 UTC
My workaround:

strptime = function(x, fmt) {
  fmt = gsub("%OS[0-9]+", "%OS", fmt)
  base::strptime(x, fmt)
}