//
// 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 time_tm = get_current_time_struct();
	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 str=strTime;
	return str;
}
std::string Time_tool::get_current_time_millisecond()
{

	auto now = std::chrono::system_clock::now();
	//通过不同精度获取相差的毫秒数
	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;
	std::string strTime=get_current_time_seconds()+" "+std::to_string((int)dis_millseconds);
	return strTime;
}
std::string Time_tool::get_current_time_microsecond()
{

	auto now = std::chrono::system_clock::now();
	//通过不同精度获取相差的微秒
	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;
	std::string strTime=get_current_time_millisecond()+" "+std::to_string((int)dis_microseconds);
	return strTime;
}
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;
	}
}
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;
	}
}
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;
	}
}
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;
	}
}
void Time_tool::clear_timer()
{
	Time_tool_uninit();
}
void Time_tool::Time_tool_uninit()
{
	timetool_map.clear();
}