Add time_format() function for time formatting #136
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds a new function,
time_format
, to format times properly, for bothPOSIXt
andhms
objects. A separate function made more sense to me than did adjustingdate_format
to accommodate times, but if this code feels too redundant, I'm happy to implement changes todate_format
instead.I also wonder if date_format and/or time_format shouldn't be moved to formatter.r with all the other formatters.
Addresses #88
Some notes about handling timezones for hms objects:
For consistency, it seemed important to continue using standard POSIX specification for formatting, but while time zone of hms objects can be specified at conversion (from other datetime classes, defaults to local time) but they not have a tzone attribute themselves and can't/don't respect daylight savings (i.e. a day == 24 hours). Coercing them back to POSIXct is easy but always sets the timezone to "UTC" -- if our function allows the user to change this in higher level format function, times can quickly get misrepresented, especially around daylight savings time. On the other hand, if the user knows the original timezone of the hms object and set it properly during conversion, then being able to play with timezones is maybe a desirable feature.
Ultimately I decided to go with the simplest approach and coerce hms objects to POSIXct and preserve the ability for the user to set the tz in the higher level format function but I'm curious what behavior you think is best.
Some possible alternatives:
tz
argument in line 143 OR dropping the tz argument entirely, replacing it with dots and feeding POSIXt objects directly intodate_format
which could taketz
when specified. It seemed a bit odd not to have a timezone argument in a formatting function for times though.Also worth noting, while I hope anyone passing an
hms
object won't attempt to request a date-time format (especially from the explicitly namedtime_format
function), as it stands, this fix doesn't do anything to prevent or warn users doing this. If they do, the results will be incorrect (i.e. reporting from January 1970).