3

I am trying to parse a string representing a period consisting of minutes, seconds, and milliseconds. My preferred functions for this would come from the readr package, where seconds and milliseconds may be seen jointly as partial seconds. Apparently, within this package there is a silent assumption that minutes are represented as two digits, i.e. padded with zeros.

readr::parse_time("1:23.456", format="%M:%OS")  # doesn't work
readr::parse_time("01:23.456", format="%M:%OS") # works

The ms function from lubridate handles this straight out of the box:

lubridate::ms("1:23.456")

Any workaround for this so I can use parse_time and other functions in readr without resorting to pad with zeros myself?

1 Answers1

0

The format specifications can be shown here: https://stat.ethz.ch/R-manual/R-devel/library/base/html/strptime.html and https://readr.tidyverse.org/reference/parse_datetime.html

The issue here is that %M refers to the time shown in minutes between "00" and "99". Note that this is the exact specification that you passed so it is part of the specified format rather than an assumption of the package. As far as I'm aware there is no minutes argument that accepts varying string lengths that can be passed to the format column. (This is dissimilar to the day argument which would accept a single character).

Lubridate's ms function uses a different method to parse time strings. Lubridate's function is far more robust due to being able to handle many formats of times including the one specified in the question when no format is given.

Joel Kandiah
  • 1,465
  • 5
  • 15
  • 1
    Opened [https://github.com/tidyverse/readr/issues/1489](https://github.com/tidyverse/readr/issues/1489) to track this. `%M` supports both 0–59 and 00–59 in lubridate but there's only code for 00–59 in readr. – Todd West Apr 03 '23 at 18:45