仿C# DateTime的C++实现
- WDateTime.h
- WTimeSpan.cpp
- WDateTime.cpp
WDateTime.h
#pragma once
#include "WPublicHead.h"
#include "WString.h"
namespace WLSJ
{
namespace WDK
{
class WMB_API WDateTimeSpan
{
public:
WDateTimeSpan();
WDateTimeSpan(WUint64 nanosecond);
private:
Wint32 day;
Wint32 hour;
Wint32 minute;
Wint32 second;
Wint32 milliseconds;
WUint64 nanosecond;
public:
Wint32 Day();
Wint32 Hour();
Wint32 Minute();
Wint32 Second();
Wint32 Milliseconds();
Wdouble TotalDays();
Wdouble TotalHours();
Wdouble TotalMinutes();
Wdouble TotalSeconds();
Wdouble TotalMilliseconds();
};
#define WDateTimeFormat "yyyy-MM-dd hh:mm:ss"
class WMB_API WDateTime
{
private:
SYSTEMTIME st;
FILETIME ft;
ULARGE_INTEGER GetULARGE_INTEGER();
public:
static WDateTime* Now();
public:
WDateTime();
Wint32 vYear();
Wint32 vMonth();
Wint32 vDay();
Wint32 vHour();
Wint32 vMinute();
Wint32 vSecond();
Wint32 vMillisecond();
Wint32 vDayOfWeek();
Wbool JudgeBissextile(IN Wint32 Year);
public:
Wbool AddDays(IN WUint64 value);
Wbool AddHours(IN WUint64 value);
Wbool AddMinutes(IN WUint64 value);
Wbool AddSeconds(IN WUint64 value);
Wbool AddMilliseconds(IN WUint64 value);
public:
Wbool StringToDateTime(IN WStrPtr datetime, IN WStrPtr format = WDateTimeFormat);
WString* ToString(IN WStrPtr format);
public:
WDateTimeSpan* operator-(WDateTime& t1);
Wbool operator<(WDateTime& t1);
Wbool operator<=(WDateTime& t1);
Wbool operator>(WDateTime& t1);
Wbool operator>=(WDateTime& t1);
Wbool operator==(WDateTime& t1);
};
}
};
WTimeSpan.cpp
#include "WTimeSpan.h"
namespace WLSJ
{
namespace WDK
{
WDateTime::WDateTime()
{
memset(&st,0,sizeof(SYSTEMTIME));
memset(&ft,0,sizeof(FILETIME));
}
WDateTime* WDateTime::Now()
{
WDateTime* datetime = new WDateTime();
SYSTEMTIME* pst = &datetime->st;
GetLocalTime(pst);
return datetime;
}
Wbool WDateTime::JudgeBissextile(IN Wint32 Year)
{
if (Year % 4 == 0 && Year % 100 != 0 || Year % 400 == 0)
{
return true;
}
else
return false;
}
Wint32 WDateTime::vYear()
{
return st.wYear;
}
Wint32 WDateTime::vMonth()
{
return st.wMonth;
}
Wint32 WDateTime::vDay()
{
return st.wDay;
}
Wint32 WDateTime::vHour()
{
return st.wHour;
}
Wint32 WDateTime::vMinute()
{
return st.wMinute;
}
Wint32 WDateTime::vSecond()
{
return st.wSecond;
}
Wint32 WDateTime::vMillisecond()
{
return st.wMilliseconds;
}
Wint32 WDateTime::vDayOfWeek()
{
return st.wDayOfWeek;
}
Wbool WDateTime::AddDays(IN WUint64 value)
{
AddMinutes(value * 24);
return true;
}
Wbool WDateTime::AddHours(IN WUint64 value)
{
AddMinutes(value * 60);
return true;
}
Wbool WDateTime::AddMinutes(IN WUint64 value)
{
AddSeconds( value * 60 );
return true;
}
Wbool WDateTime::AddSeconds(IN WUint64 value)
{
AddMilliseconds( value * 1000);
return true;
}
Wbool WDateTime::AddMilliseconds(IN WUint64 value)
{
ULARGE_INTEGER uliTime;
if (SystemTimeToFileTime(&st, &ft))
{
uliTime.LowPart = ft.dwLowDateTime;
uliTime.HighPart = ft.dwHighDateTime;
uliTime.QuadPart += value * 10000;
ft.dwLowDateTime = uliTime.LowPart;
ft.dwHighDateTime = uliTime.HighPart;
FileTimeToSystemTime(&ft, &st);
return true;
}
else
{
return false;
}
}
Wbool WDateTime::StringToDateTime(IN WStrPtr datetime, IN WStrPtr format)
{
Wint32 size_datetime = strlen(datetime);
Wint32 size_format = strlen(format);
if(size_datetime != size_format)
return false;
memset(&st,0,sizeof(SYSTEMTIME));
Wchar temp[20] = {0};
for(Wint32 i=0;i<size_format;i++)
{
if(st.wYear == 0 && i + 4 <= size_format)
{
if(format[i] == 'y' && format[i+1] == 'y' && format[i+2] == 'y' && format[i+3] == 'y')
{
memcpy(temp,datetime + i, 4);
temp[4] = '\0';
st.wYear = atoi(temp);
i+=4;
}
}
if(i + 2 <= size_format)
{
switch(format[i])
{
case 'M':
if(st.wMonth == 0 && format[i+1] == 'M')
{
memcpy(temp,datetime + i, 2);
temp[2] = '\0';
st.wMonth = atoi(temp);
i+=2;
}
break;
case 'd':
if(st.wDay == 0 && format[i+1] == 'd')
{
memcpy(temp,datetime + i, 2);
temp[2] = '\0';
st.wDay = atoi(temp);
i+=2;
}
break;
case 'h':
if(st.wHour == 0 && format[i+1] == 'h')
{
memcpy(temp,datetime + i, 2);
temp[2] = '\0';
st.wHour = atoi(temp);
i+=2;
}
break;
case 'm':
if(st.wMinute == 0 && format[i+1] == 'm')
{
memcpy(temp,datetime + i, 2);
temp[2] = '\0';
st.wMinute = atoi(temp);
i+=2;
}
break;
case 's':
if(st.wSecond == 0 && format[i+1] == 's')
{
memcpy(temp,datetime + i, 2);
temp[2] = '\0';
st.wSecond = atoi(temp);
i+=2;
}
break;
}
}
}
if(SystemTimeToFileTime(&st, &ft) == false)
{
memset(&st,0,sizeof(SYSTEMTIME));
return false;
}
else
{
return true;
}
}
WString* WDateTime::ToString(IN WStrPtr format)
{
WString* str = new WString(format);
Wint32 index = 0;
WStrPtr pchar = str->GetData();
Wchar temp[20] = {0};
if((index = str->IndexOf("yyyy")) != -1)
{
sprintf(temp,"%d",this->vYear());
pchar[index] = temp[0];
pchar[index+1] = temp[1];
pchar[index+2] = temp[2];
pchar[index+3] = temp[3];
}
if((index = str->IndexOf("MM")) != -1)
{
sprintf(temp,"%d",this->vMonth());
if(this->vMonth() < 10)
{
temp[1] = temp[0];
temp[0] = '0';
}
pchar[index] = temp[0];
pchar[index+1] = temp[1];
}
if((index = str->IndexOf("dd")) != -1)
{
sprintf(temp,"%d",this->vDay());
if(this->vDay() < 10)
{
temp[1] = temp[0];
temp[0] = '0';
}
pchar[index] = temp[0];
pchar[index+1] = temp[1];
}
if((index = str->IndexOf("hh")) != -1)
{
sprintf(temp,"%d",this->vHour());
if(this->vHour() < 10)
{
temp[1] = temp[0];
temp[0] = '0';
}
pchar[index] = temp[0];
pchar[index+1] = temp[1];
}
if((index = str->IndexOf("mm")) != -1)
{
sprintf(temp,"%d",this->vMinute());
if(this->vMinute() < 10)
{
temp[1] = temp[0];
temp[0] = '0';
}
pchar[index] = temp[0];
pchar[index+1] = temp[1];
}
if((index = str->IndexOf("ss")) != -1)
{
sprintf(temp,"%d",this->vSecond());
if(this->vSecond() < 10)
{
temp[1] = temp[0];
temp[0] = '0';
}
pchar[index] = temp[0];
pchar[index+1] = temp[1];
}
return str;
}
ULARGE_INTEGER WDateTime::GetULARGE_INTEGER()
{
ULARGE_INTEGER uliTime = { 0 };
uliTime.HighPart = ft.dwHighDateTime;
uliTime.LowPart = ft.dwLowDateTime;
return uliTime;
}
WDateTimeSpan* WDateTime::operator-(WDateTime& t1)
{
ULARGE_INTEGER uliTime1 = GetULARGE_INTEGER();
ULARGE_INTEGER uliTime2 = t1.GetULARGE_INTEGER();
WDateTimeSpan* res;
if(uliTime1.QuadPart > uliTime2.QuadPart)
res = new WDateTimeSpan(uliTime1.QuadPart - uliTime2.QuadPart);
else
res = new WDateTimeSpan(uliTime2.QuadPart - uliTime1.QuadPart);
return res;
}
Wbool WDateTime::operator<(WDateTime& t1)
{
if (GetULARGE_INTEGER().QuadPart < t1.GetULARGE_INTEGER().QuadPart)
{
return true;
}
else
return false;
}
Wbool WDateTime::operator>(WDateTime& t1)
{
if (GetULARGE_INTEGER().QuadPart > t1.GetULARGE_INTEGER().QuadPart)
{
return true;
}
else
return false;
}
Wbool WDateTime::operator<=(WDateTime& t1)
{
if (GetULARGE_INTEGER().QuadPart <= t1.GetULARGE_INTEGER().QuadPart)
{
return true;
}
else
return false;
}
Wbool WDateTime::operator>=(WDateTime& t1)
{
if (GetULARGE_INTEGER().QuadPart >= t1.GetULARGE_INTEGER().QuadPart)
{
return true;
}
else
return false;
}
Wbool WDateTime::operator==(WDateTime& t1)
{
if (GetULARGE_INTEGER().QuadPart == t1.GetULARGE_INTEGER().QuadPart)
{
return true;
}
else
return false;
}
}
};
WDateTime.cpp
#include "WDateTime.h"
namespace WLSJ
{
namespace WDK
{
WDateTime::WDateTime()
{
memset(&st,0,sizeof(SYSTEMTIME));
memset(&ft,0,sizeof(FILETIME));
}
WDateTime* WDateTime::Now()
{
WDateTime* datetime = new WDateTime();
SYSTEMTIME* pst = &datetime->st;
GetLocalTime(pst);
return datetime;
}
Wbool WDateTime::JudgeBissextile(IN Wint32 Year)
{
if (Year % 4 == 0 && Year % 100 != 0 || Year % 400 == 0)
{
return true;
}
else
return false;
}
Wint32 WDateTime::vYear()
{
return st.wYear;
}
Wint32 WDateTime::vMonth()
{
return st.wMonth;
}
Wint32 WDateTime::vDay()
{
return st.wDay;
}
Wint32 WDateTime::vHour()
{
return st.wHour;
}
Wint32 WDateTime::vMinute()
{
return st.wMinute;
}
Wint32 WDateTime::vSecond()
{
return st.wSecond;
}
Wint32 WDateTime::vMillisecond()
{
return st.wMilliseconds;
}
Wint32 WDateTime::vDayOfWeek()
{
return st.wDayOfWeek;
}
Wbool WDateTime::AddDays(IN WUint64 value)
{
AddMinutes(value * 24);
return true;
}
Wbool WDateTime::AddHours(IN WUint64 value)
{
AddMinutes(value * 60);
return true;
}
Wbool WDateTime::AddMinutes(IN WUint64 value)
{
AddSeconds( value * 60 );
return true;
}
Wbool WDateTime::AddSeconds(IN WUint64 value)
{
AddMilliseconds( value * 1000);
return true;
}
Wbool WDateTime::AddMilliseconds(IN WUint64 value)
{
ULARGE_INTEGER uliTime;
if (SystemTimeToFileTime(&st, &ft))
{
uliTime.LowPart = ft.dwLowDateTime;
uliTime.HighPart = ft.dwHighDateTime;
uliTime.QuadPart += value * 10000;
ft.dwLowDateTime = uliTime.LowPart;
ft.dwHighDateTime = uliTime.HighPart;
FileTimeToSystemTime(&ft, &st);
return true;
}
else
{
return false;
}
}
Wbool WDateTime::StringToDateTime(IN WStrPtr datetime, IN WStrPtr format)
{
Wint32 size_datetime = strlen(datetime);
Wint32 size_format = strlen(format);
if(size_datetime != size_format)
return false;
memset(&st,0,sizeof(SYSTEMTIME));
Wchar temp[20] = {0};
for(Wint32 i=0;i<size_format;i++)
{
if(st.wYear == 0 && i + 4 <= size_format)
{
if(format[i] == 'y' && format[i+1] == 'y' && format[i+2] == 'y' && format[i+3] == 'y')
{
memcpy(temp,datetime + i, 4);
temp[4] = '\0';
st.wYear = atoi(temp);
i+=4;
}
}
if(i + 2 <= size_format)
{
switch(format[i])
{
case 'M':
if(st.wMonth == 0 && format[i+1] == 'M')
{
memcpy(temp,datetime + i, 2);
temp[2] = '\0';
st.wMonth = atoi(temp);
i+=2;
}
break;
case 'd':
if(st.wDay == 0 && format[i+1] == 'd')
{
memcpy(temp,datetime + i, 2);
temp[2] = '\0';
st.wDay = atoi(temp);
i+=2;
}
break;
case 'h':
if(st.wHour == 0 && format[i+1] == 'h')
{
memcpy(temp,datetime + i, 2);
temp[2] = '\0';
st.wHour = atoi(temp);
i+=2;
}
break;
case 'm':
if(st.wMinute == 0 && format[i+1] == 'm')
{
memcpy(temp,datetime + i, 2);
temp[2] = '\0';
st.wMinute = atoi(temp);
i+=2;
}
break;
case 's':
if(st.wSecond == 0 && format[i+1] == 's')
{
memcpy(temp,datetime + i, 2);
temp[2] = '\0';
st.wSecond = atoi(temp);
i+=2;
}
break;
}
}
}
if(SystemTimeToFileTime(&st, &ft) == false)
{
memset(&st,0,sizeof(SYSTEMTIME));
return false;
}
else
{
return true;
}
}
WString* WDateTime::ToString(IN WStrPtr format)
{
WString* str = new WString(format);
Wint32 index = 0;
WStrPtr pchar = str->GetData();
Wchar temp[20] = {0};
if((index = str->IndexOf("yyyy")) != -1)
{
sprintf(temp,"%d",this->vYear());
pchar[index] = temp[0];
pchar[index+1] = temp[1];
pchar[index+2] = temp[2];
pchar[index+3] = temp[3];
}
if((index = str->IndexOf("MM")) != -1)
{
sprintf(temp,"%d",this->vMonth());
if(this->vMonth() < 10)
{
temp[1] = temp[0];
temp[0] = '0';
}
pchar[index] = temp[0];
pchar[index+1] = temp[1];
}
if((index = str->IndexOf("dd")) != -1)
{
sprintf(temp,"%d",this->vDay());
if(this->vDay() < 10)
{
temp[1] = temp[0];
temp[0] = '0';
}
pchar[index] = temp[0];
pchar[index+1] = temp[1];
}
if((index = str->IndexOf("hh")) != -1)
{
sprintf(temp,"%d",this->vHour());
if(this->vHour() < 10)
{
temp[1] = temp[0];
temp[0] = '0';
}
pchar[index] = temp[0];
pchar[index+1] = temp[1];
}
if((index = str->IndexOf("mm")) != -1)
{
sprintf(temp,"%d",this->vMinute());
if(this->vMinute() < 10)
{
temp[1] = temp[0];
temp[0] = '0';
}
pchar[index] = temp[0];
pchar[index+1] = temp[1];
}
if((index = str->IndexOf("ss")) != -1)
{
sprintf(temp,"%d",this->vSecond());
if(this->vSecond() < 10)
{
temp[1] = temp[0];
temp[0] = '0';
}
pchar[index] = temp[0];
pchar[index+1] = temp[1];
}
return str;
}
ULARGE_INTEGER WDateTime::GetULARGE_INTEGER()
{
ULARGE_INTEGER uliTime = { 0 };
uliTime.HighPart = ft.dwHighDateTime;
uliTime.LowPart = ft.dwLowDateTime;
return uliTime;
}
WDateTimeSpan* WDateTime::operator-(WDateTime& t1)
{
ULARGE_INTEGER uliTime1 = GetULARGE_INTEGER();
ULARGE_INTEGER uliTime2 = t1.GetULARGE_INTEGER();
WDateTimeSpan* res;
if(uliTime1.QuadPart > uliTime2.QuadPart)
res = new WDateTimeSpan(uliTime1.QuadPart - uliTime2.QuadPart);
else
res = new WDateTimeSpan(uliTime2.QuadPart - uliTime1.QuadPart);
return res;
}
Wbool WDateTime::operator<(WDateTime& t1)
{
if (GetULARGE_INTEGER().QuadPart < t1.GetULARGE_INTEGER().QuadPart)
{
return true;
}
else
return false;
}
Wbool WDateTime::operator>(WDateTime& t1)
{
if (GetULARGE_INTEGER().QuadPart > t1.GetULARGE_INTEGER().QuadPart)
{
return true;
}
else
return false;
}
Wbool WDateTime::operator<=(WDateTime& t1)
{
if (GetULARGE_INTEGER().QuadPart <= t1.GetULARGE_INTEGER().QuadPart)
{
return true;
}
else
return false;
}
Wbool WDateTime::operator>=(WDateTime& t1)
{
if (GetULARGE_INTEGER().QuadPart >= t1.GetULARGE_INTEGER().QuadPart)
{
return true;
}
else
return false;
}
Wbool WDateTime::operator==(WDateTime& t1)
{
if (GetULARGE_INTEGER().QuadPart == t1.GetULARGE_INTEGER().QuadPart)
{
return true;
}
else
return false;
}
}
};