date_format does not work correctly on hms objects #88
Comments
If I understand correctly the issue here is that library(hms)
library(scales)
t <- Sys.time()
date_format("%H:%M", tz = "GMT")(t)
#> [1] "17:22"
date_format("%H:%M", tz = "GMT")(as.hms(t))
#> [1] "17:22:08.155499"
format(t, "%H:%M", tz = "GMT")
#> [1] "17:22"
format(as.hms(t), "%H:%M", tz = "GMT")
#> [1] "17:22:08.155499"
|
We should probably add support for hms. |
So this is caused because the Assuming we want to fix this in scales without adjusting the This fix of course stops making sense when a user provides an hms object and asks for a format containing dates not just hours minutes and seconds (as is currently the default for |
I'd rewrite date_format <- function(format = "%Y-%m-%d", tz = 'UTC') {
function(x) {
if (inherits(x, "POSIXt")) {
format(x, format, tz = tz)
} else if (inherits(x, "Date")) {
} else if (inherits(x, "difftime")) {
} else {
stop("Unsupported type (needs better error message)")
}
}
} Alternatively, it might be better to add a custom time format type — it's a bit odd to use |
As the function is called "date_format" behaving like "format" is perhaps the right behaviour.
Is this perhaps rather an issue with the hms library which should implement some kind of format method?
The text was updated successfully, but these errors were encountered: