How to format std::chrono durations?

前端 未结 4 1765
名媛妹妹
名媛妹妹 2021-01-11 12:49

Is there a convenient way to format std::chrono::duration to a specified format?

std::chrono::high_resolution_clock::time_point now, then;
then          


        
4条回答
  •  借酒劲吻你
    2021-01-11 13:06

    One can use something like:

    #include 
    #include 
    
    //...
    
    auto c(timeInMicroSec.count());
    std::ostringstream oss;
    oss << std::setfill('0')          // set field fill character to '0'
        << (c % 1000000000) / 1000000 // format seconds
        << "::"
        << std::setw(3)               // set width of milliseconds field
        << (c % 1000000) / 1000       // format milliseconds
        << "::"
        << std::setw(3)               // set width of microseconds field
        << c % 1000;                  // format microseconds
    auto formatted(oss.str());
    

提交回复
热议问题