Comments (4)
If I understand correctly the issue here is that date_format
behaves differently for POSIXct and hms objects, and for hms objects it shows seconds even when the format requested is only "%H:%M"
. A more illustrative example is below:
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"
date_format
uses format
under the hood, so that is why they behave the same. @hadley do you think this behavior should change? Or is this issue more appropriate for the hms
package?
from scales.
We should probably add support for hms.
from scales.
So this is caused because the format.hms
method converts hms
objects to character
, ultimately calling format.Default
and ignoring the format
argument to date_format
(and the underlying methods format.POSIXlt
and format.POSIXct
)
Assuming we want to fix this in scales without adjusting the format.hms
method itself, the simplest way is to call format.Date
directly, although I think this is generally considered bad practice. format.Date
and strftime
(just a wrapper for format.POSIXlt
) seem to differ in how the timezone is treated but (with proper consideration of the timezone) either will output the expected result (as shown using strftime
in the OP).
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 date_format
). Perhaps it's worth providing hms
support in a separate function time_format
? Or is it more desirable to generalize date_format
with specific handling of hms
objects so they print using a format method for date/time objects?
from scales.
I'd rewrite date_format()
along these lines:
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 date_format()
with a time.
from scales.
Related Issues (20)
- Feature request: `pal_size`, `pal_alpha`, & `pal_linewidth` HOT 5
- Feature request: properties for palettes
- Feature request: `hold` argument for `label_*` functions HOT 1
- Feature request: could scales support keeping oob values but with infinites turned to NA? HOT 2
- Consider including package name in S3 class name(s)?
- Add help for breaks_* functions that says they return a function etc
- Could arguments be added `breaks_extended`, so that it is easier to use?
- Feature - wrap at a specific character
- `breaks_width()` does not work with reverse transform
- Feature request: Could `label_*` functions support labelling every nth break? HOT 1
- Unexpected Error when using `scale_cut = cut_short_scale()` HOT 1
- Feature request: Could a `breaks_equal` function be provided? HOT 1
- Should `transform_time` be named `transform_datetime`?
- Feature request: could `scales::breaks_width` have an argument to return breaks that encompass the full range of the data?
- Feature request: `transform_squish` or `label_squish`
- Feature request: an option to remove leading zeros in `label_date_short()`
- Feature request: Could `scales::number` etc have a `style_negative` option of `"none"`?
- label_number(accuracy=) generating plots with multiple 0 values on the X axis HOT 2
- `label_log()` generated function outputs `expr` instead of a `character vector` as documented
- Label formatting in `transform_time()` with zero-width limits
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from scales.