How do you print a C++11 time_point?

后端 未结 7 1371
执笔经年
执笔经年 2020-12-07 19:47

I\'ve created a time point, but I have been struggling to print it to the terminal.

#include 
#include 

int main(){

    //set         


        
7条回答
  •  广开言路
    2020-12-07 20:39

    Updated answer for an old question:

    For a std::chrono::time_point there is now a 3rd party libraries that give you much better control. For time_points based on other clocks, there is still no better solution than to just get the internal representation and print it out.

    But for system_clock, using this library, this is as easy as:

    #include "date.h"
    #include 
    
    int
    main()
    {
        using namespace date;
        using namespace std::chrono;
        std::cout << system_clock::now() << " UTC\n";
    }
    

    which just output for me:

    2016-07-19 03:21:01.910626 UTC
    

    which is the current UTC date and time to microsecond precision. If on your platform system_clock::time_point has nanosecond precision, it will print out nanosecond precision for you.

提交回复
热议问题