1 个不稳定版本

新版本 0.1.1 2024年8月7日

值格式化 中排名第118

Download history 122/week @ 2024-08-05

每月下载量 122

MIT/Apache

18KB
338 代码行

Jiff Humanize

快速开始

use jiff::{Local, Duration};
use jiffy::HumanTime;

let dt = jiff::Zoned::now() + Duration::days(35);
let ht = HumanTime::from(dt);
let english = format!("{}", ht);
assert_eq!("in a month", english);

lib.rs:

人类语言中jiff对象的表示

快速开始

HumanTime 对象由jiff对象创建,例如 jiff::DateTimejiff::Span

示例

将当前时间(now)转换为 HumanTime

let dt = jiff::Zoned::now();
let ht = jiffy::HumanTime::from(dt);

assert_eq!("now", format!("{}", ht));
let dt = jiff::Zoned::now().checked_sub(58.minutes()).unwrap()
let ht = jiffy::HumanTime::from(dt);

assert_eq!("an hour ago", format!("{}", ht));

要完全控制文本表示,请使用 HumanTime::to_text_en()

use jiff::ToSpan;;
use jiffy::{Accuracy, HumanTime, Tense};

let dt = 45.days();
let ht = HumanTime::from(dt);

assert_eq!("a month", ht.to_text_en(Accuracy::Rough, Tense::Present));
assert_eq!("1 month, 2 weeks and 1 day", ht.to_text_en(Accuracy::Precise, Tense::Present));

依赖项

~2.5MB
~30K SLoC