I've been using date_parse to parse character inputs, from a user, in to dates with something akin to
as_myclass.character <- function(x, format = NULL, locale = clock_locale(), ...) {
check_dots_empty()
x <- date_parse(x, format = format, locale = locale)
if (all(is.na(x))) abort("Unable to parse any entries of x as Dates")
vec_cast(x, new_myclass())
}
However I have just noticed the warning in the "Details" of the documentation:
Parsing strings with sub-daily components, such as hours, minutes, or seconds, should be done with date_time_parse(). If you only need the date components, round the result to day precision, and then use as_date(). Attempting to directly parse a sub-daily string into a Date is ambiguous and undefined, and is unlikely to work as you might expect.
I had wrongly been assuming this would work similar to strptime where trailing characters were ignored
strptime converts character vectors to class "POSIXlt": its input x is first converted by as.character. Each input string is processed as far as necessary for the format specified: any trailing characters are ignored.
This leads to the following question. If passing a sub-standard daily string into a Date is undefined in clock would it be better for the following to error?
clock::date_parse(c("2019-01-01 11", "2019-01-01 12"), format = "%Y-%m-%d")
Hope this makes sense. This potentially links to #255
I've been using date_parse to parse character inputs, from a user, in to dates with something akin to
However I have just noticed the warning in the "Details" of the documentation:
I had wrongly been assuming this would work similar to strptime where trailing characters were ignored
This leads to the following question. If passing a sub-standard daily string into a Date is undefined in clock would it be better for the following to error?
Hope this makes sense. This potentially links to #255