-
-
Notifications
You must be signed in to change notification settings - Fork 34
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
api: introducing the now_with_time_zone for Zoned #94
Conversation
src/zoned.rs
Outdated
/// use jiff::{Timestamp, Zoned}; | ||
/// use jiff::tz::TimeZone; | ||
/// | ||
/// assert!(Zoned::now_with_time_zone(TimeZone::UTC).timestamp() > Timestamp::UNIX_EPOCH); |
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.
Can you make sure this is wrapped to 79 columns (inclusive)? Probably like this:
let zdt = Zoned::now_with_time_zone(TimeZone::UTC);
assert!(zdt.timestamp() > Timestamp::UNIX_EPOCH);
@@ -413,6 +413,40 @@ impl Zoned { | |||
.expect("system time is valid") | |||
} | |||
|
|||
/// Returns the current system time with the specific time zone. |
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.
Can you add that this is a convenience function for Zoned::new(Timestamp::now(), time_zone)
?
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.
Thanks @BurntSushi for the quick review!
All commnets are addressed :)
052e9ed
to
6202f70
Compare
This PR adds a note to the `Zoned::now()` documentation describing the best way to create a `Zoned` for the current time in a time zone that isn't the system time zone. Unfortunately, one intuitive route is `Zoned::now().with_time_zone()`, but this 1) fetches the system time zone, which isn't free and 2) computes the civil datetime for the system time zone, which is not necessary here. Instead, `Timestamp::now().to_zoned()` is a more direct approach that doesn't do any unnecessary work. I decided to go this route instead of adding a new API as requested in #93. In particular, I think the API suggested in #93 is a bit clunky, and is likely to lead to the addition of other even clunkier APIs as well. While using a `Timestamp` might require an extra import, it does have the benefit of being compositional. I'd like to encourage that instead of adding a bunch of alternative constructors. Moreoever, I feel like the Jiff API overall does a good job of avoiding lots of different constructors that I usually find somewhat confusing in other APIs. We might eventually have to give into that depending on use cases, but I think for this here, a documentation note suffices. Closes #93, Closes #94
close #93
Currently, Zoned has now funcation to return the current system time in this system's time zone. I propose to introduce the now_with_time_zone for Zoned.
It's needed for some reasons:
How to create a now Zoned with specific time zone for current code?
Zoned::now
(Solution2) has clearer semantics for developers, but it requires repeated creation of TimeZone objects. (Zoned::now needs to clone a default TimeZone, and with_time_zone also requires a TimeZone).I have a benchmark for them[2]:
- The avg time solution 1 is 297.61 ns
- The avg time solution 2 is 443.87 ns
- 297.61 ns * 151% = 443.87 ns, so performance of solution 1 is totally better than solution2.
The ZonedDateTime[3] of Java has 3 now functions, it includes now(), now(ZoneId) and now(Clock). It's useful for developers.
[1] https://github.com/fast/logforth/blob/b2d5e7e664595f58d35377136b4936fc4ac22c3f/src/layout/text.rs#L89
[2] https://github.com/1996fanrui/fanrui-learning/blob/aacfa73de1a2299745241806f61aa098d0b66a23/rust-learning/benches/jiff_benchmark.rs#L6
[3] https://docs.oracle.com/en%2Fjava%2Fjavase%2F11%2Fdocs%2Fapi%2F%2F/java.base/java/time/ZonedDateTime.html#now(java.time.ZoneId)