You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
5.5 KiB
C++

#include "StdAfx.h"
#include "WorkTime.h"
CWorkTime gWorkTime;
CWorkTime::CWorkTime(void)
{
m_bRecordTime = false;//<2F>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ڼ<EFBFBD>ʱ
m_BackTime1Seconds = 0;//<2F><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
m_BackTime2Seconds = 0;//<2F><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
m_bBackTime1End = false;//<2F><><EFBFBD><EFBFBD>ʱ1 <20>Ƿ<EFBFBD><C7B7>жϹ<D0B6>
}
CWorkTime::~CWorkTime(void)
{
}
CString CWorkTime::Int2Str_LeftZero(int n)
{
CString s;
if(n<10)
s.Format("0%ld",n);//<2F><><EFBFBD>߲<EFBFBD>0
else
s.Format("%ld",n);
return s;
}
#if 1//<2F><><EFBFBD>Լ<EFBFBD>ʱ
//<2F><>ʼ<EFBFBD><CABC>ʱ
void CWorkTime::StartRecordTime()
{
m_bRecordTime = true;
m_StartTime=CTime::GetCurrentTime();
}
void CWorkTime::StopRecordTime()
{
m_bRecordTime = false;
}
CString CWorkTime::GetTimeSpanStr()
{
if(!m_bRecordTime)
{
return "";
}
//<2F><>ȡ<EFBFBD><C8A1>ǰϵͳʱ<CDB3><CAB1>
CTime CurTime;
CTimeSpan TimeSpan;//ʱ<><CAB1><EFBFBD><EFBFBD>
CurTime=CTime::GetCurrentTime();
TimeSpan = CurTime - m_StartTime;
return GetTimeSpanStr(TimeSpan);
}
//<2F><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>˶<EFBFBD><CBB6><EFBFBD><EFBFBD><EFBFBD>
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()
{
//<2F><>ȡ<EFBFBD><C8A1>ǰϵͳʱ<CDB3><CAB1>
CTime CurTime;
CTimeSpan TimeSpan;//ʱ<><CAB1><EFBFBD><EFBFBD>
CurTime=CTime::GetCurrentTime();
TimeSpan = CurTime - m_StartTime;
return TimeSpan;
}
//<2F><>ȡ<EFBFBD><C8A1>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
CString CWorkTime::GetTimeSpanStr(CTimeSpan &TimeSpan)
{
return Int2Str_LeftZero(TimeSpan.GetHours())+":"+Int2Str_LeftZero(TimeSpan.GetMinutes())+":"+Int2Str_LeftZero(TimeSpan.GetSeconds());
}
//<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>"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;
}
//<2F><>ȡ<EFBFBD><C8A1>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>
CString CWorkTime::GetCurYear()
{
CString str;
CTime CurTime;
CurTime=CTime::GetCurrentTime();
str += Int2Str_LeftZero(CurTime.GetYear());
return str;
}
//<2F><>ȡ<EFBFBD><C8A1>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>
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;
}
//<2F><>ȡ<EFBFBD><C8A1>ǰʱ<C7B0><CAB1>Separator Ϊ<>ָ<EFBFBD><D6B8><EFBFBD>
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;
}
//<2F><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>˶<EFBFBD><CBB6>ٷ<EFBFBD><D9B7><EFBFBD>
int CWorkTime::GetTimeSpanMin(CTimeSpan &TimeSpan)
{
int m = 0;
m += TimeSpan.GetHours()*60;
m += TimeSpan.GetMinutes();
return m;
}
#endif
#if 1//<2F><>ȷ<EFBFBD><C8B7>ʱ
//<2F><>ʼ<EFBFBD><CABC>ȷ<EFBFBD><C8B7>ʱ
void CWorkTime::StartExactTime()
{
QueryPerformanceCounter(&m_ExactStart);
}
#endif
#if 1//<2F><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
//<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>ʱ(Time2 <20><><EFBFBD><EFBFBD>֮<EFBFBD><D6AE><EFBFBD>Ž<EFBFBD><C5BD><EFBFBD><EFBFBD><EFBFBD>ʱ)
void CWorkTime::StartBackTime(double Time1,double Time2)
{
m_BackTime1Seconds = Time1;
m_BackTime2Seconds = Time2;
m_bBackTime1End = false;//<2F><><EFBFBD><EFBFBD>ʱ1 <20>Ƿ<EFBFBD><C7B7>жϹ<D0B6>
StartRecordTime();
}
//<2F><>ȡ<EFBFBD><C8A1>ʣ<EFBFBD><CAA3><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
CString CWorkTime::GetLeftTimes()
{
double CurSeconds = GetTimeSpanSecond();//<2F><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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;
}
//<2F><><EFBFBD><EFBFBD>ʱ<EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>(idx == 1 <20><><EFBFBD><EFBFBD>idx == 2)
bool CWorkTime::IsBackTimeEnd(int idx)
{
if(!m_bRecordTime)//<2F>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD>
return true;
if(idx==1 && m_bBackTime1End)//<2F><>֤<EFBFBD><D6A4><EFBFBD><EFBFBD>ʱ1 ֻ<><D6BB><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
return false;
double CurSeconds = GetTimeSpanSecond();//<2F><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
double BackTimeSeconds = (idx==1)?m_BackTime1Seconds:m_BackTime2Seconds;
if(CurSeconds > BackTimeSeconds)
{
if(idx==2)
{
m_bRecordTime = false;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ
}
else
{
m_bBackTime1End = true;
}
return true;
}
return false;
}
#endif
//΢<><EFBFBD><EBBCB6>ʱ<EFBFBD><CAB1>usΪ΢<CEAA><CEA2>
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<count);
}
}