[Rd] bug with strptime, %OS, and "."

Upton, Stephen (Steve) (CIV) scupton at nps.edu
Wed Jan 11 14:36:07 CET 2017


Works for me:
> strptime("17_35_14.01234.mp3","%H_%M_%OS")$sec
[1] 14.01234
> strptime("17_35_14.mp3","%H_%M_%OS")$sec
[1] 14

Just leave off the ".mp3" in your time pattern.

Relevant section from the help ("Details") for strptime:
strptime converts character vectors to class "POSIXlt": its input x is first
converted by as.character. Each input string is processed as far as
necessary for the format specified: any trailing characters are ignored.

R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

Stephen C. Upton
Faculty Associate - Research
SEED (Simulation Experiments & Efficient Designs) Center
Operations Research Department
Naval Postgraduate School
Mobile: 804-994-4257
NIPR: scupton at nps.edu
SIPR: uptonsc at nps.navy.smil.mil
SEED Center web site: http://harvest.nps.edu
-----Original Message-----
From: R-devel [mailto:r-devel-bounces at r-project.org] On Behalf Of
frederik at ofb.net
Sent: Tuesday, January 10, 2017 9:59 PM
To: Dirk Eddelbuettel
Cc: R-devel
Subject: Re: [Rd] bug with strptime, %OS, and "."

On Tue, Jan 10, 2017 at 08:13:21PM -0600, Dirk Eddelbuettel wrote:
> 
> On 10 January 2017 at 17:48, frederik at ofb.net wrote:
> | Hi R Devel,
> | 
> | I just ran into a corner case with 'strptime'. Recall that the "%OS"
> | conversion accepts fractional seconds:
> | 
> | > strptime("17_35_14.01234.mp3","%H_%M_%OS.mp3")$sec
> | [1] 14.01234
> | 
> | Unfortunately for my application it seems to be "greedy", in that it 
> | tries to parse a decimal point which might belong to the rest of the
> | format:
> | 
> | > strptime("17_35_14.mp3","%H_%M_%OS.mp3")
> | [1] NA
> 
> Maybe just don't use the optional O:
> 
>    R> strptime("17_35_14.mp3","%H_%M_%S.mp3")$sec
>    [1] 14
>    R> 
>    R> strptime("17_35_14.mp3","%H_%M_%S.mp3")
>    [1] "2017-01-10 17:35:14 CST"
>    R>

For my application I wanted to be able to accept both formats, "14.mp3" and
"14.01234.mp3". Since "14" and "14.01234" both parse as numbers, I thought
"%OS" should accept both.

Yes, I can work around it fairly easily.

Frederick

______________________________________________
R-devel at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


More information about the R-devel mailing list