TimeEx.cpp
上传用户:maryhy001
上传日期:2007-05-02
资源大小:2317k
文件大小:17k
- #include ".TimeEx.h"
- #define MIN_DATE (-657434L) // about year 100
- #define MAX_DATE 2958465L // about year 9999
- // Half a second, expressed in days
- #define HALF_SECOND (1.0/172800.0)
- // One-based array of days in year at month start
- static int rgMonthDays[13] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
- //get current date
- void CDateEx::NowDate()
- {
- SYSTEMTIME systime;
- memset(&systime, 0x0, sizeof(systime));
- GetLocalTime(&systime);
- this->m_unYear = systime.wYear;
- this->m_unMonth = systime.wMonth;
- this->m_unDay = systime.wDay;
- }
- //get the date string text
- void CDateEx::AsDString(LPSTR lpzDate, bool bFixedFmt)
- {
- if(NULL == lpzDate){
- throw "param is null, invalid memory!!!";
- }
- memset(lpzDate, 0x0, MAX_DATE_SIZE);
- if(bFixedFmt){
- sprintf(lpzDate, "%04d-%02d-%02d", m_unYear, m_unMonth, m_unDay);
- }
- else{
- sprintf(lpzDate, "%d-%d-%d", m_unYear, m_unMonth, m_unDay);
- }
- return ;
- }
- //operator "="
- CDateEx CDateEx::operator=(CDateEx &dtDate)
- {
- this->m_unYear = dtDate.Year();
- this->m_unMonth = dtDate.Month();
- this->m_unDay = dtDate.Day();
- return (*this);
- }
- //operator "=="
- bool CDateEx::operator==(CDateEx &dtDate)
- {
- return (this->m_unYear == dtDate.Year() &&
- this->m_unMonth == dtDate.Month() &&
- this->m_unDay == dtDate.Day());
- }
- //operator "!="
- bool CDateEx::operator!=(CDateEx &dtDate)
- {
- return !(*this==dtDate);
- }
- //operator ">"
- bool CDateEx::operator>(CDateEx &dtDate)
- {
- if( this->m_unYear >= dtDate.Year() &&
- this->m_unMonth >= dtDate.Month() &&
- this->m_unDay > dtDate.Day())
- return true;
- else
- return false;
- }
- //operator ">="
- bool CDateEx::operator>=(CDateEx &dtDate)
- {
- if(*this>dtDate || *this==dtDate){
- return true;
- }
- else{
- return false;
- }
- }
- //operator "<"
- bool CDateEx::operator<(CDateEx &dtDate)
- {
- return !(*this >= dtDate);
- }
- //operator "<="
- bool CDateEx::operator<=(CDateEx &dtDate)
- {
- return (*this < dtDate || *this == dtDate);
- }
- //operator "-"
- int CDateEx::operator-(CDateEx &dtDate)
- {
- if(*this==dtDate){
- return 0;
- }
-
- //get the begin and end date
- CDateEx dtBegin, dtEnd;
- if(*this > dtDate){
- dtBegin = dtDate;
- dtEnd = *this;
- }
- else{
- dtBegin = *this;
- dtEnd = dtDate;
- }
- //get the days
- int nret = ( dtEnd.Year() - dtBegin.Year() ) * 365;
- for(int i = dtBegin.Month(); i < dtEnd.Month(); ++i){
- switch(i) {
- case 1: case 3: case 5: case 7:
- case 8: case 10: case 12:
- nret += 31;
- break;
- case 2:
- nret += this->IsLeapYear()?29:28;
- break;
- default:
- nret += 30;
- break;
- }
- }
- nret += (dtEnd.Day() - dtBegin.Day());
-
- return nret;
- }
- //operator "++"
- CDateEx CDateEx::operator++(int)
- {
- bool bnextMonth = false;
- if( IsLeapYear() ){
- if(m_unMonth == 2 && 29 == m_unDay){
- bnextMonth = true;
- }
- }
- else{
- if(m_unMonth == 2 && 28 == m_unDay){
- bnextMonth = true;
- }
- }
- if(bnextMonth){
- goto NextMonth;
- }
- else{
- if( (IsLargeMonth(m_unMonth) && 31 == m_unDay) ||
- (!IsLargeMonth(m_unMonth) && 30 == m_unDay) ){
- bnextMonth = true;
- }
- }
- if(bnextMonth){
- goto NextMonth;
- }
- else{
- ++m_unDay;
- goto Over;
- }
- NextMonth:
- {
- //is end of the year
- if(12 == m_unMonth){
- m_unMonth = 1;
- ++m_unYear;
- }
- else{
- ++m_unMonth;
- }
- m_unDay = 1;
- }
- Over:
- return (*this);
- }
- //operator "--"
- CDateEx CDateEx::operator--(int)
- {
- bool bpriorMonth = false;
- if( IsLeapYear() ){
- if(m_unMonth == 3 && 1 == m_unDay){
- m_unDay = 29;
- bpriorMonth = true;
- }
- }
- else{
- if(m_unMonth == 3 && 1 == m_unDay){
- m_unDay = 28;
- bpriorMonth = true;
- }
- }
- if(bpriorMonth){
- goto PriorMonth;
- }
- else{
- if(IsLargeMonth(m_unMonth) && 1 == m_unDay){
- m_unDay = 31;
- bpriorMonth = true;
- }else if(!IsLargeMonth(m_unMonth) && 1 == m_unDay){
- m_unDay = 30;
- bpriorMonth = true;
- }
- }
- if(bpriorMonth){
- goto PriorMonth;
- }
- else{
- --m_unDay;
- goto Over;
- }
- PriorMonth:
- {
- //is end of the year
- if(1 == m_unMonth){
- m_unMonth = 12;
- --m_unYear;
- }
- else{
- --m_unMonth;
- }
- }
- Over:
- return (*this);
- }
- //#########################################################################
- //get current date
- void CTimeEx::NowTime()
- {
- SYSTEMTIME systime;
- memset(&systime, 0x0, sizeof(systime));
- GetLocalTime(&systime);
- this->m_unHour = systime.wHour;
- this->m_unMinu = systime.wMinute;
- this->m_unSecn = systime.wSecond;
- }
- //get the time string text
- void CTimeEx::AsTString(LPSTR lpzTime, bool bFixedFmt)
- {
- if(NULL == lpzTime){
- throw "param is null, invalid memory!!!";
- }
- memset(lpzTime, 0x0, MAX_TIME_SIZE);
- if(bFixedFmt){
- sprintf(lpzTime, "%04d-%02d-%02d", m_unHour, m_unMinu, m_unSecn);
- }
- else{
- sprintf(lpzTime, "%d-%d-%d", m_unHour, m_unMinu, m_unSecn);
- }
- return ;
- }
- //operator "="
- CTimeEx CTimeEx::operator=(CTimeEx &dtTime)
- {
- this->m_unHour = dtTime.Hour();
- this->m_unMinu = dtTime.Minute();
- this->m_unSecn = dtTime.Second();
-
- return (*this);
- }
- //operator "=="
- bool CTimeEx::operator==(CTimeEx &dtTime)
- {
- if( this->m_unHour == dtTime.Hour() &&
- this->m_unMinu == dtTime.Minute() &&
- this->m_unSecn == dtTime.Second())
- return true;
- else
- return false;
- }
- //operator "!="
- bool CTimeEx::operator!=(CTimeEx &dtTime)
- {
- return !(*this==dtTime);
- }
- //operator ">"
- bool CTimeEx::operator>(CTimeEx &dtTime)
- {
- if( this->m_unHour >= dtTime.Hour() &&
- this->m_unMinu >= dtTime.Minute() &&
- this->m_unSecn > dtTime.Second())
- return true;
- else
- return false;
- }
- //operator ">="
- bool CTimeEx::operator>=(CTimeEx &dtTime)
- {
- return (*this == dtTime || *this > dtTime);
- }
- //operator "<"
- bool CTimeEx::operator<(CTimeEx &dtTime)
- {
- return !(*this >= dtTime);
- }
- //operator "<="
- bool CTimeEx::operator<=(CTimeEx &dtTime)
- {
- return (*this == dtTime || *this < dtTime);
- }
- //operator "-"
- DWORD CTimeEx::operator-(CTimeEx &dtTime)
- {
- CTimeEx dtBegin,
- dtEnd;
- if(*this >= dtTime){
- dtBegin = dtTime;
- dtEnd = *this;
- }
- else{
- dtBegin = *this;
- dtEnd = dtTime;
- }
- //calc the seconds value
- DWORD dwret = (dtEnd.Hour() - dtBegin.Hour()) * 3600 ;
- dwret += (dtEnd.Minute() - dtBegin.Minute()) * 60;
- dwret += (dtEnd.Second() - dtBegin.Second());
- return dwret;
- }
- //operator "++"(add one second)
- CTimeEx CTimeEx::operator++(int)
- {
- if(59 == m_unSecn){
- m_unSecn = 0;
- if(59 == m_unMinu){
- m_unMinu = 0;
- if(23 == m_unHour){
- m_unHour = 0;
- }
- else{
- ++m_unHour;
- }
- }
- else{
- ++m_unMinu;
- }
- }
- else{
- ++m_unSecn;
- }
- return (*this);
- }
- //operator "--"(sub one second)
- CTimeEx CTimeEx::operator--(int)
- {
- if(0 == m_unSecn){
- m_unSecn = 59;
- if(0 == m_unMinu){
- m_unMinu = 59;
- if(0 == m_unHour){
- m_unHour = 23;
- }
- else{
- --m_unHour;
- }
- }
- else{
- --m_unMinu;
- }
- }
- else{
- --m_unSecn;
- }
- return (*this);
- }
- //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- //constructor and destructor
- CDateTimeEx::CDateTimeEx()
- {
- this->Now();
- }
- //updown inherited static cast change
- CDateTimeEx::CDateTimeEx(CDateEx &dtDate)
- {
- this->m_unYear = dtDate.Year();
- this->m_unMonth = dtDate.Month();
- this->m_unDay = dtDate.Day();
- }
- CDateTimeEx::CDateTimeEx(CTimeEx &dtTime)
- {
- this->m_unHour = dtTime.Hour();
- this->m_unMinu = dtTime.Minute();
- this->m_unSecn = dtTime.Second();
- }
- //SYSTEMTIME change to CDateTimeEx
- CDateTimeEx::CDateTimeEx(const SYSTEMTIME stTime)
- {
- equalSysTime(stTime);
- }
- //FILETIME change to CDateTimeEx
- CDateTimeEx::CDateTimeEx(const FILETIME ftTime)
- {
- try{
- equalFileTime(ftTime);
- }
- catch(std::exception e){
- throw e.what();
- }
- }
- //DATE change to CDateTimeEx
- CDateTimeEx::CDateTimeEx(const DATE dtSrc)
- {
- struct tm tmDest;
- memset(&tmDest, 0x0, sizeof(tmDest));
- if( this->DTFromOleDate(dtSrc, tmDest) ){
- this->m_unYear = tmDest.tm_year;
- this->m_unMonth = tmDest.tm_mon + 1;
- this->m_unDay = tmDest.tm_mday;
- this->m_unHour = tmDest.tm_hour;
- this->m_unMinu = tmDest.tm_min;
- this->m_unSecn = tmDest.tm_sec;
- }
- }
- //"struct tm" change to CDateTimeEx
- CDateTimeEx::CDateTimeEx(struct tm &tmDest)
- {
- this->m_unYear = tmDest.tm_year;
- this->m_unMonth = tmDest.tm_mon + 1;
- this->m_unDay = tmDest.tm_mday;
-
- this->m_unHour = tmDest.tm_hour;
- this->m_unMinu = tmDest.tm_min;
- this->m_unSecn = tmDest.tm_sec;
- }
- //operator "=" --- system time
- CDateTimeEx& CDateTimeEx::operator=(const SYSTEMTIME stTime)
- {
- equalSysTime(stTime);
- return (*this);
- };
- //operator "=" --- file time
- CDateTimeEx& CDateTimeEx::operator=(const FILETIME ftTime)
- {
- try{
- equalFileTime(ftTime);
- }
- catch(std::exception e){
- throw e.what();
- }
- return (*this);
- }
- //get the now datetime info
- void CDateTimeEx::Now()
- {
- static_cast<CDateEx *>(this)->NowDate();
- static_cast<CTimeEx *>(this)->NowTime();
- }
- //get the datetime string text
- void CDateTimeEx::AsDTString(LPSTR lpzDateTime, bool bFixedFmt)
- {
- if(NULL == lpzDateTime){
- throw "param is null, invalid memory!!!";
- }
- memset(lpzDateTime, 0x0, MAX_DATE_SIZE + MAX_TIME_SIZE);
- if(bFixedFmt){
- sprintf(lpzDateTime, "%04d-%02d-%02d %02d:%02d:%02d",
- m_unYear, m_unMonth, m_unDay,
- m_unHour, m_unMinu, m_unSecn);
- }
- else{
- sprintf(lpzDateTime, "%d-%d-%d %d:%d:%d",
- m_unYear, m_unMonth, m_unDay,
- m_unHour, m_unMinu, m_unSecn);
- }
- return ;
- }
- void CDateTimeEx::equalSysTime(const SYSTEMTIME stTime)
- {
- this->m_unYear = stTime.wYear;
- this->m_unMonth = stTime.wMonth;
- this->m_unDay = stTime.wDay;
- this->m_unHour = stTime.wHour;
- this->m_unMinu = stTime.wMinute;
- this->m_unSecn = stTime.wSecond;
- }
- void CDateTimeEx::equalFileTime(const FILETIME ftTime)
- {
- FILETIME lftTime;
- if( 0 == FileTimeToLocalFileTime(&ftTime, &lftTime) ){
- throw "FileTimeToLocalFileTime function is failed...";
- }
- //////////////////////////////////////////////////////////
- WORD wDate, wTime;
- if( 0 == FileTimeToDosDateTime(&lftTime, &wDate, &wTime) ){
- throw "FileTimeToDosDateTime function is failed...";
- }
- SYSTEMTIME stTime;
- memset(&stTime, 0x0, sizeof(stTime));
- //get the date and time value
- stTime.wDay = (wDate & 0x001F);
- stTime.wMonth = (wDate & 0x01E0) >> 5;
- stTime.wYear = ( (wDate & 0xFE00) >>9 ) + 1980;
-
- stTime.wSecond = (wTime & 0x001E) * 2;
- stTime.wMinute = (wTime >> 5) & 0x003F;
- stTime.wHour = (wTime >> 11) & 0x001F;
-
- equalSysTime(stTime);
- }
- //get "struct tm" from DATE structor
- bool CDateTimeEx::DTFromOleDate(DATE dtSrc, struct tm& tmDest)
- {
- // The legal range does not actually span year 0 to 9999.
- if (dtSrc > MAX_DATE || dtSrc < MIN_DATE) // about year 100 to about 9999
- return false;
-
- long nDays; // Number of days since Dec. 30, 1899
- long nDaysAbsolute; // Number of days since 1/1/0
- long nSecsInDay; // Time in seconds since midnight
- long nMinutesInDay; // Minutes in day
-
- long n400Years; // Number of 400 year increments since 1/1/0
- long n400Century; // Century within 400 year block (0,1,2 or 3)
- long n4Years; // Number of 4 year increments since 1/1/0
- long n4Day; // Day within 4 year block
- // (0 is 1/1/yr1, 1460 is 12/31/yr4)
- long n4Yr; // Year within 4 year block (0,1,2 or 3)
- BOOL bLeap4 = TRUE; // TRUE if 4 year block includes leap year
-
- double dblDate = dtSrc; // tempory serial date
-
- // If a valid date, then this conversion should not overflow
- nDays = (long)dblDate;
-
- // Round to the second
- dblDate += ((dtSrc > 0.0) ? HALF_SECOND : -HALF_SECOND);
-
- nDaysAbsolute = (long)dblDate + 693959L; // Add days from 1/1/0 to 12/30/1899
-
- dblDate = fabs(dblDate);
- nSecsInDay = (long)((dblDate - floor(dblDate)) * 86400.0);
-
- // Calculate the day of week (sun=1, mon=2...)
- // -1 because 1/1/0 is Sat. +1 because we want 1-based
- tmDest.tm_wday = (int)((nDaysAbsolute - 1) % 7L) + 1;
-
- // Leap years every 4 yrs except centuries not multiples of 400.
- n400Years = (long)(nDaysAbsolute / 146097L);
-
- // Set nDaysAbsolute to day within 400-year block
- nDaysAbsolute %= 146097L;
-
- // -1 because first century has extra day
- n400Century = (long)((nDaysAbsolute - 1) / 36524L);
-
- // Non-leap century
- if (n400Century != 0)
- {
- // Set nDaysAbsolute to day within century
- nDaysAbsolute = (nDaysAbsolute - 1) % 36524L;
-
- // +1 because 1st 4 year increment has 1460 days
- n4Years = (long)((nDaysAbsolute + 1) / 1461L);
-
- if (n4Years != 0)
- n4Day = (long)((nDaysAbsolute + 1) % 1461L);
- else
- {
- bLeap4 = FALSE;
- n4Day = (long)nDaysAbsolute;
- }
- }
- else
- {
- // Leap century - not special case!
- n4Years = (long)(nDaysAbsolute / 1461L);
- n4Day = (long)(nDaysAbsolute % 1461L);
- }
-
- if (bLeap4)
- {
- // -1 because first year has 366 days
- n4Yr = (n4Day - 1) / 365;
-
- if (n4Yr != 0)
- n4Day = (n4Day - 1) % 365;
- }
- else
- {
- n4Yr = n4Day / 365;
- n4Day %= 365;
- }
-
- // n4Day is now 0-based day of year. Save 1-based day of year, year number
- tmDest.tm_yday = (int)n4Day + 1;
- tmDest.tm_year = n400Years * 400 + n400Century * 100 + n4Years * 4 + n4Yr;
-
- // Handle leap year: before, on, and after Feb. 29.
- if (n4Yr == 0 && bLeap4)
- {
- // Leap Year
- if (n4Day == 59)
- {
- /* Feb. 29 */
- tmDest.tm_mon = 2;
- tmDest.tm_mday = 29;
-
- goto DoTime;
- }
-
- // Pretend it's not a leap year for month/day comp.
- if (n4Day >= 60)
- --n4Day;
- }
-
- // Make n4DaY a 1-based day of non-leap year and compute
- // month/day for everything but Feb. 29.
- ++n4Day;
-
- // Month number always >= n/32, so save some loop time */
- for (tmDest.tm_mon = (n4Day >> 5) + 1;
- n4Day > rgMonthDays[tmDest.tm_mon]; tmDest.tm_mon++);
-
- tmDest.tm_mday = (int)(n4Day - rgMonthDays[tmDest.tm_mon-1]);
-
- DoTime:
- if (nSecsInDay == 0)
- tmDest.tm_hour = tmDest.tm_min = tmDest.tm_sec = 0;
- else
- {
- tmDest.tm_sec = (int)nSecsInDay % 60L;
- nMinutesInDay = nSecsInDay / 60L;
- tmDest.tm_min = (int)nMinutesInDay % 60;
- tmDest.tm_hour = (int)nMinutesInDay / 60;
- }
-
- return true;
- }
- //######################################################################
- bool CDateTimeEx::OleDateFromDT(WORD wYear, WORD wMonth, WORD wDay,
- WORD wHour, WORD wMinute, WORD wSecond, DATE& dtDest)
- {
- // Validate year and month (ignore day of week and milliseconds)
- if (wYear > 9999 || wMonth < 1 || wMonth > 12)
- return false;
-
- // Check for leap year and set the number of days in the month
- BOOL bLeapYear = ((wYear & 3) == 0) &&
- ((wYear % 100) != 0 || (wYear % 400) == 0);
-
- int nDaysInMonth =
- rgMonthDays[wMonth] - rgMonthDays[wMonth-1] +
- ((bLeapYear && wDay == 29 && wMonth == 2) ? 1 : 0);
-
- // Finish validating the date
- if (wDay < 1 || wDay > nDaysInMonth ||
- wHour > 23 || wMinute > 59 ||
- wSecond > 59)
- {
- return false;
- }
-
- // Cache the date in days and time in fractional days
- long nDate;
- double dblTime;
-
- //It is a valid date; make Jan 1, 1AD be 1
- nDate = wYear*365L + wYear/4 - wYear/100 + wYear/400 +
-
- rgMonthDays[wMonth-1] + wDay;
-
- // If leap year and it's before March, subtract 1:
- if (wMonth <= 2 && bLeapYear)
- --nDate;
-
- // Offset so that 12/30/1899 is 0
- nDate -= 693959L;
-
- dblTime = (((long)wHour * 3600L) + // hrs in seconds
- ((long)wMinute * 60L) + // mins in seconds
- ((long)wSecond)) / 86400.;
-
- dtDest = (double) nDate + ((nDate >= 0) ? dblTime : -dblTime);
-
- return true;
- }
- //get OleDate
- bool CDateTimeEx::getOleDate(struct tm *tmTime)
- {
- if(NULL==tmTime){
- return false;
- }
- //start conversion
- DATE dtDest;
- memset(&dtDest, 0x0, sizeof(dtDest));
- if( !this->OleDateFromDT(this->Year(), this->Month(), this->Day(),
- this->Hour(), this->Minute(), this->Second(), dtDest) ){
- return false;
- }
-
- memset(tmTime, 0x0, sizeof(struct tm));
- if( !this->DTFromOleDate(dtDest, *tmTime) ){
- return false;
- }
- return true;
- };
- //day of week
- WEEKDAY CDateTimeEx::DayOfWeek()
- {
- struct tm tmTime;
- if( !this->getOleDate(&tmTime) ){
- return WD_ERROR;
- }
- else{
- switch(tmTime.tm_wday-1) {
- case 0:
- return WD_SUNDAY;
- break;
-
- case 1:
- return WD_MONDAY;
- break;
- case 2:
- return WD_TUESDAY;
- break;
-
- case 3:
- return WD_WEDNESDAY;
- break;
- case 4:
- return WD_THURSDAY;
- break;
- case 5:
- return WD_FRIDAY;
- break;
- case 6:
- return WD_SATURDAY;
- break;
- default:
- return WD_ERROR;
- break;
- }
- }
- }