Pet*_*nak 6 time strftime rust
如何在不带前导零的情况下打印日期/时间?例如,Jul 5, 9:15.
根据文档,它使用相同的语法strftime,但是抑制前导零
time::strftime("%b %-d, %-I:%M", &time::now()).unwrap()
Run Code Online (Sandbox Code Playgroud)
导致错误:
线程''恐慌'调用
Result::unwrap()一个Err值:InvalidFormatSpecifier(' - ')',../src/libcore/result.rs:746
我怀疑Rust不支持提供此标志的glibc扩展(以及其他几个); 但是没有前缀日期/时间的语法; alternative(%l)只是前缀空格,同样没用.
我可以手工创建字符串,但这会破坏函数的用途.
查看代码我们可以确认timecrate不支持该标志-.另请注意,timecrate是on rust-lang-deprecateduser,因此不推荐使用.
也就是说,我建议你使用chrono箱子.除了支持您想要的格式说明符外,chrono crate还支持时区等等.
let now = chrono::Utc::now();
println!("{}", now.format("%b %-d, %-I:%M").to_string());
Run Code Online (Sandbox Code Playgroud)