datetime.cpp
来自「这是VCF框架的代码」· C++ 代码 · 共 1,226 行 · 第 1/2 页
CPP
1,226 行
//DateTime.cpp/*Copyright 2000-2004 The VCF Project.Please see License.txt in the top level directorywhere you installed the VCF.*/#include "vcf/FoundationKit/FoundationKit.h"#include "vcf/FoundationKit/DateTime.h"using namespace VCF;#define TOSTRING_FORMAT "%#d %b %Y %H:%M:%S.%s"#if defined(VCF_GCC) || defined(VCF_BCC8) #define BASIC_GREGORIAN_TIME_IN_MS 198647424000000LL#else #define BASIC_GREGORIAN_TIME_IN_MS 198647424000000#endif///////////////////////////////////////////////////////////////////////////////// DateTime implementationDateTime::DateTime( time_t newTime ): time_(0){ tm gm = *localtime( &newTime ); set( gm.tm_year + 1900, gm.tm_mon+1, gm.tm_mday, gm.tm_hour, gm.tm_min, gm.tm_sec );}DateTime::DateTime( unsigned long year, unsigned long month, unsigned long day ): time_(0){ set( year, month, day );}DateTime::DateTime( unsigned long year, unsigned long month, unsigned long day, unsigned long hour, unsigned long minute, unsigned long second ){ set( year, month, day, hour, minute, second );}bool DateTime::isGregorianCalendarDate( const DateTime& dt ){ bool result = false; ulong64 gd = BASIC_GREGORIAN_TIME_IN_MS + (DateTime::ONEDAY-1); result = gd < dt.time_; return result;}bool DateTime::isGregorianCalendarDate( const unsigned long& year, const unsigned long& month, const unsigned long& day ){ /** this is the default determination. The correct determination of this is dependant on the country that the date is being considered for. For more info on the rules used for this, please see: http://www.tondering.dk/claus/cal/node3.html#SECTION00324000000000000000 Final implementation of this will use either : the current Locale from the System or the Locale of the DateTime object, if it is not NULL */ bool result = false; if ( (year == 1582) ) { if ( month == 10 ) { if ( day >= 5 ) { result = true; } } else if ( month > 10 ) { result = true; } } else if ( year > 1582 ) { result = true; } return result;}void DateTime::setDate( const unsigned long& year, const unsigned long& month, const unsigned long& day ){ unsigned long h; unsigned long m; unsigned long s; unsigned long ms; getHourMinuteSecond( *this, &h, &m, &s, &ms ); set( year, month, day, h, m, s, ms ) ;}void DateTime::setTime( const unsigned long& hour, const unsigned long& minutes, const unsigned long& seconds ){ unsigned long y; unsigned long m; unsigned long d; getYearMonthDay( *this, &y, &m, &d ); unsigned long ms = getMillisecond(); set( y, m, d, hour, minutes, seconds, ms );}void DateTime::set( const unsigned long& year, const unsigned long& month, const unsigned long& day, const unsigned long& hour, const unsigned long& minutes, const unsigned long& seconds, const unsigned long& milliseconds ){ /** see http://www.tondering.dk/claus/cal/node3.html#SECTION003151000000000000000 for julian day algorithm */ if ( (month > 12) || (month < 1) ) { throw BadDateFormat( BAD_MONTH_VALUE ); } unsigned int maxDayVal = DateTime::getNumberOfDaysInMonth( year, (DateTime::Months)month ); if ( (day > maxDayVal) || (day < 1) ) { throw BadDateFormat( BAD_DAY_VALUE ); } if ( (hour > 59) || (hour < 0) ) { throw BadTimeFormat( BAD_HOUR_VALUE ); } if ( (minutes > 59) || (minutes < 0) ) { throw BadTimeFormat( BAD_MIN_VALUE ); } if ( (seconds > 59) || (seconds < 0) ) { throw BadTimeFormat( BAD_SEC_VALUE ); } if ( (milliseconds > 999) || (milliseconds < 0) ) { throw BadTimeFormat( BAD_MILLISEC_VALUE ); } time_ = 0; int a = (14 - month)/12; int y = year + 4800 - a; int m = month + (12 * a ) - 3; if ( isGregorianCalendarDate( year, month, day ) ) { //use formula for gregorian dates time_ = (day + ((153*m + 2)/5) + (365*y) + (y/4) - (y/100) + (y/400) - 32045); time_ = time_ * DateTime::ONEDAY; time_ += (hour*60*60*1000) + (minutes*60*1000) + (seconds*1000) + milliseconds; } else { //use formula for Julian Calendar dates time_ = (day + ((153*m + 2)/5) + (365*y) + (y/4) - 32083); time_ = time_ * DateTime::ONEDAY; time_ += (hour*60*60*1000) + (minutes*60*1000) + (seconds*1000) + milliseconds; }}DateTime DateTime::toLocal() const{ return System::convertUTCTimeToLocalTime( *this );}DateTime DateTime::toUTC() const{ return System::convertLocalTimeToUTCTime( *this );}void DateTime::getYearMonthDay( const DateTime& dt, unsigned long* year, unsigned long* month, unsigned long* day ){ //need to know if the date is gregorian or not bool gregorianDay = isGregorianCalendarDate( dt ); int currentDay = dt.time_ / DateTime::ONEDAY; unsigned int a = currentDay + 32044; unsigned int b = 0; unsigned int c = 0; if ( gregorianDay ) { b = (4*a + 3)/146097; c = a - (b * 146097)/4; } else { c = currentDay + 32082; } int d = (4*c + 3)/1461; int e = c - (1461*d)/4; int m = (5*e + 2)/153; int tmp = 0; if ( NULL != year ) { tmp = (100 * b) + d - 4800 + m/10; *year = (tmp > 0) ? tmp : 0; } if ( NULL != month ) { tmp = m + 3 - (12 * (m/10)); *month = tmp > 0 ? tmp : 0; } if ( NULL != day ) { tmp = e - ((153*m) + 2)/5 + 1; *day = tmp > 0 ? tmp : 0; }}void DateTime::getHourMinuteSecond( const DateTime& dt, unsigned long* hour, unsigned long* minute, unsigned long* second, unsigned long* millsecond/*=NULL*/ ){ if ( NULL != hour ) { *hour = ((dt.time_ % DateTime::ONEDAY) / DateTime::ONEHOUR ); } if ( NULL != minute ) { *minute = ((dt.time_ % DateTime::ONEHOUR) / DateTime::ONEMINUTE ); } if ( NULL != second ) { *second = ((dt.time_ % DateTime::ONEMINUTE) / DateTime::ONESECOND ); } if ( NULL != millsecond ) { *millsecond = ((dt.time_ % DateTime::ONESECOND)); }}unsigned long DateTime::getYear() const{ unsigned long result = 0; getYearMonthDay( *this, &result, NULL, NULL ); return result;}unsigned long DateTime::getMonth() const{ unsigned long result = 0; getYearMonthDay( *this, NULL, &result, NULL ); return result;}unsigned long DateTime::getDay() const{ unsigned long result = 0; getYearMonthDay( *this, NULL, NULL, &result ); return result;}unsigned long DateTime::getHour() const{ unsigned long result = 0; result = ((time_ % DateTime::ONEDAY) / DateTime::ONEHOUR ); return result;}unsigned long DateTime::getMinute() const{ unsigned long result = 0; result = ((time_ % DateTime::ONEHOUR) / DateTime::ONEMINUTE ); return result;}unsigned long DateTime::getSecond() const{ unsigned long result = 0; result = ((time_ % DateTime::ONEMINUTE) / DateTime::ONESECOND ); return result;}unsigned long DateTime::getMillisecond() const{ unsigned long result = 0; result = ((time_ % DateTime::ONESECOND)); return result;}void DateTime::setCurrent( DateTime& dt ){ System::setDateToLocalTime( &dt );}DateTime& DateTime::incrYear(const unsigned long& by){ DateTime::Iterator<ByYear> it = *this; it+=(by); *this = *it; return *this;}DateTime& DateTime::incrMonth(const unsigned long& by){ DateTime::Iterator<ByMonth> it = *this; it+=(by); *this = *it; return *this;}DateTime& DateTime::incrDay(const unsigned long& by){ DateTime::Iterator<ByDay> it = *this; it+=(by); *this = *it; return *this;}DateTime& DateTime::incrHour(const unsigned long& by){ DateTime::Iterator<ByHour> it = *this; it+=(by); *this = *it; return *this;}DateTime& DateTime::incrMinute(const unsigned long& by){ DateTime::Iterator<ByMinute> it = *this; it+=(by); *this = *it; return *this;}DateTime& DateTime::incrSecond(const unsigned long& by){ DateTime::Iterator<BySecond> it = *this; it+=(by); *this = *it; return *this;}DateTime& DateTime::incrMilliSecond(const unsigned long& by){ DateTime::Iterator<ByMillisecond> it = *this; it+=(by); *this = *it; return *this;}DateTime& DateTime::decrYear(const unsigned long& by){ DateTime::Iterator<ByYear> it = *this; it-=(by); *this = *it; return *this;}DateTime& DateTime::decrMonth(const unsigned long& by){ DateTime::Iterator<ByMonth> it = *this; it-=(by); *this = *it; return *this;}DateTime& DateTime::decrDay(const unsigned long& by){ DateTime::Iterator<ByDay> it = *this; it-=(by); *this = *it; return *this;}DateTime& DateTime::decrHour(const unsigned long& by){ DateTime::Iterator<ByHour> it = *this; it-=(by); *this = *it; return *this;}DateTime& DateTime::decrMinute(const unsigned long& by){ DateTime::Iterator<ByMinute> it = *this; it-=(by); *this = *it; return *this;}DateTime& DateTime::decrSecond(const unsigned long& by){ DateTime::Iterator<BySecond> it = *this; it-=(by); *this = *it; return *this;}DateTime& DateTime::decrMilliSecond(const unsigned long& by){ DateTime::Iterator<ByMillisecond> it = *this; it-=(by); *this = *it; return *this;}void DateTime::setAndAdjustForGregorianDay( const unsigned long& year, const unsigned long& month, const unsigned long& day, const unsigned long& hour, const unsigned long& minutes, const unsigned long& seconds, const unsigned long& milliseconds ){ bool gregorianDate = DateTime::isGregorianCalendarDate( *this ); set( year, month, day, hour, minutes, seconds, milliseconds ); bool newGregorianDate = DateTime::isGregorianCalendarDate( *this ); if ( gregorianDate != newGregorianDate ) { if ( !gregorianDate ) { set( getYear(), getMonth(), getDay()+10, getHour(), getMinute(), getSecond(), getMillisecond() ); } }}DateTime::WeekDay DateTime::getWeekDay() const{ unsigned long year = 0; unsigned long month = 0; unsigned long day = 0; getYearMonthDay( *this, &year, &month, &day ); unsigned long a = (unsigned long) floor( static_cast<float>( (14.0f - month) / 12.0f) ); unsigned long y = year -a ; unsigned long m = month + (12 * a) - 2; unsigned long d = (unsigned long) (day + y + floor(static_cast<float>(y / 4.0f)) - floor(static_cast<float>(y/100)) + floor(static_cast<float>(y/400)) + floor(static_cast<float>((31 * m)/12))); return (DateTime::WeekDay)(d % 7);}unsigned long DateTime::getNumberOfDaysInMonth() const{ unsigned long year = 0; unsigned long month = 0; getYearMonthDay( *this, &year, &month, NULL ); return getNumberOfDaysInMonth( year, (DateTime::Months)month );}bool DateTime::isLeapYear( unsigned long year ){ bool result = (year % 4) == 0; if ( result ) { if ( ((year % 100) == 0) && !((year % 400) == 0) ) { result = false; } } return result;}bool DateTime::isLeapYear() const{ return DateTime::isLeapYear( getYear() );}unsigned long DateTime::getNumberOfDaysInMonth( unsigned long year, DateTime::Months month ){ unsigned long result = 0; switch( month ) { case DateTime::Feb : { result = DateTime::isLeapYear( year ) ? 29 : 28; } break; case DateTime::Apr : case DateTime::Jun : case DateTime::Sep : case DateTime::Nov : { result = 30; } break; case DateTime::Jan : case DateTime::Mar : case DateTime::May : case DateTime::Jul : case DateTime::Aug : case DateTime::Oct : case DateTime::Dec : { result = 31; } break; } return result;}unsigned long DateTime::getDayOfYear() const{ unsigned long result = 0; DateTime startOfYear; startOfYear.set( getYear(), 1, 1, getHour(), getMinute(), getSecond(), getMillisecond() ); ulong64 diff = time_ - startOfYear.time_; //+1 is added so we get a 1 based result - otherwise it'd be zero based result = (diff / DateTime::ONEDAY) + 1; return result;}unsigned long DateTime::getDaysInYear() const{ unsigned long result = 365; if ( isGregorianCalendarDate( *this ) ) { if ( isLeapYear() ) { result ++; } } return result;}bool DateTime::operator> ( const DateTime& rhs ) const{ return time_ > rhs.time_;}bool DateTime::operator>= ( const DateTime& rhs ) const{ return time_ >= rhs.time_;}bool DateTime::operator< ( const DateTime& rhs ) const{ return time_ < rhs.time_;}bool DateTime::operator<= ( const DateTime& rhs ) const{ return time_ <= rhs.time_;}bool DateTime::operator== ( const DateTime& rhs ) const{ return time_ == rhs.time_;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?