bug with strptime, %OS, and "."

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

bug with strptime, %OS, and "."

frederik-2
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

If I use "_" instead of ".", then it works:

> strptime("17_35_14_mp3","%H_%M_%OS_mp3")
[1] "2017-01-10 17:35:14 PST"

Perhaps a low priority, but seems like a bug to me...

Thanks,

Frederick

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: bug with strptime, %OS, and "."

Dirk Eddelbuettel

On 10 January 2017 at 17:48, [hidden email] 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>
   
Dirk
 
| If I use "_" instead of ".", then it works:
|
| > strptime("17_35_14_mp3","%H_%M_%OS_mp3")
| [1] "2017-01-10 17:35:14 PST"
|
| Perhaps a low priority, but seems like a bug to me...
|
| Thanks,
|
| Frederick
|
| ______________________________________________
| [hidden email] mailing list
| https://stat.ethz.ch/mailman/listinfo/r-devel

--
http://dirk.eddelbuettel.com | @eddelbuettel | [hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: bug with strptime, %OS, and "."

frederik-2
On Tue, Jan 10, 2017 at 08:13:21PM -0600, Dirk Eddelbuettel wrote:

>
> On 10 January 2017 at 17:48, [hidden email] 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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: bug with strptime, %OS, and "."

Upton, Stephen C
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: [hidden email]
SIPR: [hidden email]
SEED Center web site: http://harvest.nps.edu
-----Original Message-----
From: R-devel [mailto:[hidden email]] On Behalf Of
[hidden email]
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, [hidden email] 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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: bug with strptime, %OS, and "."

frederik-2
Thanks for the reply. That's a creative way to do it, but I wanted to
use the same format string for both parsing and formatting my file
name.

I figured I should just put this on the bug tracker in case someone is
editing strptime at some point in the distant future:

https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=17208

I also made a related bug asking for e.g. "%OS6" to be accepted by
strptime.

https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=17209

Cheers,

Frederick

On Wed, Jan 11, 2017 at 01:36:07PM +0000, Upton, Stephen (Steve) (CIV) wrote:

> 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: [hidden email]
> SIPR: [hidden email]
> SEED Center web site: http://harvest.nps.edu
> -----Original Message-----
> From: R-devel [mailto:[hidden email]] On Behalf Of
> [hidden email]
> 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, [hidden email] 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
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

strptime("1","%m") returns NA

frederik-2
In reply to this post by frederik-2
Hi R Devel,

I wrote some code which depends on 'strptime' being able to parse an
incomplete date, like this:

> base::strptime("2016","%Y")
[1] "2016-01-14 PST"

The above works - although it's odd that it gives the month and day
for Sys.time(). I might expect it to set them both to zero as the GNU
libc strptime does on my system, or to use January 1 which would also
be reasonable.

When I specify the month, however, I get NA:

> base::strptime("2016-12","%Y-%m")
[1] NA
> base::strptime("1", "%m")
[1] NA

Any reason for this to be the case?

I reported a bug here:

https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=17212

but I don't think I'm getting emails from Bugzilla so maybe best to
ping me if anyone replies there instead.

I've just written a simple reimplementation of 'strptime' for my own
use; I hope this bug report may be useful to others.

Thank you,

Frederick

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: strptime("1","%m") returns NA

Martyn Plummer-3
Hi Frederik,

On Mon, 2017-01-16 at 18:20 -0800, [hidden email] wrote:

> Hi R Devel,
>
> I wrote some code which depends on 'strptime' being able to parse an
> incomplete date, like this:
>
> >
> > base::strptime("2016","%Y")
> [1] "2016-01-14 PST"
>
> The above works - although it's odd that it gives the month and day
> for Sys.time(). I might expect it to set them both to zero as the GNU
> libc strptime does on my system, or to use January 1 which would also
> be reasonable.

From the help page for strptime:

"For ‘strptime’ the input string need not specify the date completely:
it is assumed that unspecified seconds, minutes or hours are zero, and
an unspecified year, month or day is the current one."
 

