123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- //
- // 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<std::chrono::milliseconds>(now.time_since_epoch()).count()
- - std::chrono::duration_cast<std::chrono::seconds>(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<std::chrono::milliseconds>(now.time_since_epoch()).count()
- - std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()).count() * 1000;
- //通过不同精度获取相差的微秒
- uint64_t dis_microseconds = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count()
- - std::chrono::duration_cast<std::chrono::milliseconds>(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<std::chrono::milliseconds>(time_point.time_since_epoch()).count()
- - std::chrono::duration_cast<std::chrono::seconds>(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<std::chrono::milliseconds>(time_point.time_since_epoch()).count()
- - std::chrono::duration_cast<std::chrono::seconds>(time_point.time_since_epoch()).count() * 1000;
- //通过不同精度获取相差的微秒
- uint64_t dis_microseconds = std::chrono::duration_cast<std::chrono::microseconds>(time_point.time_since_epoch()).count()
- - std::chrono::duration_cast<std::chrono::milliseconds>(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<<"还未开始"<<std::endl;
- }
- }
- void Time_tool::cout_time_seconds(int key)
- {
- if ( timetool_map.find(key)!=timetool_map.end() )
- {
- double dieoutTime=(double)timetool_map[key].t_time_difference.count()/1000000000;
- std::cout << "计时器:"<<key<<" 计时的时间为:" <<dieoutTime<<" 秒" << std::endl;
- }
- else
- {
- std::cout<<"没有此计时器:"<<key<<std::endl;
- }
- }
- double Time_tool::get_time_seconds(int key)
- {
- if ( timetool_map.find(key)!=timetool_map.end() )
- {
- double dieoutTime=(double)timetool_map[key].t_time_difference.count()/1000000000;
- return dieoutTime;
- }
- else
- {
- return 0;
- }
- }
- void Time_tool::cout_time_millisecond(int key)
- {
- if ( timetool_map.find(key)!=timetool_map.end() )
- {
- double dieoutTime=(double)timetool_map[key].t_time_difference.count()/1000000;
- std::cout << "计时器:"<<key<<" 计时的时间为:" <<dieoutTime<<" 毫秒" << std::endl;
- }
- else
- {
- std::cout<<"没有此计时器:"<<key<<std::endl;
- }
- }
- double Time_tool::get_time_millisecond(int key)
- {
- if ( timetool_map.find(key)!=timetool_map.end() )
- {
- double dieoutTime=(double)timetool_map[key].t_time_difference.count()/1000000;
- return dieoutTime;
- }
- else
- {
- return 0;
- }
- }
- void Time_tool::cout_time_microsecond(int key)
- {
- if ( timetool_map.find(key)!=timetool_map.end() )
- {
- double dieoutTime=(double)timetool_map[key].t_time_difference.count()/1000;
- std::cout << "计时器:"<<key<<" 计时的时间为:" <<dieoutTime<<" 微秒" << std::endl;
- }
- else
- {
- std::cout<<"没有此计时器:"<<key<<std::endl;
- }
- }
- double Time_tool::get_time_microsecond(int key)
- {
- if ( timetool_map.find(key)!=timetool_map.end() )
- {
- double dieoutTime=(double)timetool_map[key].t_time_difference.count()/1000;
- return dieoutTime;
- }
- else
- {
- return 0;
- }
- }
- void Time_tool::cout_time_nanosecond(int key)
- {
- if ( timetool_map.find(key)!=timetool_map.end() )
- {
- std::cout << "计时器:"<<key<<" 计时的时间为:" <<timetool_map[key].t_time_difference.count()<<" 纳秒" << std::endl;
- }
- else
- {
- std::cout<<"没有此计时器:"<<key<<std::endl;
- }
- }
- double Time_tool::get_time_nanosecond(int key)
- {
- if ( timetool_map.find(key)!=timetool_map.end() )
- {
- double dieoutTime=(double)timetool_map[key].t_time_difference.count();
- return dieoutTime;
- }
- else
- {
- return 0;
- }
- }
- void Time_tool::clear_timer()
- {
- Time_tool_uninit();
- }
- void Time_tool::Time_tool_uninit()
- {
- timetool_map.clear();
- }
|