-
Notifications
You must be signed in to change notification settings - Fork 561
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add DateTime::to_rfc3339_opts method with SecondsFormat and 'Z' support #205
Conversation
Awesome, thanks! I'll review this over the weekend, I want to do a survey of our existing API to make sure that we are working towards something consistent. |
Thanks. While Travis CI is delayed waiting for osx builds, there are two failures so far:
I assume that lint is new since the last successful build a few months ago. I'm willing to try and fix that as well, depending on how to solve (1), but that might be better in its own PR? |
We want to preserve compat back to rust 1.13, so we should disable that clippy lint. Feel free to do it in this PR but in a separate commit, thanks! By the same logic, please change it to const. I've enabled weekly builds so that we'll get alerted if clippy changes cause things to fail and future contributors won't have to deal with this. |
These additions allow convenient control of RFC 3339 formatted output: * Number of subsecond digits to display * Whether to use the 'Z' variant, instead of "+00:00" for TZ offset 0, UTC. ...while remaining faithful to the RFC 3339. The implementation uses the existing formatting Item mechanism. github: cc: chronotope#157 chronotope#178
Was using static but that's only supported as of rustc 1.17 (rust these older versions. Also continue using the copious explicit 'static lifetimes for the same compatibility, despite the clippy lint.
so it builds with rustc 1.13
Thanks for merging #206. I've rebased this branch hoping to eventually (looking at you MacOS) see passing CI for the PR. |
@quodlibetor: Tests are passing. Did you have a chance to survey and consider this regarding API consistency? In case it helps: I'm not attached to the introduced - Also this currently uses the Fixed enum for the sub-second formatting parameter. That's arguably consistent, but at the expense of a potential panic to enforce RFC 3339 constraints at runtime. An alternative would be to introduce a new enum for just the sub-second formats supported. Please let me know your preferences or other concerns. Perhaps I didn't make a strong enough case for this addition in the first place: I believe the 'Z' and fixed sub-second format variants of RFC-3339 are too commonly needed to not include in this crate, particularly when considering the amount of code required to correctly implement it externally. |
Two things:
I think that taking a new enum (especially one that statically prevents errors) will be more ergonomic, despite the extra import. My larger concern is with formatting in general -- the fact that this is complex to handle this in an external crate is a bug which should be fixed, but I think that we basically just need to create a new formatting API to manage it. If you want to help me out, the survey that I wanted to do was mostly just to look at our various other formatting APIs and see:
|
In the latest I've introduced a SecondsFormat enum. This simplifies the function enough that I also went with a single method, named In my evolving understanding of the formatting API, I share your larger concern. Error handling appears problematic with parsing formats from strings. Working with the format Items directly is complicated by enum/variant name collisions and the need for cloned value iteration? It would be nice if there was some easy way to create a static or constant Format(-ter) , for this application from a But this PR makes chrono more usable now and in the interim. When there is a a better format module, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The doc test improvements are important, but this looks great and I'd be comfortable merging it.
Some time in the future I think this enum could actually be extended to enhance the use case from #165 , too.
@@ -1059,6 +1133,28 @@ mod tests { | |||
Ok(EDT.ymd(2015, 2, 18).and_hms_micro(23, 59, 59, 1_234_567))); | |||
} | |||
|
|||
#[test] | |||
fn test_rfc3339_opts() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a couple of these asserts to the docs for to_rfc3339_opts
? Maybe just all 4 (pst/utc,true/false) of the Millis
asserts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, included in latest.
} | ||
); | ||
|
||
match ssitem { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this match could probably be something like:
PREFIX.iter().chain(s.iter()).flat_map(|s| s).chain([tzitem].iter())
I'm not sure off the top of my head what the correct nesting of flat_map with option would be (maybe just sticking the option in the map chain would do it?) but if you're willing to give it a shot I think it'd be cleaner.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried this, but I couldn't get it to compile. I'm glad we agree that the format API is hard to use though. :) I also agree the current impl looks redundant, but in my own attempts to refactor, I ran into various forms of trouble with the barrow-checker. Non-Lexical Lifetimes might make that possible in the future?
src/datetime.rs
Outdated
@@ -16,6 +16,30 @@ use Date; | |||
use format::{Item, Numeric, Pad, Fixed}; | |||
use format::{parse, Parsed, ParseError, ParseResult, DelayedFormat, StrftimeItems}; | |||
|
|||
/// Specific formatting options for seconds | |||
#[derive(Clone, Copy, Debug, Eq, PartialEq)] | |||
pub enum SecondsFormat { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually: we should probably name this FractionFormat
and also add a #[doc(hidden)] __Nonexhaustive
variant so that we can expand this enum in the future, without it being a breaking change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Naming: I went through some pre-PR iteration on this. Calling it SubSecFormat or FractionFormat makes one want to use something like Omit
for the Secs
variant and then it doesn't fit together as well. Its really intended to be one parameter that specifies the seconds format including the seconds ['.' subseconds]
. Added more doc to try and clarify this.
If I add __Nonexhaustive
in this, then I need to add a panic!()
in to_rfc3339_opts
to catch the case where it doesn't cover a new variant. The std
ErrorKind
example of this being used is for a case where the enum in question is being returned and consumed by external code. I don't think there is a valid use case for external code matching on SecondsFormat
but I added a warning in rustdoc not to do this. I hope that is sufficient.
Thank you! Sorry for the slow turnaround, but this is great! |
Thanks! Was just looking at the accumulated changes since 0.4. At some point, would be good to know if there as anything else that is particularly release gating? |
These additions allow convenient control of RFC 3339 formatted output:
Number of subsecond digits to display
Whether to use the 'Z' variant, instead of "+00:00" for TZ offset
0, UTC.
...while remaining faithful to the RFC 3339. The implementation uses the existing formatting Item mechanism.
The 'Z' variant was originally requested in #157 where it was suggested a PR was welcome. Control of subsecond digits was requested in #178 and @quodlibetor gave me a starting point for using the formatting Item mechanism.
Tests and rustdoc are included. Please consider merging this or let me know if it has any deficits, thanks!