3

I'm trying to convert some dates in R with strptime. I've read this thread and have set my locale so that it should work. For example:

> Sys.getlocale("LC_TIME")
[1] "en_GB"
> format(Sys.time(), format="%B")
[1] "November"

However if I try to convert a month string, strptime returns NA, e.g.:

> strptime("November", format="%B")
[1] NA

I can run the example at the above link without any problem but if I simplify the example to just include the month, I get NA again.

> var <- "Thu Nov 8 15:41:45 2012"
> strptime(var, format="%a %b %d %H:%M:%S %Y")
[1] "2012-11-08 15:41:45 GMT"
> strptime("Nov", format="%b")
[1] NA

What's going on here? How can I parse a character month into a date object?

EDIT Thanks for the comments below. To clarify, this also doesn't work:

> strptime("November 2011", format="%B %Y")
[1] NA

I would also expect strptime to fill in any missing fields with the current system time, as in strptime("2011", format="%Y")

Community
  • 1
  • 1
jkeirstead
  • 2,881
  • 3
  • 23
  • 26
  • "November" is not a date or a datetime. You need at least a year, month, and day to specify a date/datetime. – Joshua Ulrich Nov 18 '14 at 15:49
  • 2
    @JoshuaUlrich I thought the same way, but surprisingly, for almost all other cases, the missing parts of the date are "filled in". Try this: `strptime('11', '%M')`: `"2014-11-18 00:11:00 EST"` – nograpes Nov 18 '14 at 15:51
  • @nograpes: that's unfortunate. I would prefer missing values/errors to "assume you meant it to be based on the current datetime". Odd that it's inconsistent and only does that for non-month values though. – Joshua Ulrich Nov 18 '14 at 15:53

1 Answers1

6

I am a bit confused by R's behaviour in the case of missing values in strptime. The documentation is clear enough:

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. Some components may be returned as NA (but an unknown tzone component is represented by an empty string).

But that doesn't always seem to be the case with months. Here is a particularly odd example:

strptime('Nov-01', '%B-%d')
[1] "2014-11-01"
strptime('2009-Nov', '%Y-%B')
[1] NA

An obvious workaround based on the above would be to make a simple function.

convert.months <- function(x) 
     strptime(paste(x,strftime(Sys.time(),'%d')), '%b %d')

Which is ugly, but it works:

convert.months(c("Nov","Dec"))
[1] "2014-11-18 EST" "2014-12-18 EST"

To be clear here are the test cases, which, according to my interpretation of the documentation, should all work:

strptime('11', '%m') # Fail
strptime('11-01', '%m-%d') # Works
strptime('2009-11', '%Y-%m') # Fail

strptime('Nov', '%b') # Fail
strptime('Nov-01', '%b-%d') # Works
strptime('2009-Nov', '%Y-%b') # Fail
nograpes
  • 18,623
  • 1
  • 44
  • 67
  • 1
    Notably, the [IEEE specification for strptime](http://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html) is silent on the matter of not fully specified dates. – nograpes Nov 18 '14 at 16:33
  • I'll have to go with this for the time being. Not very satisfying though, is it? – jkeirstead Nov 18 '14 at 17:21
  • Unsatisfying indeed. I suspect that this is a bug, but I wouldn't bring it up unless I had thoroughly read the source code. – nograpes Nov 18 '14 at 19:19