BurntSushi / jiff

A date-time library for Rust that encourages you to jump into the pit of success.
The Unlicense
1.65k stars 26 forks source link

api: introducing the now_with_time_zone for Zoned #94

Closed 1996fanrui closed 3 weeks ago

1996fanrui commented 1 month ago

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:

  1. How to create a now Zoned with specific time zone for current code?

    // solution 1
    Zoned::new(Timestamp::now(), time_zone)
    
    // Solution 2, logforth[1] is using this solution.
    Zoned::now().with_time_zone(tz)

    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.
  2. 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)