开发者

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

开发者 https://www.devze.com 2022-12-10 01:30 出处:网络
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:

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.


EDIT

The standard "datetime" class is std::chrono::time_point since C++11. The code in the question should be roughly equivalent to

const auto now = std::chrono::system_clock::now();
const auto t_c = std::chrono::system_clock::to_time_t(now);
std::cout << std::put_time(std::localtime(&t_c), "%F %T.\n");

OLD ANSWER

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

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.

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


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


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


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


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('\0'), &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


Well, it's been a dozen years since this question was asked. And now (in C++20) it finally has a better answer.

Yes, there are several standard date/time classes in C++20 (not just one). Each serves different purposes in a strongly typed system. For example std::chrono::zoned_time represents a pairing of a std::chrono::time_zone and a std::chrono::time_point<system_clock, SomeDuration>, and represents the local time in some geographic area. Here is how you might create and print the local time in your current time zone to the finest sub-second precision your OS allows.

cout << zoned_time{current_zone(), system_clock::now()} << '\n';

If you need the local time somewhere else, that is just as easily obtained:

cout << zoned_time{"Africa/Casablanca", system_clock::now()} << '\n';

Unlike in previous C++ standards, time_points based on system_clock are now guaranteed to represent UTC, neglecting leap seconds (aka Unix Time). So to get the current time in UTC it is simply:

cout << system_clock::now() << '\n';

Though if you really wanted to use a zoned_time instead (for example the code may be generic), this also works:

cout << zoned_time{"UTC", system_clock::now()} << '\n';

See https://en.cppreference.com/w/cpp/chrono for many more standard date/time classes. All of them are thread-safe. And you are no longer limited to seconds precision.

0

精彩评论

暂无评论...
验证码 换一张
取 消