C++ 中是否有标准的日期/时间类?

2022-01-07 00:00:00 datetime stream c++ stl

C++ stl 有标准时间类吗?或者我是否必须在写入流之前转换为 c 字符串.例如,我想将当前日期/时间输出到字符串流:

<前>time_t tm();ostringstream 南;南<

在这种情况下,我将当前日期/时间写成一个没有任何格式的数字.我可以用c- 运行时函数 strftime 首先格式化 tm,但如果 stl 具有可以从 time_t 值实例化的时间类,这似乎没有必要

解决方案

不是 STL 的一部分,但众所周知的库是 boost.

我会使用boost::date.以下是一些示例:http://www.boost.org/doc/libs/1_55_0/doc/html/date_time/date_time_io.html#date_time.io_tutorial.

如果你还没有尝试过 boost,我鼓励你这样做,因为它可以让你免于许多讨厌的问题,因为它掩盖了大多数操作系统相关的事情,例如线程.boost 中的很多东西都是头文件(模板库).但是 datetime 需要一个 lib 或 dll.

Does C++ stl have a standard time class? Or do I have to convert to c-string before writing to a stream. Example, I want to output the current date/time to a string stream:

time_t tm();
ostringstream sout;
sout << tm << ends;

In this case I get the current date/time written out as a number without any formatting. I can use c- runtime function strftime to format tm first, but that seems like it should not be necessary if the stl has a time class that can be instantiated from time_t value

解决方案

Not part of STL but well known library is boost.

I would go the way of using boost::date. Here are some examples: http://www.boost.org/doc/libs/1_55_0/doc/html/date_time/date_time_io.html#date_time.io_tutorial.

If you did not try out boost yet I encourage you to do so as it saves you from a lot of nasty issues, as it masks most OS dependent things like threading for example. Many things in boost are header only (template libraries). However datetime requires a lib or dll.

相关文章