#include "StdAfx.h" #include "WorkTime.h" CWorkTime gWorkTime; CWorkTime::CWorkTime(void) { m_bRecordTime = false;//是否正在计时 m_BackTime1Seconds = 0;//倒计时秒数 m_BackTime2Seconds = 0;//倒计时秒数 m_bBackTime1End = false;//倒计时1 是否判断过 } CWorkTime::~CWorkTime(void) { } CString CWorkTime::Int2Str_LeftZero(int n) { CString s; if(n<10) s.Format("0%ld",n);//左边补0 else s.Format("%ld",n); return s; } #if 1//粗略计时 //开始计时 void CWorkTime::StartRecordTime() { m_bRecordTime = true; m_StartTime=CTime::GetCurrentTime(); } void CWorkTime::StopRecordTime() { m_bRecordTime = false; } CString CWorkTime::GetTimeSpanStr() { if(!m_bRecordTime) { return ""; } //获取当前系统时间 CTime CurTime; CTimeSpan TimeSpan;//时间差 CurTime=CTime::GetCurrentTime(); TimeSpan = CurTime - m_StartTime; return GetTimeSpanStr(TimeSpan); } //当前经过了多少秒 double CWorkTime::GetTimeSpanSecond() { CTimeSpan TimeSpan = GetTimeSpan(); int s = 0; s += TimeSpan.GetHours() * 60 * 60; s += TimeSpan.GetMinutes() * 60; s += TimeSpan.GetSeconds(); return s; } CTimeSpan CWorkTime::GetTimeSpan() { //获取当前系统时间 CTime CurTime; CTimeSpan TimeSpan;//时间差 CurTime=CTime::GetCurrentTime(); TimeSpan = CurTime - m_StartTime; return TimeSpan; } //获取当前经过时间 CString CWorkTime::GetTimeSpanStr(CTimeSpan &TimeSpan) { return Int2Str_LeftZero(TimeSpan.GetHours())+":"+Int2Str_LeftZero(TimeSpan.GetMinutes())+":"+Int2Str_LeftZero(TimeSpan.GetSeconds()); } //获取当天的字符串"12:02:25" CString CWorkTime::GetCurDate(CString Separator) { CString str; CTime CurTime; CurTime=CTime::GetCurrentTime(); str += Int2Str_LeftZero(CurTime.GetYear())+Separator; str += Int2Str_LeftZero(CurTime.GetMonth())+Separator; str += Int2Str_LeftZero(CurTime.GetDay()); return str; } //获取当前的年 CString CWorkTime::GetCurYear() { CString str; CTime CurTime; CurTime=CTime::GetCurrentTime(); str += Int2Str_LeftZero(CurTime.GetYear()); return str; } //获取当前的月 CString CWorkTime::GetCurMonth() { CString str; CTime CurTime; CurTime=CTime::GetCurrentTime(); str += Int2Str_LeftZero(CurTime.GetMonth()); return str; } CString CWorkTime::GetCurDay() { CString str; CTime CurTime; CurTime=CTime::GetCurrentTime(); str += Int2Str_LeftZero(CurTime.GetDay()); return str; } //获取当前时间Separator 为分隔符 CString CWorkTime::GetCurTime(CString Separator) { CString str; CTime CurTime; CurTime=CTime::GetCurrentTime(); str += Int2Str_LeftZero(CurTime.GetHour())+Separator; str += Int2Str_LeftZero(CurTime.GetMinute())+Separator; str += Int2Str_LeftZero(CurTime.GetSecond()); return str; } CString CWorkTime::GetDateTime(CString SeparatorDate,CString SeparatorTime) { CString CurDate = GetCurDate(SeparatorDate); CString CurTime = GetCurTime(SeparatorTime); CString DateTime = CurDate + SeparatorDate + CurTime; return DateTime; } CString CWorkTime::GetDateStr(CTime Time,CString Separator) { CString str; str += Int2Str_LeftZero(Time.GetYear())+Separator; str += Int2Str_LeftZero(Time.GetMonth())+Separator; str += Int2Str_LeftZero(Time.GetDay()); return str; } //当前经过了多少分钟 int CWorkTime::GetTimeSpanMin(CTimeSpan &TimeSpan) { int m = 0; m += TimeSpan.GetHours()*60; m += TimeSpan.GetMinutes(); return m; } #endif #if 1//精确计时 //开始精确计时 void CWorkTime::StartExactTime() { QueryPerformanceCounter(&m_ExactStart); } #endif #if 1//倒计时功能 //开始倒计时(Time2 到了之后才结束计时) void CWorkTime::StartBackTime(double Time1,double Time2) { m_BackTime1Seconds = Time1; m_BackTime2Seconds = Time2; m_bBackTime1End = false;//倒计时1 是否判断过 StartRecordTime(); } //获取还剩多少时间 CString CWorkTime::GetLeftTimes() { double CurSeconds = GetTimeSpanSecond();//当前经过的秒数 int LeftSeconds = (int)(m_BackTime2Seconds-CurSeconds); CString str = "00:00:00"; if(LeftSeconds>0) { int Seconds = LeftSeconds % 60; LeftSeconds -= Seconds; int Minutes = (LeftSeconds%3600)/60; LeftSeconds -= Minutes*60; int Hours = LeftSeconds/3600; str = Int2Str_LeftZero(Hours)+":"+Int2Str_LeftZero(Minutes)+":"+Int2Str_LeftZero(Seconds); } return str; } //倒计时是否结束(idx == 1 或者idx == 2) bool CWorkTime::IsBackTimeEnd(int idx) { if(!m_bRecordTime)//已经结束 return true; if(idx==1 && m_bBackTime1End)//保证倒计时1 只返回一次 return false; double CurSeconds = GetTimeSpanSecond();//当前经过的秒数 double BackTimeSeconds = (idx==1)?m_BackTime1Seconds:m_BackTime2Seconds; if(CurSeconds > BackTimeSeconds) { if(idx==2) { m_bRecordTime = false;//结束计时 } else { m_bBackTime1End = true; } return true; } return false; } #endif //微秒级延时,us为微秒 void CWorkTime::DelayTime(unsigned int us) { if(us>0) { LARGE_INTEGER ClockFre; QueryPerformanceFrequency(&ClockFre); LARGE_INTEGER start, end; LONGLONG count = (us*ClockFre.QuadPart)/(1000*1000); QueryPerformanceCounter(&start); count = count + start.QuadPart ; do { QueryPerformanceCounter(&end); }while(end.QuadPart