the new 2023,the elapsed time drives me more mature

//model/util.cpp

void util::time_demo()
{
    static uint64_t num=0;
    chrono::time_point<chrono::high_resolution_clock> now = chrono::high_resolution_clock::now();
    // cout << "nanos " << chrono::duration_cast<chrono::nanoseconds>(now.time_since_epoch()).count()
    //      << ",micros " << chrono::duration_cast<chrono::microseconds>(now.time_since_epoch()).count()
    //      << ",mills " << chrono::duration_cast<chrono::milliseconds>(now.time_since_epoch()).count()
    //      << ",seconds " << chrono::duration_cast<chrono::seconds>(now.time_since_epoch()).count() << endl;
    chrono::milliseconds mills = chrono::duration_cast<chrono::milliseconds>(now.time_since_epoch());
    chrono::seconds seconds = chrono::duration_cast<chrono::seconds>(now.time_since_epoch());
    uint64_t millsValue = mills.count() - seconds.count() * 1000;
    time_t rawTime = chrono::high_resolution_clock::to_time_t(now);
    struct tm tmInfo = *std::localtime(&rawTime);
    stringstream ss;
    ss << std::put_time(&tmInfo, "%Y%m%d%H%M%S") << std::setfill('0') << std::setw(3) << millsValue;
    string dtvalue = ss.str();
    ss = stringstream(std::string());
    cout<< dtvalue << endl;
}

void util::print_time()
{
    while (1)
    {
        time_demo();
        usleep(100000);
    }
}

//main.cpp
#include "model/util.h"

void print_time_demo()
{
util ul;
ul.print_time();
}
 

int main(int args,char **argv)
{
print_time_demo();
}
 

 

Compile

g++ -g -std=c++2a -I. *.cpp ./model/*.cpp -o h1 -luuid -lpthread -ljsoncpp

 

Run

./h1

 

posted @ 2023-01-01 17:06  FredGrit  阅读(15)  评论(0编辑  收藏  举报