Why is there no C++11 threadsafe alternative to std::localtime and std::gmtime?

According to N2661, the paper that added <chrono>: This paper does not offer calendrical services except for a minimal mapping to and from C’s time_t. As this paper does not propose a date/time library, nor specify epochs, it also does not address leap seconds. However, a date/time library will find this to be an excellent … Read more

Get time since epoch in milliseconds, preferably using C++11 chrono

unsigned long milliseconds_since_epoch = std::chrono::system_clock::now().time_since_epoch() / std::chrono::milliseconds(1); although, especially since you want platform independence, it might be better to replace unsigned long with a type that’s more likely to be large enough: (unsigned) long long std::(u)int64_t std::chrono::milliseconds::rep auto To me, this clearly states both that you’re risking loss of precision (by analogy with integer division) … Read more

std::put_time implementation status in GCC?

See TODO extended iomanip manipulators std::get_time and std::put_time for gcc 4.8.0. See also Cross Platform way to get the time of day? claiming that is not implemented in 4.7.0. UPDATE: As the gcc developer Jonathan Wakely confirmed below: The std::get_time and std::put_time manipulators are still missing in gcc 4.9. UPDATE: Jonathan Wakely closed this ticket … Read more

How to convert std::chrono::time_point to calendar datetime string with fractional seconds?

If system_clock, this class have time_t conversion. #include <iostream> #include <chrono> #include <ctime> using namespace std::chrono; int main() { system_clock::time_point p = system_clock::now(); std::time_t t = system_clock::to_time_t(p); std::cout << std::ctime(&t) << std::endl; // for example : Tue Sep 27 14:21:13 2011 } example result: Thu Oct 11 19:10:24 2012 EDIT: But, time_t does not contain … Read more

Extract year/month/day etc. from std::chrono::time_point in C++

You can only extract this information from a system_clock::time_point. This is the only system-supplied clock that has a relationship with the civil calendar. Here is how to get the current time_point using this clock: system_clock::time_point now = system_clock::now(); You can then convert this to a time_t with: time_t tt = system_clock::to_time_t(now); Using the C library … Read more

How do you print a C++11 time_point?

(In this post I will omit std::chrono:: qualifications for clarity. I trust you know where they go.) The reason your code example fails to compile is that there is a mismatch between the return type of system_clock::now() and the type of variable you are trying to assign this to (time_point<system_clock, nanoseconds>). The documented return value … Read more

What is the difference between chrono::month and chrono::months

Yes, it can be confusing to have both month and months when first encountering this library. However there are consistent naming conventions in this library to help reduce that confusion. And the benefit is having a clear separation of distinct semantics while retaining short intuitive names. months All “predefined” chrono::duration types are plural: nanoseconds microseconds … Read more