|
1 |
| -# What is Time? |
| 1 | +# Introduction |
| 2 | + |
| 3 | +`time` is a date and time library for Rust. It is: |
| 4 | + |
| 5 | +- **Easy and safe**. `time` has a |
| 6 | +[straightforward API](https://time-rs.github.io/api/time/struct.OffsetDateTime.html) without |
| 7 | +footguns. |
| 8 | +- **space optimal and efficient**. `time` provides support for dates in the ±9999 year range, with |
| 9 | +nanosecond precision; ranges up to ±999,999 are supported with the `large-dates` feature. |
| 10 | +- **`serde` ready**. Supports ISO8601, RFC2822 and RFC3339 with the `serde-well-known` feature. |
| 11 | +Not in the list? [Make your format](./api/format-description.md). |
| 12 | +- **`const` ready**. A majority of the API is `const`, making it ready for resource-constrained |
| 13 | +applications, with optional [macros](https://time-rs.github.io/api/time/macros/index.html) for easy |
| 14 | +date creation. |
| 15 | +- **`no-std` support** with `alloc` and `std` features. |
| 16 | +- **numeric traits**. Use durations easily: `2.seconds()`. |
| 17 | +- Supports Windows, Linux, macOS, |
| 18 | +[WebAssembly](https://developer.mozilla.org/fr/docs/WebAssembly) targets among others. |
| 19 | +- Six-month |
| 20 | +[minimum supported Rust version](https://rust-lang.github.io/rfcs/2495-min-rust-version.html) |
| 21 | +guarantee. |
| 22 | + |
| 23 | +And more... |
| 24 | + |
| 25 | +## Getting started |
| 26 | + |
| 27 | +This short tutorial describes basic usage of `time`, to get operational quickly. |
| 28 | + |
| 29 | +1. **Install `time`**. Add it to your `Cargo.toml`. We'll enable `macros`: |
| 30 | +```toml |
| 31 | +[dependencies] |
| 32 | +time = { version = "0.3", features = ["macros"] } |
| 33 | +``` |
| 34 | + |
| 35 | +2. **Create dates and times.** We can create dates |
| 36 | +([`Date`](https://docs.rs/time/latest/time/struct.Date.html)), |
| 37 | +dates with times |
| 38 | +([`PrimitiveDateTime`](https://docs.rs/time/latest/time/struct.PrimitiveDateTime.html)) |
| 39 | +and date times with an UTC offset |
| 40 | +([`OffsetDateTime`](https://docs.rs/time/latest/time/struct.OffsetDateTime.html)). |
| 41 | +A simple [`Time`](https://docs.rs/time/latest/time/struct.Time.html) is also available. |
| 42 | + |
| 43 | +```rust |
| 44 | +use time::{Date, PrimitiveDateTime, OffsetDateTime, UtcOffset}; |
| 45 | +use time::Weekday::Wednesday; |
| 46 | + |
| 47 | +let date = Date::from_iso_week_date(2022, 1, Wednesday).unwrap(); |
| 48 | +let datetime = date.with_hms(13, 0, 55).unwrap(); |
| 49 | +let datetime_off = datetime.assume_offset(UtcOffset::from_hms(1, 2, 3).unwrap()); |
| 50 | + |
| 51 | +println!("{date}, {datetime}, {datetime_off}"); |
| 52 | +// 2022-01-01, 2022-01-01 13:00:55.0, 2022-01-01 13:00:55.0 +01:02:03 |
| 53 | +``` |
| 54 | + |
| 55 | +With the `macros` feature: |
| 56 | + |
| 57 | +```rust |
| 58 | +use time::macros::{date, datetime}; |
| 59 | + |
| 60 | +let date = date!(2022-01-01); |
| 61 | +let datetime = datetime!(2022-01-01 13:00:55); |
| 62 | +let datetime_off = datetime!(2022-01-01 13:00:55 +1:02:03); |
| 63 | + |
| 64 | +println!("{date}, {datetime}, {datetime_off}"); |
| 65 | +// 2022-01-01, 2022-01-01 13:00:55.0, 2022-01-01 13:00:55.0 +01:02:03 |
| 66 | +``` |
| 67 | + |
| 68 | +3. **Manipulate dates and use |
| 69 | +[`Duration`s](https://time-rs.github.io/api/time/struct.Duration.html)**: |
| 70 | +```rust |
| 71 | +use time::Duration; |
| 72 | +use time::macros::{datetime}; |
| 73 | + |
| 74 | +let a = datetime!(2022-01-01 10:00:55); |
| 75 | +let b = datetime!(2022-01-01 13:00:00); |
| 76 | + |
| 77 | +let duration: Duration = b - a; |
| 78 | + |
| 79 | +println!("{}", b - a); |
| 80 | +// 2h59m5s |
| 81 | +``` |
| 82 | + |
| 83 | +## `time` vs `chrono` |
| 84 | + |
| 85 | +time 0.1 was originally a thin wrapper around libc time functions. Because it was relatively |
| 86 | +barebones, [`chrono`](https://docs.rs/chrono/0.4.19/chrono/) was developped as a richer API on top |
| 87 | +of time 0.1. |
| 88 | + |
| 89 | +Around 2019, the time crate, which was unmaintained since August 2016, was picked up for maintenance |
| 90 | +again. `time` has since been rewritten as of time 0.2, and is incompatible with the 0.1 version. |
| 91 | + |
| 92 | +Today: |
| 93 | +- `time` has been rewritten from 0.1,and is actively developed. |
| 94 | +- `chrono` depends on time 0.1, an old version unrelated with current `time`, and is actively |
| 95 | +developed as well. |
| 96 | + |
| 97 | +Since they are incompatible with each other, please choose the library that fits your needs. |
0 commit comments