A datetime library for Rust
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
A datetime library for Rust
Jiff is a datetime library for Rust that encourages you to jump into the pit of success. Jiff automatically handles many common problems related to daylight saving time by putting time zone handling front and center into the API. Jiff has already seen widespread adoption in the Rust ecosystem. Jiff is pronounced like “gif” with a soft “g,” as in “gem.” This blog announces the Jiff 1.0 release by giving a tour of its API. Target audience: Rust programmers who need convenient datetime handling.
Jiff 是一个 Rust 的日期时间库,旨在引导你走向成功。通过将时区处理置于 API 的核心位置,Jiff 自动处理了许多与夏令时相关的常见问题。Jiff 在 Rust 生态系统中已经得到了广泛应用。Jiff 的发音类似于“gif”,其中的“g”发软音,如“gem”。本篇博客通过介绍其 API 来宣布 Jiff 1.0 版本的发布。目标受众:需要便捷日期时间处理的 Rust 程序员。
The Jiff 1.0 Commitment
Jiff is already used in production in many projects: uv, Cargo, Starship, fd, Nushell, mise, Jujutsu, uutils/coreutils and even as the basis for a Temporal implementation in AWS LLRT. It is time to call it 1.0. Unless some significant defect is found in Jiff’s API, my plan is to commit to Jiff 1.0 indefinitely. This is important because the intent is for Jiff to be an ecosystem building block that appears in the public API of other libraries. This makes Jiff an attractive public dependency, which means semver incompatible releases are likely to cause significant pain in the ecosystem. They will not be made lightly. To see a list of breaking changes between Jiff 0.2 and Jiff 1.0, please consult the CHANGELOG.
Jiff 1.0 的承诺
Jiff 已经在许多项目中投入生产使用:uv、Cargo、Starship、fd、Nushell、mise、Jujutsu、uutils/coreutils,甚至作为 AWS LLRT 中 Temporal 实现的基础。现在是时候将其称为 1.0 版本了。除非在 Jiff 的 API 中发现重大缺陷,否则我的计划是无限期地致力于 Jiff 1.0。这一点很重要,因为 Jiff 的初衷是成为一个生态系统的构建模块,出现在其他库的公共 API 中。这使得 Jiff 成为一个具有吸引力的公共依赖项,这意味着不兼容语义化版本(semver)的发布可能会给生态系统带来巨大的痛苦。因此,此类发布不会轻易做出。若要查看 Jiff 0.2 和 Jiff 1.0 之间的重大变更列表,请查阅 CHANGELOG。
Introduction
In programming, a datetime library’s core functionality rests at the intersection of a computer system’s concept of time and a human’s concept of time. The prevailing notion of system time is Unix time. A human’s concept of time is far more complex, but the most popular such model is the Gregorian calendar coupled with either a 12-hour or 24-hour clock. At the core of every datetime library rests some routine for converting between these concepts. A datetime library typically provides much more than this. Indeed, datetime handling has historically been very difficult. My own belief is that the difficulty comes from poor modeling of the domain. Instead of encapsulating the complexity inside a reusable bundle of code, many datetime libraries pass some or all of the complexity on to its users. Jiff instead tries hard to encapsulate the complexity and make it difficult for users to do something wrong. (Jiff is not the first to do this!) In this section of the blog, we’ll do some very basic introductions.
简介
在编程中,日期时间库的核心功能位于计算机系统的时间概念与人类的时间概念的交汇处。系统时间的通用概念是 Unix 时间。人类的时间概念要复杂得多,但最流行的模型是格里高利历(公历)结合 12 小时制或 24 小时制时钟。每个日期时间库的核心都包含一些用于在这些概念之间进行转换的例程。日期时间库通常提供的远不止这些。事实上,日期时间处理在历史上一直非常困难。我个人认为,这种困难源于对该领域建模的不佳。许多日期时间库没有将复杂性封装在可重用的代码包中,而是将部分或全部复杂性转嫁给了用户。相反,Jiff 努力封装这些复杂性,并使用户难以犯错。(Jiff 并不是第一个这样做的库!)在本节博客中,我们将进行一些非常基础的介绍。
Follow Along
To follow along with the examples in this blog, create a new Cargo project with dependencies on jiff and anyhow:
mkdir jiff-playground
cd jiff-playground
cargo init --bin
cargo add jiff anyhow
$EDITOR src/main.rs
Most examples are a complete self-contained Rust program. Just copy the code and replace whatever is in src/main.rs. Then run the program: cargo run
跟随操作
要跟随本博客中的示例进行操作,请创建一个新的 Cargo 项目并添加 jiff 和 anyhow 依赖:
mkdir jiff-playground
cd jiff-playground
cargo init --bin
cargo add jiff anyhow
$EDITOR src/main.rs
大多数示例都是完整的、自包含的 Rust 程序。只需复制这些代码并替换 src/main.rs 中的内容即可。然后运行程序:cargo run
Terms
I am loath to present a dictionary. However, the datetime domain is littered with confusable terms, so I think it’s important to orient ourselves on a shared vocabulary. I’ve kept this section as small as possible. (Some terms, like “time zone,” are omitted here because they are best defined through exploration in the sections that follow.) Civil time: The time you see on your clock. And in general, the time that the humans in your approximate geographic vicinity also see. That is, civil time is a human coordinated agreement for communicating time in a particular geographic region. Civil time is also known as: local time, plain time, naive time, clock time and others. UTC: An abbreviation for Coordinated Universal Time; it is a time standard that serves as the basis for civil time around the world. It was originally intended to roughly correspond to mean solar time. TAI: An abbreviation for International Atomic Time; it is a time standard as measured by atomic clocks. It isn’t directly connected to mean solar time. Leap second: An adjustment to TAI to bring it closer to mean solar time and thus forming the basis of UTC. Unix time: UTC, but ignoring leap seconds. TAI and leap seconds won’t be a feature of this blog, but they are listed here in order to convey the difference between Unix time and UTC.
术语
我不愿罗列词典,但日期时间领域充斥着容易混淆的术语,因此我认为建立共享词汇表非常重要。我已尽可能精简本节。(一些术语,如“时区”,此处省略,因为它们最好在后续章节的探索中定义。) 民用时间(Civil time):你在时钟上看到的时间。通常也是你所在地理位置附近的人们所看到的时间。也就是说,民用时间是人类在特定地理区域内沟通时间的一种协调协议。民用时间也称为:本地时间、普通时间、朴素时间、时钟时间等。 UTC:协调世界时的缩写;它是一种作为全球民用时间基础的时间标准。它最初旨在大致对应平均太阳时。 TAI:国际原子时的缩写;它是一种由原子钟测量的时间标准。它与平均太阳时没有直接联系。 闰秒:对 TAI 的一种调整,使其更接近平均太阳时,从而构成 UTC 的基础。 Unix 时间:忽略闰秒的 UTC。TAI 和闰秒不会成为本博客的主题,但在此列出是为了传达 Unix 时间与 UTC 之间的区别。
The DateTime Onion
As a way to transition into some of the core types of Jiff, I’ve come to think of its datetime types like an onion. In reverse order, we can build up its layers all the way to a ZonedDateTime. So let’s start with more terms, but this time, they are also types in Jiff:
A civil::Date is a plain Gregorian date, unattached to any specific location.
A civil::Time is a plain time on a clock, to nanosecond precision, unattached to any specific location.
A civil::DateTime combines a civil::Date and a civil::Time to provide both a Gregorian date and a time on a clock. It is also unattached to any specific location.
A Timestamp is an unambiguous specific instant in time regardless of geographic location, represented as the number of nanoseconds since the Unix epoch.
A TimeZone is a set of rules for mapping a Timestamp to a civil::DateTime, and vice versa, in a specific geographic region.
A ZonedDateTime combines a Timestamp, a civil::DateTime and a TimeZone to represent a specific instant in time in a specific geographic location.
To make this a bit more concrete, we can see what this looks like in code. Since a time zone can map from a civil datetime to an instant in time, one way to create a zoned datetime requires only a civil datetime and a time zone:
use jiff::civil;
fn main() -> anyhow::Result<()> {
let date = civil::date(2026, 11, 1);
let time = civil::time(17, 30, 0, 0);
let datetime = date.to_datetime(time);
// zdt is an initialism for "ZonedDateTime"
let zdt = datetime.in_tz("America/New_York")?;
assert_eq!(zdt.
日期时间“洋葱”
为了过渡到 Jiff 的一些核心类型,我开始将它的日期时间类型想象成一个洋葱。我们可以按相反的顺序,一层层构建直到 ZonedDateTime。让我们从更多的术语开始,但这一次,它们也是 Jiff 中的类型:
civil::Date 是一个普通的公历日期,不附属于任何特定地点。
civil::Time 是时钟上的普通时间,精确到纳秒,不附属于任何特定地点。
civil::DateTime 结合了 civil::Date 和 civil::Time,同时提供公历日期和时钟时间。它也不附属于任何特定地点。
Timestamp 是一个明确的特定时间瞬间,与地理位置无关,表示为自 Unix 纪元以来的纳秒数。
TimeZone 是一组规则,用于在特定地理区域内将 Timestamp 映射到 civil::DateTime,反之亦然。
ZonedDateTime 结合了 Timestamp、civil::DateTime 和 TimeZone,以表示特定地理位置的特定时间瞬间。
为了更具体一点,我们可以看看它在代码中是什么样子。由于时区可以将民用日期时间映射到时间瞬间,创建带时区日期时间的一种方法只需要一个民用日期时间和时区:
use jiff::civil;
fn main() -> anyhow::Result<()> {
let date = civil::date(2026, 11, 1);
let time = civil::time(17, 30, 0, 0);
let datetime = date.to_datetime(time);
// zdt 是 "ZonedDateTime" 的首字母缩写
let zdt = datetime.in_tz("America/New_York")?;
assert_eq!(zdt.