#formatting #human-friendly #approximate #human-readable #style #tiny #format

folktime

一个用于以人类友好的方式近似格式化时间值的微小库

3 个不稳定版本

0.2.1 2024 年 5 月 11 日
0.2.0 2024 年 5 月 9 日
0.1.0 2024 年 5 月 9 日

#78 in 值格式化

Download history 238/week @ 2024-05-03 505/week @ 2024-05-10 282/week @ 2024-05-17 130/week @ 2024-05-24 20/week @ 2024-05-31 39/week @ 2024-06-07 42/week @ 2024-06-14 21/week @ 2024-06-21 13/week @ 2024-06-28 44/week @ 2024-07-05 36/week @ 2024-07-12 27/week @ 2024-07-19 165/week @ 2024-07-26 51/week @ 2024-08-02 104/week @ 2024-08-09 128/week @ 2024-08-16

452 每月下载量
用于 2 crate

MIT/Apache

61KB
1.5K SLoC

folktime

Build status Crates.io

一个用于以人类友好的方式近似格式化 std::time::Duration 的微小库。

如果您正在寻找完整的精度人类可读格式,请查看 humantime

使用方法

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

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

精度

格式化仅显示最显著的数字

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");

格式化样式

有几种格式化样式

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");

以下是样式的比较

持续时间 样式::OneUnitFrac 样式::OneUnitWhole 样式::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
最大 584Gy 584Gy 584Gy 4mo

无运行时依赖