// // Created by wk on 2020/9/25. // #include "time_tool.h" Time_tool::Time_tool() { } Time_tool::~Time_tool() { Time_tool_uninit(); } void Time_tool::set_points_digits(int num) { std::cout.precision(num); std::cout.setf(std::ios::fixed); } std::chrono::system_clock::time_point Time_tool::get_system_point() { return std::chrono::system_clock::now(); } tm Time_tool::get_current_time_struct() { auto now = std::chrono::system_clock::now(); time_t tt = std::chrono::system_clock::to_time_t(now); tm time_tm=*localtime(&tt); return time_tm; } std::string Time_tool::get_current_time_seconds() { auto now = std::chrono::system_clock::now(); time_t tt = std::chrono::system_clock::to_time_t(now); tm time_tm=*localtime(&tt); char strTime[100] = ""; sprintf(strTime, "%d-%02d-%02d %02d:%02d:%02d", time_tm.tm_year + 1900, time_tm.tm_mon + 1, time_tm.tm_mday, time_tm.tm_hour, time_tm.tm_min, time_tm.tm_sec); std::string result=strTime; return result; } std::string Time_tool::get_current_time_millisecond() { auto now = std::chrono::system_clock::now(); time_t tt = std::chrono::system_clock::to_time_t(now); tm time_tm=*localtime(&tt); char strTime[100] = ""; sprintf(strTime, "%d-%02d-%02d %02d:%02d:%02d", time_tm.tm_year + 1900, time_tm.tm_mon + 1, time_tm.tm_mday, time_tm.tm_hour, time_tm.tm_min, time_tm.tm_sec); std::string result=strTime; //通过不同精度获取相差的毫秒数 uint64_t dis_millseconds = std::chrono::duration_cast(now.time_since_epoch()).count() - std::chrono::duration_cast(now.time_since_epoch()).count() * 1000; result = result+" "+std::to_string((int)dis_millseconds); return result; } std::string Time_tool::get_current_time_microsecond() { auto now = std::chrono::system_clock::now(); time_t tt = std::chrono::system_clock::to_time_t(now); tm time_tm=*localtime(&tt); char strTime[100] = ""; sprintf(strTime, "%d-%02d-%02d %02d:%02d:%02d", time_tm.tm_year + 1900, time_tm.tm_mon + 1, time_tm.tm_mday, time_tm.tm_hour, time_tm.tm_min, time_tm.tm_sec); std::string result=strTime; //通过不同精度获取相差的毫秒数 uint64_t dis_millseconds = std::chrono::duration_cast(now.time_since_epoch()).count() - std::chrono::duration_cast(now.time_since_epoch()).count() * 1000; //通过不同精度获取相差的微秒 uint64_t dis_microseconds = std::chrono::duration_cast(now.time_since_epoch()).count() - std::chrono::duration_cast(now.time_since_epoch()).count() * 1000; result = result+" "+std::to_string((int)dis_millseconds)+" "+std::to_string((int)dis_microseconds); return result; } std::string Time_tool::get_time_string_seconds(std::chrono::system_clock::time_point time_point) { time_t tt = std::chrono::system_clock::to_time_t(time_point); tm time_tm=*localtime(&tt); char strTime[100] = ""; sprintf(strTime, "%d-%02d-%02d %02d:%02d:%02d", time_tm.tm_year + 1900, time_tm.tm_mon + 1, time_tm.tm_mday, time_tm.tm_hour, time_tm.tm_min, time_tm.tm_sec); std::string result=strTime; return result; } //转化 时间 精确到秒 std::chrono::system_clock::time_point Time_tool::transform_time_string_seconds(std::string time_string) { std::chrono::system_clock::time_point t_time_point; time_t tt; tm time_tm; int t_year = 0; int t_mon = 0; sscanf(time_string.c_str(), "%d-%02d-%02d %02d:%02d:%02d", &t_year, &t_mon, &time_tm.tm_mday, &time_tm.tm_hour, &time_tm.tm_min, &time_tm.tm_sec); time_tm.tm_year = t_year -1900; time_tm.tm_mon = t_mon -1; time_tm.tm_isdst = 0; // std::cout << " huli test :::: " << " t_year = " << t_year << std::endl; // std::cout << " huli test :::: " << " t_mon = " << t_mon << std::endl; // std::cout << " huli test :::: " << " time_tm.tm_year = " << time_tm.tm_year << std::endl; // std::cout << " huli test :::: " << " time_tm.tm_mon = " << time_tm.tm_mon << std::endl; // std::cout << " huli test :::: " << " time_tm.tm_mday = " << time_tm.tm_mday << std::endl; // std::cout << " huli test :::: " << " time_tm.tm_hour = " << time_tm.tm_hour << std::endl; // std::cout << " huli test :::: " << " time_tm.tm_min = " << time_tm.tm_min << std::endl; // std::cout << " huli test :::: " << " time_tm.tm_sec = " << time_tm.tm_sec << std::endl; tt = mktime(&time_tm); t_time_point = std::chrono::system_clock::from_time_t(tt); // std::cout << " huli test :::: " << " time_tm.tt = " << tt << std::endl; // std::cout << " huli test :::: " << " t_time_point = " << Time_tool::get_instance_references().get_time_string_seconds(t_time_point) << std::endl; return t_time_point; } std::string Time_tool::get_time_string_millisecond(std::chrono::system_clock::time_point time_point) { time_t tt = std::chrono::system_clock::to_time_t(time_point); tm time_tm=*localtime(&tt); char strTime[100] = ""; sprintf(strTime, "%d-%02d-%02d %02d:%02d:%02d", time_tm.tm_year + 1900, time_tm.tm_mon + 1, time_tm.tm_mday, time_tm.tm_hour, time_tm.tm_min, time_tm.tm_sec); std::string result=strTime; //通过不同精度获取相差的毫秒数 uint64_t dis_millseconds = std::chrono::duration_cast(time_point.time_since_epoch()).count() - std::chrono::duration_cast(time_point.time_since_epoch()).count() * 1000; result = result+" "+std::to_string((int)dis_millseconds); return result; } std::string Time_tool::get_time_string_microsecond(std::chrono::system_clock::time_point time_point) { time_t tt = std::chrono::system_clock::to_time_t(time_point); tm time_tm=*localtime(&tt); char strTime[100] = ""; sprintf(strTime, "%d-%02d-%02d %02d:%02d:%02d", time_tm.tm_year + 1900, time_tm.tm_mon + 1, time_tm.tm_mday, time_tm.tm_hour, time_tm.tm_min, time_tm.tm_sec); std::string result=strTime; //通过不同精度获取相差的毫秒数 uint64_t dis_millseconds = std::chrono::duration_cast(time_point.time_since_epoch()).count() - std::chrono::duration_cast(time_point.time_since_epoch()).count() * 1000; //通过不同精度获取相差的微秒 uint64_t dis_microseconds = std::chrono::duration_cast(time_point.time_since_epoch()).count() - std::chrono::duration_cast(time_point.time_since_epoch()).count() * 1000; result = result+" "+std::to_string((int)dis_millseconds)+" "+std::to_string((int)dis_microseconds); return result; } void Time_tool::time_start(int key) { timetool_map[key].t_time_start=get_system_point();//保存开始的时间 //单位为微秒 } void Time_tool::time_end(int key) { if ( timetool_map.find(key)!=timetool_map.end() ) { timetool_map[key].t_time_end = get_system_point();//保存结束的时间 timetool_map[key].t_time_difference = timetool_map[key].t_time_end - timetool_map[key].t_time_start;//保存时差 } else { std::cout << "计时器:" << key<<"还未开始"<