> When I specify the month, however, I get NA:
>
> >
> > base::strptime("2016-12","%Y-%m")
> [1] NA
> >
> > base::strptime("1", "%m")
> [1] NA
>
> Any reason for this to be the case?

Also from the help page:

"(However, if a month is specified, the day of that month has to be
specified by ‘%d’ or ‘%e’ since the current day of the month need not
be valid for the specified month.)"

If strptime("2016-2", "%Y-%m") just filled in the current day then it
would give valid output when called on the 1st to the 28th of each
month, but would give either invalid output or fail when called on the
29th to the 31st of any month. This would be a nightmare to debug. The
current behaviour lets you know there is a logical problem with your
input.

> I reported a bug here:
>
> https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=17212
>
> but I don't think I'm getting emails from Bugzilla so maybe best to
> ping me if anyone replies there instead.

See the general guidance on submitting bug reports:

"Code doing something unexpected is not necessarily a bug - make sure to carefully review the documentation for the function you are calling to see if the behaviour it exhibits is what it was designed to do, even if it’s not what you want."

https://www.r-project.org/bugs.html


Martyn

> I've just written a simple reimplementation of 'strptime' for my own
> use; I hope this bug report may be useful to others.
>
> Thank you,
>
> Frederick
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: strptime("1","%m") returns NA

frederik-2
Hi Martyn,

Thanks for finding that stuff in the documentation, and apologies for
not reading the whole thing carefully. I guess when I got to the
minutiae about printing years before '999', I started to skim.

My vote is for more sensible / standard behavior, but I guess this has
probably been this way for a long time.

Frederick

On Tue, Jan 17, 2017 at 01:35:35PM +0000, Martyn Plummer wrote:

> Hi Frederik,
>
> On Mon, 2017-01-16 at 18:20 -0800, [hidden email] wrote:
> > Hi R Devel,
> >
> > I wrote some code which depends on 'strptime' being able to parse an
> > incomplete date, like this:
> >
> > >
> > > base::strptime("2016","%Y")
> > [1] "2016-01-14 PST"
> >
> > The above works - although it's odd that it gives the month and day
> > for Sys.time(). I might expect it to set them both to zero as the GNU
> > libc strptime does on my system, or to use January 1 which would also
> > be reasonable.
>
> From the help page for strptime:
>
> "For ‘strptime’ the input string need not specify the date completely:
> it is assumed that unspecified seconds, minutes or hours are zero, and
> an unspecified year, month or day is the current one."
>  
> > When I specify the month, however, I get NA:
> >
> > >
> > > base::strptime("2016-12","%Y-%m")
> > [1] NA
> > >
> > > base::strptime("1", "%m")
> > [1] NA
> >
> > Any reason for this to be the case?
>
> Also from the help page:
>
> "(However, if a month is specified, the day of that month has to be
> specified by ‘%d’ or ‘%e’ since the current day of the month need not
> be valid for the specified month.)"
>
> If strptime("2016-2", "%Y-%m") just filled in the current day then it
> would give valid output when called on the 1st to the 28th of each
> month, but would give either invalid output or fail when called on the
> 29th to the 31st of any month. This would be a nightmare to debug. The
> current behaviour lets you know there is a logical problem with your
> input.
>
> > I reported a bug here:
> >
> > https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=17212
> >
> > but I don't think I'm getting emails from Bugzilla so maybe best to
> > ping me if anyone replies there instead.
>
> See the general guidance on submitting bug reports:
>
> "Code doing something unexpected is not necessarily a bug - make sure to carefully review the documentation for the function you are calling to see if the behaviour it exhibits is what it was designed to do, even if it’s not what you want."
>
> https://www.r-project.org/bugs.html
>
>
> Martyn
>
> > I've just written a simple reimplementation of 'strptime' for my own
> > use; I hope this bug report may be useful to others.
> >
> > Thank you,
> >
> > Frederick
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel