stringify_interval 0.1.0

Stringify an interval into human-friendly text.
Documentation
  • Coverage
  • 35%
    14 out of 40 items documented2 out of 7 items with examples
  • Size
  • Source code size: 45.73 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 4.15 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 18s Average build duration of successful builds.
  • all releases: 18s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Pulau-Komodo/stringify_interval
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • Pulau-Komodo

Generates a user-friendly string from a chrono::Duration, like "1 day, 5 hours and 20 minutes".

Years and months can be displayed, but they will need some date as a reference point, because the exact length of a year or month can vary.

It is fairly configurable.

The public API is very much up for debate and subject to change.

Usage

let text = stringify_interval::without_date(
	chrono::Duration::seconds(1_234_567),
	&DisplayConfigConstant::default(),
	&Text::default(),
);
assert_eq!(text, Ok(String::from("14 days, 6 hours and 56 minutes")));

Configuration

Each individual unit can have a range of values set for when it should show up. This allows it to do things like automatically drop seconds for long durations, or not mention years unless the interval includes 5 of them. When units are dropped, the interval is rounded to the nearest multiple of the smallest unit still displayed.

Each individual unit can also be padded with zeroes, or be set to display even when the value is 0.

Additionally, all the string elements can be changed out with the Text struct. This allows for formatting changes and for some degree of localisation. For each unit, a ThresholdMap allows setting for which number range which text should be displayed.

The default values for Text are as follows:

Text {
	years: ThresholdMap::from_iter("years", [(1, "year"), (2, "years")]).unwrap(),
	months: ThresholdMap::from_iter("months", [(1, "month"), (2, "months")]).unwrap(),
	weeks: ThresholdMap::from_iter("weeks", [(1, "week"), (2, "weeks")]).unwrap(),
	days: ThresholdMap::from_iter("days", [(1, "day"), (2, "days")]).unwrap(),
	hours: ThresholdMap::from_iter("hours", [(1, "hour"), (2, "hours")]).unwrap(),
	minutes: ThresholdMap::from_iter("minutes", [(1, "minute"), (2, "minutes")]).unwrap(),
	seconds: ThresholdMap::from_iter("seconds", [(1, "second"), (2, "seconds")]).unwrap(),
	joiner: ", ".into(),
	final_joiner: Some(" and ".into()),
	spacer: " ".into(),
}