BurntSushi / jiff

A date-time library for Rust that encourages you to jump into the pit of success.
The Unlicense
1.65k stars 26 forks source link

api: add options for controlling precision #114

Closed BurntSushi closed 3 weeks ago

BurntSushi commented 3 weeks ago

This PR introduces new options for controlling the precision of fractional seconds when printing Zoned, Timestamp, civil::DateTime or civil::Time values. This is principally exposed via jiff::fmt::temporal::DateTimePrinter::precision, but it's also available via the standard library's formatting machinery. For example, if zdt is a jiff::Zoned, then format!("{zdt:.6}") will format it into a string with microsecond precision, even if its fractional component is zero.

This is useful when one wants a datetime to use a "fixed width" format. Or at least, as close to one as possible. For Zoned in particular, a fixed width format is somewhat difficult to accomplish because of the variable length IANA time zone identifier. But if the time zone identifier is the same for all Zoned values in a particular context, then setting the precision will provide fixed width. (Unless the years are negative.)

Closes #92

BurntSushi commented 3 weeks ago

This PR is on crates.io in jiff 0.1.9.