格式化 std::time 输出 [英] Format std::time output
问题描述
我想以某种格式显示当前时间.
I want to display the current time in a certain format.
我试图避免使用 time crate,因为它在 GitHub 存储库.
I am trying to avoid the time crate since it's flagged as deprecated on its the GitHub repo.
我想使用这个确切的格式 time::now().strftime("%Y-%m-%d][%H:%M:%S").unwrap()
使用 std::time
,但它似乎没有 strftime
.
I want to use this exact format time::now().strftime("%Y-%m-%d][%H:%M:%S").unwrap()
using std::time
, but it doesn't seems to have a strftime
.
推荐答案
(目前)有两种 now
方法:Instant::now
和 SystemTime::now
.
There are (currently) two now
methods: Instant::now
and SystemTime::now
.
Instant
说:
对单调递增时钟的测量.不透明且仅在 Duration
时有用.
A measurement of a monotonically increasing clock. Opaque and useful only with
Duration
.
SystemTime
说:
系统时钟的测量值,对于与文件系统或其他进程等外部实体进行通信非常有用.
A measurement of the system clock, useful for talking to external entities like the file system or other processes.
这些都不适合向人类展示.时间困难,格式化时间是额外的复杂性.不属于标准库确实是一件好事,否则它就会有一个无法改进的固定API.
Neither of these is truly appropriate for showing to a human. Time is hard, and formatting time is additional complexity. It's really a good thing that it's not part of the standard library, otherwise it would have a fixed API that couldn't be improved.
如其他地方所述,我建议使用 chrono,继承人 到 time
箱子.
As mentioned elsewhere, I'd recommend using chrono, the heir apparent to the time
crate.
这篇关于格式化 std::time 输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!