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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1650715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:46:27  来源:igfitidea点击:

Is there a standard date/time class in C++?

c++datetimestlstream

提问by Farid Z

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:

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

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 strftimeto 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

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

回答by jdehaan

Not part of STL but well known library is boost.

不是 STL 的一部分,但众所周知的库是 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.

我会去使用的方式boost::date。以下是一些示例: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 boostare header only (template libraries). However datetime requires a lib or dll.

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

回答by mkluwe

Nitpicking: The STL being the Standard TemplateLibrary deals with generic container and algorithms etc. and is unlikely to incorporate classes for date handling and calculation even in the future…

挑剔:作为标准模板库的 STL处理通用容器和算法等,即使在将来也不可能合并用于日期处理和计算的类......

The C++ Standard Library itself includes the STL and a previous version of the C standard library. The latter offers some date and time related functions via #include <ctime>which has already been mentioned above.

C++ 标准库本身包括 STL 和 C 标准库的早期版本。后者提供了一些与日期和时间相关的功能#include <ctime>,上面已经提到过这些功能。

If wrapping (or simply using) these functions is sufficient (and quicker) than pulling in boost, go with these. There is nothing wrong with them.

如果包装(或简单地使用)这些函数比拉入boost就足够(并且更快),请使用这些。他们没有任何问题。

回答by zahir

There are get_timeand put_timein <iomanip> header (i guess these came with C++11) which effectively does string formatting or parsing jobs.

<iomanip> 标头中有get_timeput_time(我猜这些是 C++11 附带的),它们有效地执行字符串格式化或解析作业。

回答by Farid Z

OK. Here is closest I have found about directly writing time to a stream:

好的。这是我发现的关于直接将时间写入流的最接近的方法:

time_t t(time(NULL));   // current time
tm tm(*localtime(&t));  

std::locale loc("");    // current user locale
ostringstream sout;
const std::time_put<TCHAR> &tput =
    std::use_facet<std::time_put<TCHAR> >(loc);
tput.put(sout.rdbuf(), sout, _T('##代码##'), &tm, _T('x'));
sout << ends;

CString sTest(sout.str().c_str());

A very helpful guide is the Apache C++ Standard Library Reference Guide http://stdcxx.apache.org/doc/stdlibref/time-put.html#sec13

一个非常有用的指南是 Apache C++ 标准库参考指南 http://stdcxx.apache.org/doc/stdlibref/time-put.html#sec13

回答by bua

There is also a ctime(&time_t) method which outputs string (char*).

还有一个ctime(&time_t) 方法可以输出字符串 (char*)。

回答by bua

C++ now has the chrono libraries for date and time. This is documented on http://en.cppreference.com/w/cpp/chronoand http://www.cplusplus.com/reference/chrono/

C++ 现在有日期和时间的 chrono 库。这记录在http://en.cppreference.com/w/cpp/chronohttp://www.cplusplus.com/reference/chrono/