Skip to content

Latest commit

 

History

History
60 lines (34 loc) · 2.9 KB

File metadata and controls

60 lines (34 loc) · 2.9 KB

Step 3.3: Date and time

Estimated time: 1 day

Rust has a simple std::time module which contains very basic primitives for time measurements. To operate with dates, time zones, epochs, and other related stuff, the time and chrono crates are used in Rust ecosystem.

The main difference between them (except the API, ergonomics and maintaining activity) is that chrono crate parametrizes time zone in types, while time crate handles it in runtime. In practice, we recommend to use time crate (unless chrono better suits your needs), as it's much actively maintained and evolved.

If you hit limitations of time and chrono crates regarding their accuracy (like swallowing leap seconds) or supported formats/standards (like TAI), consider using the hifitime crate, representing a scientifically accurate and formally verified date and time library.

For better understanding and familiarity, read through the following documentation:

Duration measurements for code

Beware, that to measure duration of some operation, you should not use time crate primitives or an std::time::SystemTime, but only an std::time::Instant instead, as it provides monotonic clock measurement (otherwise, your time measurement may be inconsistent due to system clock drift).

Task

Provide implementations for User::age() and User::is_adult() methods in this step's crate.

Prove your implementation correctness with additional tests. For tests reproducibility consider that "now time" is the date specified in the NOW constant.

Questions

After completing everything above, you should be able to answer (and understand why) the following questions:

  • How does system clock and monotonic clock differ? What are use-cases for both?
  • Why is system clock is not reliable for measuring duration? What causes its drift?
  • What is the main practical difference between chrono and time crates?
  • When hifitime crate could be useful?