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_point
s 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.
Example:
Let's say you wanted to print out your local time 24 hours ago.
Well the first thing is to decide if you mean 24 physical hours, or you want the same time-of-day yesterday as it is now according to your local clock. These two things are almost always the same. But if you've experienced going onto, or coming off of daylight saving in the last 24 hours, they're different.
C++20 chrono can do both, and it is quite easy either way:
A. This is how you compute exactly 24 physical hours before now:
auto timeNow = system_clock::now();
auto timeYesterday = current_zone()->to_local(timeNow - 24h);
std::cout << "24 hours ago, the local time was " << timeYesterday << '\n';
- Get the current time UTC.
- Subtract 24h from the current time.
- Convert the UTC time to your local time.
- Print it out.
B. If you instead want the date/time which has the same local time yesterday as the local time is now, that is a very slight adjustment to the above code:
auto timeYesterday = current_zone()->to_local(timeNow) - 24h;
- Convert to local time prior to doing the subtraction, and then do the arithmetic in local time as opposed to UTC.
As stated before, these two algorithms nearly always give the same result. But every once in a while, and for some time zones, they can give different answers. So it is good to know what you want, and have the flexibility to easily program what you want. And to have the code reflect your decision when other programmers are reading it.
Finally, instead of subtracting 24h
, one could also use days{1}
or 86'400s
, or any other unit which indicates the desired passage of time. The only difference is readability.