#include "logic.h"
#include "cprecisetimeex.h"


CPreciseTimeEx::CPreciseTimeEx(void)
{
	ClearTime();
	m_bStart = true;
	QueryPerformanceCounter(&t1);
}

CPreciseTimeEx::~CPreciseTimeEx(void)
{
}

double CPreciseTimeEx::GetTime()
{
	//如果暂停了，时间不更新
	if(false == m_bStart)
	{
		return m_lastTime;
	}

	QueryPerformanceFrequency(&tc);
	if (!tc.QuadPart)
	{
		if(m_lastTime > 0.1)
			return m_lastTime;

		return 0;
	}
	if (m_bStart)
	{
		QueryPerformanceCounter(&t2);
	}

	return float(t2.QuadPart-t1.QuadPart)/tc.QuadPart/1000;
}

void CPreciseTimeEx::StartTime()
{
	//继续开始
	ResumeTime();
}

void CPreciseTimeEx::StopTime()
{
	PauseTime();
}

void CPreciseTimeEx::PauseTime()
{
	if(m_bStart)
	{
		m_lastTime = GetTime();

		double dTime = m_lastTime;
		ClearTime();
		m_lastTime = dTime;
		m_bStart = false;
	}
}

QString CPreciseTimeEx::FormatTime()
{
	double dTime = GetTime();

	int iHour = dTime/3600;
	int dLeast = int(dTime - iHour*3600);
	int iMinutes = dLeast / 60 % 60;
	int iSeconds = dLeast % 60;
	return QString("%1:%2:%3").arg(iHour).arg(iMinutes).arg(iSeconds);
}

void CPreciseTimeEx::ClearTime()
{
	m_lastTime = 0;

	t1.QuadPart=t2.QuadPart=0;
	tc.QuadPart=0;
}

void CPreciseTimeEx::ResumeTime()
{
	if(false == m_bStart)
	{
		double dTime = GetTime();
		ClearTime();
		m_lastTime = dTime;
		QueryPerformanceCounter(&t1);
		m_bStart = true;
	}
}
