Get millisecond part of time

后端 未结 8 1914
再見小時候
再見小時候 2020-12-18 22:07

I need to get milliseconds from the timer

    // get timer part
    time_t timer = time(NULL);
    struct tm now = *localtime( &timer );
    char timesta         


        
8条回答
  •  有刺的猬
    2020-12-18 22:26

    #include 
    
    typedef std::chrono::system_clock Clock;
    
    auto now = Clock::now();
    auto seconds = std::chrono::time_point_cast(now);
    auto fraction = now - seconds;
    time_t cnow = Clock::to_time_t(now);
    

    Then you can print out the time_t with seconds precision and then print whatever the fraction represents. Could be milliseconds, microseconds, or something else. To specifically get milliseconds:

    auto milliseconds = std::chrono::duration_cast(fraction);
    std::cout << milliseconds.count() << '\n';
    

提交回复
热议问题