How do you print a C++11 time_point?

后端 未结 7 1332
执笔经年
执笔经年 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:35

    The nanoseconds seems to be part of the problem, looking at the documentation a bit I was able to get this to work:

    #include 
    #include 
    #include 
    
    
    int main(){
    
        //set time_point to current time
        std::chrono::time_point time_point;
        time_point = std::chrono::system_clock::now();
    
        std::time_t ttp = std::chrono::system_clock::to_time_t(time_point);
        std::cout << "time: " << std::ctime(&ttp);
    
        return 0;
    }
    

    Although it looks like std::chrono::microseconds works ok:

    std::chrono::time_point time_point;
    

提交回复
热议问题