#human-readable #formatting #duration #human-friendly #approximate #format #format-time

folktime

Tiny library for approximate formatting of time values in a human-friendly way

3 unstable releases

0.2.1 May 11, 2024
0.2.0 May 9, 2024
0.1.0 May 9, 2024

#73 in Value formatting

Download history 47/week @ 2024-06-10 37/week @ 2024-06-17 19/week @ 2024-06-24 33/week @ 2024-07-01 26/week @ 2024-07-08 36/week @ 2024-07-15 63/week @ 2024-07-22 128/week @ 2024-07-29 49/week @ 2024-08-05 105/week @ 2024-08-12 132/week @ 2024-08-19 92/week @ 2024-08-26 86/week @ 2024-09-02 87/week @ 2024-09-09 64/week @ 2024-09-16 71/week @ 2024-09-23

316 downloads per month
Used in 2 crates

MIT/Apache

61KB
1.5K SLoC

folktime

Build status Crates.io

Tiny library for approximate formatting of std::time::Duration in a human-friendly way.

If you are looking for a full precision human readable format, take a look at humantime.

Usage

use std::time::Duration;
use folktime::Folktime;

let a = Folktime::duration(Duration::from_secs(5));
assert_eq!(format!("{}", a), "5.00s");

Precision

Formatting only shows the most significant digits:

use std::time::Duration;
use folktime::Folktime;

let a = Folktime::duration(Duration::new(0, 123_456_789));
let b = Folktime::duration(Duration::new(1, 123_456_789));
let c = Folktime::duration(Duration::new(12, 123_456_789));
let d = Folktime::duration(Duration::new(123, 123_456_789));

assert_eq!(format!("{}", a), "123ms");
assert_eq!(format!("{}", b), "1.12s");
assert_eq!(format!("{}", c), "12.1s");
assert_eq!(format!("{}", d), "2.05m");

Formatting styles

There are several styles for formatting:

use std::time::Duration;
use folktime::Folktime;
use folktime::duration::Style;

let a = Folktime::duration(Duration::new(0, 12_056_999));
let b = Folktime::duration(Duration::new(0, 12_056_999)).with_style(Style::OneUnitWhole);
let c = Folktime::duration(Duration::new(0, 12_056_999)).with_style(Style::TwoUnitsWhole);

assert_eq!(format!("{}", a), "12.0ms");
assert_eq!(format!("{}", b), "12ms");
assert_eq!(format!("{}", c), "12ms 56us");

Here's a comparison of styles:

Duration Style::OneUnitFrac Style::OneUnitWhole Style::TwoUnitsWhole
0s 0.00s 0s 0s 0ms
0.123456s 123ms 123ms 123ms 456us
1.123456s 1.12s 1s 1s 123ms
12.12345s 12.1s 12s 12s 123ms
123.1234s 2.05m 2m 2m 3s
86400s 1.00d 1d 1d 0h
12345678s 2.04w 2w 2w 0d
123456789s 4.69mo 4mo 4mo 21d
max 584Gy 584Gy 584Gy 4mo

No runtime deps