New Rust library offers simple and secure date and time functions

The new Rust library Jiff simplifies the handling of date and time under Rust, including international time zones and daylight saving time.

Save to Pocket listen Print view

(Image: iX/mit KI)

2 min. read
This article was originally published in German and has been automatically translated.

Jiff, a new datetime library for Rust, simplifies and accelerates working with dates and times. The focus of Jiff is on secure, unambiguous use of date and time types as well as high performance.

According to the initiator BurntSushi, the library provides "daytime primitives" and supports the international time zone database as well as summer (DST, Daylight Saving Time) and winter time. It offers functions for rounding, formatting and parsing. If required, Serde can be integrated to serialize formats. The Temporal library for JavaScript serves as a model for Jiff.

Jiff supports Unix, where the time zone table can be found under /usr/share/zoneinfo, and Windows, where Jiff integrates its own zone database. To determine the system time, Jiff reads /etc/localtime (Unix) or GetDynamicTimeZoneInformation (Windows). The author hopes that other platforms will follow, but says that he is dependent on contributors here. His primary goal is "to get Jiff to a stable 1.0 level as quickly as possible. The reason for this is so that others can rely on Jiff as a public dependency ..."

The heise conference on Rust

The betterCode() Rust 2024 will take place on November 5. The fourth edition of the online conference organized by iX and dpunkt.verlag will focus on the topic of embedded development. The betterCode() Rust 2024 shows how to develop with Rust for microcontrollers, what advantages the language has for safety-critical systems in particular and how to integrate unavoidable unsafe code sections as securely as possible.

The conference program offers presentations on the following topics:

  • Embedded development with Rust: Combining safety and performance
  • Asynchronous Rust: More efficient embedded development
  • Unsafe for Work
  • Between man and machine: Embedded UI
  • Rust in the automotive sector
  • Safe and Open: Rust and RISC-V

Tickets for the conference are available until October 8 at the early bird price of 229 euros (all prices plus VAT).

The following example shows how developers parse a typical RFC 3339 timestamp, convert it to a zone-dependent datetime, add a time span to it and output it losslessly at the end:

use jiff::{Timestamp, ToSpan};

fn main() -> Result<(), jiff::Error> {
    let time: Timestamp = "2024-07-11T01:14:00Z".parse()?;
    let zoned = time.intz("America/New_York")?.checked_add(1.month().hours(2))?;
    assert_eq!(zoned.to_string(), "2024-08-10T23:14:00-04:00[America/New_York]");
    // Or, if you want an RFC3339 formatted string:
    assert_eq!(zoned.timestamp().to_string(), "2024-08-11T03:14:00Z");
    Ok(())
}

Users find Jiff on Crates.io and add it to Cargo.toml with cargo add jiff. It is under MIT- and the Unlicence. The minimum required Rust version is 1.70.0. BurntSushi's GitHub page also documents a comparison with other Rust time functions.

(who)