The problem with %p
part is locale related. See this issue.
The inability to parse has to do with the way lubridate guesser works.
Tthere are two ways lubridate infers formats, flex and exact. With flex matching all numeric elements can have flexible length (for example both 4
and 04
for day will work), but then, there must be non-numeric separators between the elements. For the exact matcher there need not be non-numeric separators but elements must have exact number of digits (like 04
).
Unfortunately you cannot combine both matchers within one expression. It would be extremely hard to fix this and preserve the current flexibility of the lubridate parser.
In your example
> parse_date_time('4/18/1950 0130', 'mdY HM')
[1] NA
Warning message:
All formats failed to parse. No formats found.
you want to perform flex matching on the date part 4/18/1950
and exact matching on time part 0130
.
Please note that if your date-time is in fully flex
, or fully exact
format the parsing will work as expected:
> parse_date_time('04/18/1950 0130', 'mdY HM')
[1] "1950-04-18 01:30:00 UTC"
> parse_date_time('4/18/1950 1:30', 'mdY HM')
[1] "1950-04-18 01:30:00 UTC"
The lubridate 1.4.1
"fixes" this by adding a new argument to parse_date_time
, exact=FALSE
. When set toTRUE
the orders
argument is interpreted as containing exact strptime
formats and no guessing or training is performed. This way you can add as many exact formats as you want and you will also gain in speed because no guessing is performed at all.
> parse_date_time(c('12/17/1996 04:00:00','4/18/1950 0130'),
+ c('%m/%d/%Y %I:%M:%S','%m/%d/%Y %H%M'),
+ exact = T)
[1] "1996-12-17 04:00:00 UTC" "1950-04-18 01:30:00 UTC"
Relatedly, there was an explicit requested asking for such an option.