[Rd] strptime("1","%m") returns NA

Martyn Plummer plummerm at iarc.fr
Tue Jan 17 14:35:35 CET 2017

Hi Frederik,

On Mon, 2017-01-16 at 18:20 -0800, frederik at ofb.net 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

> 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."



> 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
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

More information about the R-devel mailing list