You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
date_format does not work correctly on hms objects #88
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:
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?
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?
Activity
karawoo commentedon Jun 14, 2017
If I understand correctly the issue here is that
date_formatbehaves 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:date_formatusesformatunder 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 thehmspackage?hadley commentedon Jun 14, 2017
We should probably add support for hms.
dpseidel commentedon Jun 7, 2018
So this is caused because the
format.hmsmethod convertshmsobjects tocharacter, ultimately callingformat.Defaultand ignoring theformatargument todate_format(and the underlying methodsformat.POSIXltandformat.POSIXct)Assuming we want to fix this in scales without adjusting the
format.hmsmethod itself, the simplest way is to callformat.Datedirectly, although I think this is generally considered bad practice.format.Dateandstrftime(just a wrapper forformat.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 usingstrftimein 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 providinghmssupport in a separate functiontime_format? Or is it more desirable to generalizedate_formatwith specific handling ofhmsobjects so they print using a format method for date/time objects?hadley commentedon Jun 14, 2018
I'd rewrite
date_format()along these lines:Alternatively, it might be better to add a custom time format type — it's a bit odd to use
date_format()with a time.Add time_format() function for time formatting
Add time_format() function for time formatting
Add time_format() function for time formatting