Parse without seconds
Not sure if this is a dupe of #30, but I'd expect as_hms to work without seconds, or at least give an intelligible warning or error.
hms::as_hms("08:00")
Error in `abort_lossy_cast()`:
! Lossy cast from <character> to <hms> at position(s) 1
Run `rlang::last_error()` to see where the error occurred.
Inspired by this Stack Overflow question where a user without seconds couldn't figure out what was going wrong.
Thanks. I agree the error message could be clearer.
The function as_hms() converts among other things character vectors properly formatted to 'hms' object.
We would like this function to fail elegantly however when the expected format is not met, currently the error is not satisfying.
The documentation of as_hms() isn't very explicit and should be updated as well. OTOH ?parse_hms (called by as_hms()) tells us :
parse_hms() accepts values of the form "HH:MM:SS", with optional fractional seconds.
This means that hms::as_hms("08:00") cannot work, but also that hms::as_hms("800:00:00") cannot work.
It also misleads us into thinking that hms::as_hms("25:00:00") will work, and it won't because strptime() is called and stops at 24.
Given the strict format input it would seem reasonable to check the format with a regular expression (allowing decimals for seconds) then split by ":" and convert so that the round trip hms::hms(360000) |> as.character() |> as_hms() works.
If a format in the form "12:34" is provided the error message could address it specifically and suggest to use parse_hm() instead, or pad the input (we don't have parse_ms(), but maybe we should ?).
The doc of both parse_hms() and as_hms() would need to be updated too.