bool IsToday(time_t nLastTime, time_t nNowTime)
{
time_t LastTime = nLastTime;
struct tm localTime;
localtime_r(&LastTime, &localTime);
time_t NowTime = nNowTime;
struct tm localNowTime;
localtime_r(&NowTime, &localNowTime);
if (localTime.tm_year == localNowTime.tm_year &&
localTime.tm_mon == localNowTime.tm_mon &&
localTime.tm_mday == localNowTime.tm_mday)
{
return true;
}
else
return false;
}
扩展
bool CTimer::IsToday() const
{
if (m_Time == 0)
return false;
time_t NowTime = time(NULL);
struct tm localNowTime;
localtime_r(&NowTime, &localNowTime);
struct tm localTime;
localtime_r(&m_Time, &localTime);
if (localTime.tm_year == localNowTime.tm_year &&
localTime.tm_mon == localNowTime.tm_mon &&
localTime.tm_mday == localNowTime.tm_mday)
{
return true;
}
else
return false;
}
bool CTimer::IsThisWeek() const
{
if (m_Time == 0)
return false;
time_t NowTime = time(NULL);
struct tm localNowTime;
localtime_r(&NowTime, &localNowTime);
struct tm localTime;
localtime_r(&m_Time, &localTime);
return (localTime.tm_year == localNowTime.tm_year &&
(localTime.tm_yday - localNowTime.tm_yday) == (GetWeek7Day(m_Time) - GetWeek7Day(NowTime)));
}
int CTimer::GetWeek7Day(time_t tTime) const
{
struct tm localNowTime;
localtime_r(&tTime, &localNowTime);
return localNowTime.tm_wday == 0 ? 7 : localNowTime.tm_wday;
}
bool CTimer::IsThisMonth() const
{
if (m_Time == 0)
return false;
time_t NowTime = time(NULL);
struct tm localNowTime;
localtime_r(&NowTime, &localNowTime);
struct tm localTime;
localtime_r(&m_Time, &localTime);
return (localTime.tm_year == localNowTime.tm_year &&
localTime.tm_mon == localNowTime.tm_mon);
}
bool CTimer::IsYesterday() const
{
time_t NowTime = time(NULL);
struct tm local;
localtime_r(&NowTime, &local);
struct tm LoaclTime;
localtime_r(&m_Time, &LoaclTime);
if (LoaclTime.tm_year != local.tm_year)
return false;
return (local.tm_yday - LoaclTime.tm_yday) == 1;
}
time_t CTimer::GetSunday() const
{
time_t NowTime = time(NULL);
struct tm local;
localtime_r(&NowTime, &local);
local.tm_mday = local.tm_mday - local.tm_wday + 1; //加1表示星期一的零时
local.tm_hour = 0;
local.tm_min = 0;
local.tm_sec = 0;
time_t Sunday = mktime(&local);
return Sunday;
}
int CTimer::GetYear() const
{
struct tm localNowTime;
localtime_r(&m_Time, &localNowTime);
return localNowTime.tm_year + 1900;
}
int CTimer::GetMonth() const
{
struct tm localNowTime;
localtime_r(&m_Time, &localNowTime);
return localNowTime.tm_mon + 1;
}
int CTimer::GetDay() const
{
struct tm localNowTime;
localtime_r(&m_Time, &localNowTime);
return localNowTime.tm_mday;
}
int CTimer::GetHour() const
{
struct tm localNowTime;
localtime_r(&m_Time, &localNowTime);
return localNowTime.tm_hour;
}
int CTimer::GetMinute() const
{
struct tm localNowTime;
localtime_r(&m_Time, &localNowTime);
return localNowTime.tm_min;
}
int CTimer::GetSecond() const
{
struct tm localNowTime;
localtime_r(&m_Time, &localNowTime);
return localNowTime.tm_sec;
}
int CTimer::GetYearDay() const
{
struct tm localNowTime;
localtime_r(&m_Time, &localNowTime);
return localNowTime.tm_yday;
}
int CTimer::Get12Clock() const
{
struct tm localNowTime;
localtime_r(&m_Time, &localNowTime);
localNowTime.tm_mday = localNowTime.tm_mday + 1;
localNowTime.tm_hour = 0;
localNowTime.tm_min = 0;
localNowTime.tm_sec = 0;
time_t tToNight = mktime(&localNowTime);
return tToNight - m_Time;
}
time_t CTimer::GetDate() const
{
struct tm localNowTime;
localtime_r(&m_Time, &localNowTime);
localNowTime.tm_hour = 0;
localNowTime.tm_min = 0;
localNowTime.tm_sec = 0;
time_t date = mktime(&localNowTime);
return date;
}
int CTimer::GetWeekDay() const
{
struct tm localNowTime;
localtime_r(&m_Time, &localNowTime);
return localNowTime.tm_wday;
}