| 1 | Jiff |
| 2 | ==== |
| 3 | Jiff is a datetime library for Rust that encourages you to jump into the |
| 4 | pit of success. The focus of this library is providing high level datetime |
| 5 | primitives that are difficult to misuse and have reasonable performance. Jiff |
| 6 | supports automatic and seamless integration with the Time Zone Database, DST |
| 7 | aware arithmetic and rounding, formatting and parsing zone aware datetimes |
| 8 | losslessly, opt-in Serde support and a whole lot more. |
| 9 | |
| 10 | Jiff takes enormous inspiration from [Temporal], which is a [TC39] proposal to |
| 11 | improve datetime handling in JavaScript. |
| 12 | |
| 13 | [](https://github.com/BurntSushi/jiff/actions) |
| 14 | [](https://crates.io/crates/jiff) |
| 15 | [](https://docs.rs/jiff) |
| 16 | |
| 17 | Dual-licensed under MIT or the [UNLICENSE](https://unlicense.org/). |
| 18 | |
| 19 | [TC39]: https://tc39.es/ |
| 20 | [Temporal]: https://tc39.es/proposal-temporal/docs/index.html |
| 21 | |
| 22 | ### Documentation |
| 23 | |
| 24 | * [API documentation on docs.rs](https://docs.rs/jiff) |
| 25 | * [Comparison with `chrono`, `time`, `hifitime` and `icu`](COMPARE.md) |
| 26 | * [The API design rationale for Jiff](DESIGN.md) |
| 27 | * [Platform support](PLATFORM.md) |
| 28 | * [CHANGELOG](CHANGELOG.md) |
| 29 | |
| 30 | ### Example |
| 31 | |
| 32 | Here is a quick example that shows how to parse a typical RFC 3339 instant, |
| 33 | convert it to a zone aware datetime, add a span of time and losslessly print |
| 34 | it: |
| 35 | |
| 36 | ```rust |
| 37 | use jiff::{Timestamp, ToSpan}; |
| 38 | |
| 39 | fn main() -> Result<(), jiff::Error> { |
| 40 | let time: Timestamp = "2024-07-11T01:14:00Z".parse()?; |
| 41 | let zoned = time.in_tz("America/New_York")?.checked_add(1.month().hours(2))?; |
| 42 | assert_eq!(zoned.to_string(), "2024-08-10T23:14:00-04:00[America/New_York]"); |
| 43 | // Or, if you want an RFC3339 formatted string: |
| 44 | assert_eq!(zoned.timestamp().to_string(), "2024-08-11T03:14:00Z"); |
| 45 | Ok(()) |
| 46 | } |
| 47 | ``` |
| 48 | |
| 49 | There are many more examples in the [documentation](https://docs.rs/jiff). |
| 50 | |
| 51 | ### Usage |
| 52 | |
| 53 | Jiff is [on crates.io](https://crates.io/crates/jiff) and can be |
| 54 | used by adding `jiff` to your dependencies in your project's `Cargo.toml`. |
| 55 | Or more simply, just run `cargo add jiff`. |
| 56 | |
| 57 | Here is a complete example that creates a new Rust project, adds a dependency |
| 58 | on `jiff`, creates the source code for a simple datetime program and then runs |
| 59 | it. |
| 60 | |
| 61 | First, create the project in a new directory: |
| 62 | |
| 63 | ```text |
| 64 | $ cargo new jiff-example |
| 65 | $ cd jiff-example |
| 66 | ``` |
| 67 | |
| 68 | Second, add a dependency on `jiff`: |
| 69 | |
| 70 | ```text |
| 71 | $ cargo add jiff |
| 72 | ``` |
| 73 | |
| 74 | Third, edit `src/main.rs`. Delete what's there and replace it with this: |
| 75 | |
| 76 | ```rust |
| 77 | use jiff::{Unit, Zoned}; |
| 78 | |
| 79 | fn main() -> Result<(), jiff::Error> { |
| 80 | let now = Zoned::now().round(Unit::Second)?; |
| 81 | println!("{now}"); |
| 82 | Ok(()) |
| 83 | } |
| 84 | ``` |
| 85 | |
| 86 | Fourth, run it with `cargo run`: |
| 87 | |
| 88 | ```text |
| 89 | $ cargo run |
| 90 | Compiling jiff v0.2.0 (/home/andrew/rust/jiff) |
| 91 | Compiling jiff-play v0.2.0 (/home/andrew/tmp/scratch/rust/jiff-play) |
| 92 | Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.37s |
| 93 | Running `target/debug/jiff-play` |
| 94 | 2024-07-10T19:54:20-04:00[America/New_York] |
| 95 | ``` |
| 96 | |
| 97 | The first time you run the program will show more output like above. But |
| 98 | subsequent runs shouldn't have to re-compile the dependencies. |
| 99 | |
| 100 | ### Crate features |
| 101 | |
| 102 | Jiff has several crate features for customizing support for Rust's standard |
| 103 | library, `serde` support and whether to embed a copy of the Time Zone Database |
| 104 | into your binary. |
| 105 | |
| 106 | The "[crate features](https://docs.rs/jiff/#crate-features)" section of the |
| 107 | documentation lists the full set of supported features. |
| 108 | |
| 109 | ### Future plans |
| 110 | |
| 111 | With `jiff 0.2` out about 6 months after the `jiff 0.1` initial release, my |
| 112 | plan remains roughly the same as it started. That is, I'd still like to get a |
| 113 | `jiff 1.0` release out this summer 2025 (in about 6 months) and then commit to |
| 114 | it indefinitely. This plan may change if something critically wrong is found |
| 115 | with the current API. |
| 116 | |
| 117 | The purpose of this plan is to get Jiff to a 1.0 stable state as quickly as |
| 118 | possible. The reason is so that others feel comfortable relying on Jiff as |
| 119 | a public dependency that won't cause ecosystem churn. |
| 120 | |
| 121 | ### Performance |
| 122 | |
| 123 | The most important design goal of Jiff is to be a high level datetime library |
| 124 | that makes it hard to do the wrong thing. Second to that is performance. Jiff |
| 125 | should have reasonable performance, but there are likely areas in which it |
| 126 | could improve. See the `bench` directory for benchmarks. |
| 127 | |
| 128 | Note that performance is still an important goal. Some aspects of Jiff have |
| 129 | had optimization attention paid to them, but many still have not. It is a goal |
| 130 | to improve where we can, but performance will generally come second to API |
| 131 | comprehension and correctness. |
| 132 | |
| 133 | ### Platform support |
| 134 | |
| 135 | The question of platform support in the context of datetime libraries comes up |
| 136 | primarily in relation to time zone support. Specifically: |
| 137 | |
| 138 | * How should Jiff determine the time zone transitions for an IANA time zone |
| 139 | identifier like `Antarctica/Troll`? |
| 140 | * How should Jiff determine the default time zone for the current system? |
| 141 | |
| 142 | Both of these require some level of platform interaction. |
| 143 | |
| 144 | For discovering time zone transition data, Jiff relies on the |
| 145 | [IANA Time Zone Database]. On Unix systems, this is usually found at |
| 146 | `/usr/share/zoneinfo`, although it can be configured via the `TZDIR` |
| 147 | environment variable (which Jiff respects). On Windows, Jiff will automatically |
| 148 | embed a copy of the time zone database into the compiled library. |
| 149 | |
| 150 | For discovering the system time zone, Jiff reads `/etc/localtime` on Unix. On |
| 151 | Windows, Jiff reads the Windows-specific time zone identifier via |
| 152 | [`GetDynamicTimeZoneInformation`] and then maps it to an IANA time zone |
| 153 | identifier via Unicode's [CLDR XML data]. |
| 154 | |
| 155 | I expect Jiff to grow more support for other platforms over time. Please file |
| 156 | issues, although I will likely be reliant on contributor pull requests for more |
| 157 | obscure platforms that aren't easy for me to test. |
| 158 | |
| 159 | For more on platform support, see [`PLATFORM.md`](PLATFORM.md). |
| 160 | |
| 161 | [IANA Time Zone Database]: https://en.wikipedia.org/wiki/Tz_database |
| 162 | [`GetDynamicTimeZoneInformation`]: https://learn.microsoft.com/en-us/windows/win32/api/timezoneapi/nf-timezoneapi-getdynamictimezoneinformation |
| 163 | [CLDR XML data]: https://github.com/unicode-org/cldr/raw/main/common/supplemental/windowsZones.xml |
| 164 | |
| 165 | ### Dependencies |
| 166 | |
| 167 | At time of writing, it is no accident that Jiff has zero dependencies on Unix. |
| 168 | In general, my philosophy on adding new dependencies in an ecosystem crate like |
| 169 | Jiff is very conservative. I consider there to be two primary use cases for |
| 170 | adding new dependencies: |
| 171 | |
| 172 | 1. When a dependency is _practically_ required in order to interact with a |
| 173 | platform. For example, `windows-sys` for discovering the system time zone on |
| 174 | Windows. |
| 175 | 2. When a dependency is necessary for inter-operability. For example, `serde`. |
| 176 | But even here, I expect to be conservative, where I'm generally only willing |
| 177 | to depend on things that have fewer breaking change releases than Jiff. |
| 178 | |
| 179 | A secondary use case for new dependencies is if Jiff gets split into multiple |
| 180 | crates. I did a similar thing for the `regex` crate for very compelling |
| 181 | reasons. It is possible that will happen with Jiff as well, although there are |
| 182 | no plans for that. And in general, I expect the number of crates to stay small, |
| 183 | if only to make keep maintenance lightweight. (Managing lots of semver API |
| 184 | boundaries has a lot of overhead in my experience.) |
| 185 | |
| 186 | ### Minimum Rust version policy |
| 187 | |
| 188 | This crate's minimum supported `rustc` version is `1.70.0`. |
| 189 | |
| 190 | The policy is that the minimum Rust version required to use this crate can be |
| 191 | increased in minor version updates. For example, if jiff 1.0 requires Rust |
| 192 | 1.20.0, then jiff 1.0.z for all values of `z` will also require Rust 1.20.0 or |
| 193 | newer. However, jiff 1.y for `y > 0` may require a newer minimum version of |
| 194 | Rust. |