📄 sh_time.cpp
字号:
// SH_Time.cpp: implementation of the SH_Time class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "SH_Time.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
SH_Time::SH_Time()
{
}
SH_Time::~SH_Time()
{
}
SH_Time::SH_Time(time_t time)
{
m_time = time;
}
SH_Time::SH_Time(const SH_Time& timeSrc)
{
m_time = timeSrc.m_time;
}
SH_Time::SH_Time(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec,
int nDST)
{
struct tm atm;
atm.tm_sec = nSec;
atm.tm_min = nMin;
atm.tm_hour = nHour;
ASSERT(nDay >= 1 && nDay <= 31);
atm.tm_mday = nDay;
ASSERT(nMonth >= 1 && nMonth <= 12);
atm.tm_mon = nMonth - 1; // tm_mon is 0 based
ASSERT(nYear >= 1900);
atm.tm_year = nYear - 1900; // tm_year is 1900 based
atm.tm_isdst = nDST;
m_time = mktime(&atm);
ASSERT(m_time != -1); // indicates an illegal input time
}
SH_Time::SH_Time(WORD wDosDate, WORD wDosTime, int nDST)
{
struct tm atm;
atm.tm_sec = (wDosTime & ~0xFFE0) << 1;
atm.tm_min = (wDosTime & ~0xF800) >> 5;
atm.tm_hour = wDosTime >> 11;
atm.tm_mday = wDosDate & ~0xFFE0;
atm.tm_mon = ((wDosDate & ~0xFE00) >> 5) - 1;
atm.tm_year = (wDosDate >> 9) + 80;
atm.tm_isdst = nDST;
m_time = mktime(&atm);
ASSERT(m_time != -1); // indicates an illegal input time
}
SH_Time::SH_Time(const SYSTEMTIME& sysTime, int nDST)
{
if (sysTime.wYear < 1900)
{
time_t time0 = 0L;
SH_Time timeT(time0);
*this = timeT;
}
else
{
SH_Time timeT(
(int)sysTime.wYear, (int)sysTime.wMonth, (int)sysTime.wDay,
(int)sysTime.wHour, (int)sysTime.wMinute, (int)sysTime.wSecond,
nDST);
*this = timeT;
}
}
SH_Time::SH_Time(const FILETIME& fileTime, int nDST)
{
FILETIME localTime;
if (!FileTimeToLocalFileTime(&fileTime, &localTime))
{
m_time = 0;
return;
}
SYSTEMTIME sysTime;
if (!FileTimeToSystemTime(&localTime, &sysTime))
{
m_time = 0;
return;
}
SH_Time timeT(sysTime, nDST);
*this = timeT;
}
const SH_Time& SH_Time::operator=(const SH_Time& timeSrc)
{
m_time = timeSrc.m_time; return *this;
}
const SH_Time& SH_Time::operator=(time_t t)
{
m_time = t; return *this;
}
time_t SH_Time::GetTime() const
{
return m_time;
}
int SH_Time::GetYear() const
{
return (GetLocalTm(NULL)->tm_year) + 1900;
}
int SH_Time::GetMonth() const
{
return GetLocalTm(NULL)->tm_mon + 1;
}
int SH_Time::GetDay() const
{
return GetLocalTm(NULL)->tm_mday;
}
int SH_Time::GetHour() const
{
return GetLocalTm(NULL)->tm_hour;
}
int SH_Time::GetMinute() const
{
return GetLocalTm(NULL)->tm_min;
}
int SH_Time::GetSecond() const
{
return GetLocalTm(NULL)->tm_sec;
}
int SH_Time::GetDayOfWeek() const
{
return GetLocalTm(NULL)->tm_wday + 1;
}
BOOL SH_Time::operator==(SH_Time time) const
{
return m_time == time.m_time;
}
BOOL SH_Time::operator!=(SH_Time time) const
{
return m_time != time.m_time;
}
BOOL SH_Time::operator<(SH_Time time) const
{
return m_time < time.m_time;
}
BOOL SH_Time::operator>(SH_Time time) const
{
return m_time > time.m_time;
}
BOOL SH_Time::operator<=(SH_Time time) const
{
return m_time <= time.m_time;
}
BOOL SH_Time::operator>=(SH_Time time) const
{
return m_time >= time.m_time;
}
SH_Time PASCAL SH_Time::GetCurrentTime()
{
return SH_Time(::time(NULL));
}
struct tm* SH_Time::GetGmtTm(struct tm* ptm) const
{
if (ptm != NULL)
{
*ptm = *gmtime(&m_time);
return ptm;
}
else
return gmtime(&m_time);
}
struct tm* SH_Time::GetLocalTm(struct tm* ptm) const
{
if (ptm != NULL)
{
struct tm* ptmTemp = localtime(&m_time);
if (ptmTemp == NULL)
return NULL; // indicates the m_time was not initialized!
*ptm = *ptmTemp;
return ptm;
}
else
return localtime(&m_time);
}
BOOL SH_Time::GetAsSystemTime(SYSTEMTIME& timeDest) const
{
struct tm* ptm = GetLocalTm(NULL);
if (ptm == NULL)
return FALSE;
timeDest.wYear = (WORD) (1900 + ptm->tm_year);
timeDest.wMonth = (WORD) (1 + ptm->tm_mon);
timeDest.wDayOfWeek = (WORD) ptm->tm_wday;
timeDest.wDay = (WORD) ptm->tm_mday;
timeDest.wHour = (WORD) ptm->tm_hour;
timeDest.wMinute = (WORD) ptm->tm_min;
timeDest.wSecond = (WORD) ptm->tm_sec;
timeDest.wMilliseconds = 0;
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -