📄 time.cpp
字号:
// Time.cpp: implementation of the CTime class.
//
//////////////////////////////////////////////////////////////////////
#include "Time.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CTime::CTime()
{
ZeroMemory(&m_Time,sizeof(SYSTEMTIME));
m_bFlag = FALSE;
}
CTime::CTime(const CTime &tmTime)
{
ZeroMemory(&m_Time,sizeof(SYSTEMTIME));
m_Time = tmTime.m_Time;
m_bFlag = TRUE;
}
CTime::~CTime()
{
m_bFlag = FALSE;
}
CTime __stdcall CTime::GetCurrentTime()
{
m_bFlag = TRUE;
GetLocalTime(&m_Time);
return *this;
}
WORD CTime::GetHour() const
{
ASSERT(m_bFlag);
return m_Time.wHour;
}
WORD CTime::GetMinute() const
{
ASSERT(m_bFlag);
return m_Time.wMinute;
}
WORD CTime::GetSecond() const
{
ASSERT(m_bFlag);
return m_Time.wSecond;
}
WORD CTime::GetYear() const
{
ASSERT(m_bFlag);
return m_Time.wYear;
}
WORD CTime::GetMonth() const
{
ASSERT(m_bFlag);
return m_Time.wMonth;
}
WORD CTime::GetDay() const
{
ASSERT(m_bFlag);
return m_Time.wDay;
}
WORD CTime::GetDayOfWeek() const
{
ASSERT(m_bFlag);
return m_Time.wDayOfWeek ;
}
CTime::CTime(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec)
{
ZeroMemory(&m_Time,sizeof(SYSTEMTIME));
m_Time.wYear = nYear;
m_Time.wMonth = nMonth;
m_Time.wDay = nDay;
m_Time.wHour = nHour;
m_Time.wMinute = nMin;
m_Time.wSecond = nSec;
m_bFlag = SetSystemTime(&m_Time);
}
CTime::CTime(const SYSTEMTIME& sysTime)
{
m_Time = sysTime;
m_bFlag = TRUE;
}
CTime::CTime(const FILETIME& fileTime)
{
m_bFlag = FileTimeToSystemTime(&fileTime,&m_Time);
}
const CTime& CTime::operator =(const CTime &tmTime)
{
m_Time = tmTime.m_Time;
m_bFlag = TRUE;
return *this;
}
BOOL CTime::operator==(CTime time) const
{
// * the only valid formats:
// %D - # of days -- NEW !!!
// %H - hour in 24 hour format
// %M - minute (0-59)
// %S - seconds (0-59)
// %% - percent sign
if((m_Time.wYear == time.m_Time.wYear)&&(m_Time.wMonth == time.m_Time.wMonth)&&
(m_Time.wDay == time.m_Time.wDay )&&(m_Time.wHour == time.m_Time.wHour )&&
(m_Time.wMinute == time.m_Time.wMinute)&&(m_Time.wSecond == time.m_Time.wSecond))
return TRUE;
return FALSE;
}
TCHAR* CTime::Format(LPCTSTR pFormat) const
{
TCHAR szBuffer[1024];
TCHAR ch;
LPTSTR pch = szBuffer;
while ((ch = *pFormat++) != '\0')
{
ASSERT(pch < &szBuffer[1024]);
if (ch == '%')
{
switch (ch = *pFormat++)
{
default:
ASSERT(FALSE); // probably a bad format character
case '%':
*pch++ = ch;
break;
case 'D':
pch += wsprintf(pch, _T("%ld"), GetDay());
break;
case 'H':
pch += wsprintf(pch, _T("%02d"), GetHour());
break;
case 'M':
pch += wsprintf(pch, _T("%02d"), GetMinute());
break;
case 'S':
pch += wsprintf(pch, _T("%02d"), GetSecond());
break;
}
}
else
{
*pch++ = ch;
if (_istlead(ch))
{
ASSERT(pch < &szBuffer[1024]);
*pch++ = *pFormat++;
}
}
}
*pch = '\0';
return pch;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -