datetime.cpp

来自「这是VCF框架的代码」· C++ 代码 · 共 1,226 行 · 第 1/2 页

CPP
1,226
字号
bool DateTime::operator!= ( const DateTime& rhs ) const{	return time_ != rhs.time_;}DateTime& DateTime::operator =( const time_t& rhs ){	tm gm = *localtime( &rhs );	set( gm.tm_year + 1900, gm.tm_mon+1, gm.tm_mday, gm.tm_hour, gm.tm_min, gm.tm_sec );	return *this;}time_t DateTime::getCTime() const{	time_t result = 0;	DateTime unixCtime( 1970, 1, 1 ) ;	result = ((time_ - unixCtime.time_) / 1000);	return result;}DateTime::DayTime DateTime::getDayTime() const{	DayTime result = DateTime::AM;	result = getHour() >= 12 ? DateTime::PM : DateTime::AM;	return result;}bool DateTime::isAM() const{	return getHour() < 12;}bool DateTime::isPM() const{	return getHour() >= 12;}bool DateTime::firstDayOfTheWeek() const{	return getWeekDay() == DateTime::Sun;}bool DateTime::lastDayOfTheWeek() const{	return getWeekDay() == DateTime::Sat;}bool DateTime::firstDayOfTheMonth() const{	return getDay() == 1;}bool DateTime::lastDayOfTheMonth() const{	return getDay() == getNumberOfDaysInMonth();}bool DateTime::firstDayOfTheYear() const{	return 1 == getDayOfYear();}bool DateTime::lastDayOfTheYear() const{	return getDayOfYear() == getDaysInYear();}DateTime DateTime::operator+( const DateTimeSpan& rhs ) const{	DateTime result = *this;	result.time_ += rhs;	return result;}DateTime DateTime::operator-( const DateTimeSpan& rhs ) const{	DateTime result = *this;	result.time_ -= rhs;	return result;}DateTimeSpan DateTime::operator-( const DateTime& rhs ) const{	DateTimeSpan result;	if ( time_ > rhs.time_ ) {		result.subtract( *this , rhs );	}	else {		result.subtract( rhs, *this );	}	return result;}ulong64 DateTime::getMilliseconds() const{	return time_;}void DateTime::setMilliseconds( const ulong64& milliseconds ){	time_ = milliseconds;}unsigned long DateTime::getWeekOfYearStartingMon() const{	//based on ISO 8601	//code shamelessly swiped from http://www.merlyn.demon.co.uk/weekinfo.htm	// J R Stockton, any problems are my fault	unsigned long result = 0;	DateTime dt(*this);	int day = dt.getWeekDay();	if ( day == 0 ) {		day = 7;	}	int month = dt.getMonth();	int d = dt.getDay() + (4 - day);	int maxDays = dt.getNumberOfDaysInMonth();	if ( (d < 1) && (month==1) ) {		//use week of prev year		dt.decrYear();		result = dt.getWeeksInYear();	}	else {		dt.set( dt.getYear(), month, maxVal<>(1, minVal<>(maxDays,d)) ) ;//max( 1, dt.getDay() + 4 - day ) );		result = (unsigned long) floor(static_cast<float>((dt.getDayOfYear()-1) / 7 )) + 1;	}	return result;}unsigned long DateTime::getWeekOfYearStartingSun() const{	//based on ISO 8601	//code shamelessly swiped from http://www.merlyn.demon.co.uk/weekinfo.htm	// J R Stockton, any problems are my fault	unsigned long result = 0;	DateTime dt(*this);	int day = dt.getWeekDay() + 1;	int month = dt.getMonth();	int d = dt.getDay() + (4 - day);	int maxDays = dt.getNumberOfDaysInMonth();	if ( (d < 1) && (month==1) ) {		//use week of prev year		dt.decrYear();		result = dt.getWeeksInYear();	}	else {		dt.set( dt.getYear(), month, maxVal<>(1, minVal<>(maxDays,d)) ) ;//max( 1, dt.getDay() + 4 - day ) );		result = (unsigned long) floor(static_cast<float>((dt.getDayOfYear()-1) / 7 )) + 1;	}	return result;}unsigned long DateTime::getWeeksInYear() const{	return (unsigned long) floor(static_cast<float>(getDaysInYear() / 7) ) + 1;}String DateTime::toString(){	return StringUtils::format( *this, TOSTRING_FORMAT );}void DateTime::loadFromStream( InputStream* stream ){	long hi = 0;	long lo = 0;	stream->read( hi );	stream->read( lo );	time_.lo( (unsigned long)lo );	time_.hi( (unsigned long)hi );}void DateTime::saveToStream( OutputStream* stream ){	long hi = time_.hi();	long lo = time_.lo();	stream->write( hi );	stream->write( lo );}void ByMillisecond::incr( DateTime& dt, unsigned long offset ){	dt.setMilliseconds( dt.getMilliseconds() + offset );}void ByMillisecond::decr( DateTime& dt, unsigned long offset ){	dt.setMilliseconds( dt.getMilliseconds() - offset );}void BySecond::incr( DateTime& dt, unsigned long offset ){	dt.setMilliseconds( dt.getMilliseconds() + (offset * DateTime::ONESECOND) );}void BySecond::decr( DateTime& dt, unsigned long offset ){	dt.setMilliseconds( dt.getMilliseconds() - (offset * DateTime::ONESECOND) );}void ByMinute::incr( DateTime& dt, unsigned long offset ){	dt.setMilliseconds( dt.getMilliseconds() + (offset * DateTime::ONEMINUTE) );}void ByMinute::decr( DateTime& dt, unsigned long offset ){	dt.setMilliseconds( dt.getMilliseconds() - (offset * DateTime::ONEMINUTE) );}void ByHour::incr( DateTime& dt, unsigned long offset ){	dt.setMilliseconds( dt.getMilliseconds() + (offset * DateTime::ONEHOUR) );}void ByHour::decr( DateTime& dt, unsigned long offset ){	dt.setMilliseconds( dt.getMilliseconds() - (offset * DateTime::ONEHOUR) );}void ByDay::incr( DateTime& dt, unsigned long offset ){	ulong64 offset64 = offset * DateTime::ONEDAY;	dt.setMilliseconds( dt.getMilliseconds() + offset64 );}void ByDay::decr( DateTime& dt, unsigned long offset ){	dt.setMilliseconds( dt.getMilliseconds() - (offset * DateTime::ONEDAY) );}void ByMonth::incr( DateTime& dt, unsigned long offset ){	long y = dt.getYear();	long m = dt.getMonth();	long d = dt.getDay();	int origMaxDaysInMon = DateTime::getNumberOfDaysInMonth( y, (DateTime::Months)m );	if ( (m + offset) > 12 ) {		y += (m + offset) / 12;		m = (m + offset) % 12;	}	else {		m += offset;	}	int newMaxDaysInMon = DateTime::getNumberOfDaysInMonth( y, (DateTime::Months)m );	if ( newMaxDaysInMon != origMaxDaysInMon ) {		//check to see if we need to move the day around		if ( d == origMaxDaysInMon ) {			//last day of the month			d = newMaxDaysInMon;		}		else if ( d > 28 ) {			//make it smaller			d = minVal<int>( d, newMaxDaysInMon );		}	}	dt.set( y, m, d, dt.getHour(), dt.getMinute(), dt.getSecond(), dt.getMillisecond() );}void ByMonth::decr( DateTime& dt, unsigned long offset ){	long y = dt.getYear();	long m = dt.getMonth();	long d = dt.getDay();	int origMaxDaysInMon = DateTime::getNumberOfDaysInMonth( y, (DateTime::Months)m );	if ( (m - offset) < 1 ) {		y -= ( abs((long)static_cast<float>( m - offset ) ) / 12) + 1;		m = 12 - ((m - offset) % 12);	}	else {		m -= offset;	}	int newMaxDaysInMon = DateTime::getNumberOfDaysInMonth( y, (DateTime::Months)m );	if ( newMaxDaysInMon != origMaxDaysInMon ) {		//check to see if we need to move the day around		if ( d == origMaxDaysInMon ) {			//last day of the month			d = newMaxDaysInMon;		}		else if ( d > 28 ) {			//make it smaller			d = minVal<int>( d, newMaxDaysInMon );		}	}	dt.set( y, m, d, dt.getHour(), dt.getMinute(), dt.getSecond(), dt.getMillisecond() );}void ByYear::incr( DateTime& dt, unsigned long offset ){	long y = dt.getYear();	long m = dt.getMonth();	long d = dt.getDay();	int origMaxDaysInMon = DateTime::getNumberOfDaysInMonth( y, (DateTime::Months)m );	y += offset;	int newMaxDaysInMon = DateTime::getNumberOfDaysInMonth( y, (DateTime::Months)m );	if ( newMaxDaysInMon != origMaxDaysInMon ) {		//check to see if we need to move the day around		if ( d == origMaxDaysInMon ) {			//last day of the month			d = newMaxDaysInMon;		}		else if ( d > 28 ) {			//make it smaller			d = minVal<int>( d, newMaxDaysInMon );		}	}	dt.set( y, m, d, dt.getHour(), dt.getMinute(), dt.getSecond(), dt.getMillisecond() );}void ByYear::decr( DateTime& dt, unsigned long offset ){	long y = dt.getYear();	long m = dt.getMonth();	long d = dt.getDay();	int origMaxDaysInMon = DateTime::getNumberOfDaysInMonth( y, (DateTime::Months)m );	y -= offset;	int newMaxDaysInMon = DateTime::getNumberOfDaysInMonth( y, (DateTime::Months)m );	if ( newMaxDaysInMon != origMaxDaysInMon ) {		//check to see if we need to move the day around		if ( d == origMaxDaysInMon ) {			//last day of the month			d = newMaxDaysInMon;		}		else if ( d > 28 ) {			//make it smaller			d = minVal<int>( d, newMaxDaysInMon );		}	}	dt.set( y, m, d, dt.getHour(), dt.getMinute(), dt.getSecond(), dt.getMillisecond() );}///////////////////////////////////////////////////////////////////////////////// DateTimeSpan implementationvoid DateTimeSpan::subtract( const DateTime& lhs, const DateTime& rhs ){	start_ = rhs.time_;	end_ = lhs.time_;	delta_ = end_ - start_;	DateTime::Iterator<ByMonth> monthIt = rhs;	unsigned int ey = lhs.getYear();	unsigned int em = lhs.getMonth();	unsigned int sy = rhs.getYear();		months_ = 0;	while ( true ) {		if ( (*monthIt).getYear() == ey ) {			if ( (*monthIt).getMonth() == em ) {				break;			}		}		++months_;		++monthIt;	}	years_ = abs(static_cast<int>(ey-sy));	if ( years_ > 0 ) {		if ( lhs > rhs ) {			if ( lhs.getMonth() < rhs.getMonth() ) {				years_ --;			}		}		else {			if ( rhs.getMonth() < lhs.getMonth() ) {				years_ --;			}		}	}	days_ = delta_ / DateTime::ONEDAY;}unsigned long DateTimeSpan::getYears() const{	return years_;}unsigned long DateTimeSpan::getMonths() const{	return months_ % 12;}unsigned long DateTimeSpan::getDays() const{	unsigned long result = 0;	DateTime end;	end.time_ = end_;	DateTime start;	start.time_ = start_;	result = abs((long)static_cast<float>(end.getDay() - start.getDay()));	return result;}unsigned long DateTimeSpan::getHours() const{	DateTime dt;	dt.time_ = delta_;	return dt.getHour();}unsigned long DateTimeSpan::getMinutes() const{	DateTime dt;	dt.time_ = delta_;	return dt.getMinute();}unsigned long DateTimeSpan::getSeconds() const{	DateTime dt;	dt.time_ = delta_;	return dt.getSecond();}unsigned long DateTimeSpan::getMilliseconds() const{	DateTime dt;	dt.time_ = delta_;	return dt.getMillisecond();}unsigned long DateTimeSpan::getTotalMonths() const{	return months_;}unsigned long DateTimeSpan::getTotalDays() const{	return days_;}unsigned long DateTimeSpan::getTotalHours() const{	return delta_ / DateTime::ONEHOUR;}unsigned long DateTimeSpan::getTotalMinutes() const{	return delta_ / DateTime::ONEMINUTE;}unsigned long DateTimeSpan::getTotalSeconds() const{	return delta_ / DateTime::ONESECOND;}ulong64 DateTimeSpan::getTotalMilliseconds() const{	return delta_;}/***CVS Log info*$Log$*Revision 1.4  2006/04/07 02:35:34  ddiego*initial checkin of merge from 0.6.9 dev branch.**Revision 1.3.4.4  2006/03/06 14:42:55  kiklop74*BDS 2006 fix**Revision 1.3.4.3  2005/12/28 21:02:21  kiklop74*Fixed ambiguity error when compiling with bcb6**Revision 1.3.4.2  2005/11/10 02:02:38  ddiego*updated the osx build so that it*compiles again on xcode 1.5. this applies to the foundationkit and graphicskit.**Revision 1.3.4.1  2005/11/10 00:03:48  obirsoy*changes required for gcc under Linux.**Revision 1.3  2004/12/01 04:31:40  ddiego*merged over devmain-0-6-6 code. Marcello did a kick ass job*of fixing a nasty bug (1074768VCF application slows down modal dialogs.)*that he found. Many, many thanks for this Marcello.**Revision 1.2.2.4  2004/08/26 04:05:47  marcelloptr*minor change on name of getMillisecond**Revision 1.2.2.3  2004/08/23 21:25:57  marcelloptr*just moved some member definitions around**Revision 1.2  2004/08/07 02:49:13  ddiego*merged in the devmain-0-6-5 branch to stable**Revision 1.1.2.6  2004/08/06 01:45:04  ddiego*updated and added Marcellos DateTime changes.**Revision 1.1.2.5  2004/08/03 20:57:22  marcelloptr*minor change on name DateTime:getSecond DateTime:getMillisecond**Revision 1.1.2.4  2004/07/30 17:28:40  kiklop74*Added first release of Borland midifications for VCF**Revision 1.1.2.3  2004/07/24 01:40:42  ddiego*committed changes requested by Marcello. Got rid of the remaining*date time members on the File class - now the dat time function call the*FilePeer directly each time. Also added 2 functions to DateTime to convert*directly to UTC or Local time.**Revision 1.1.2.2  2004/04/29 04:07:07  marcelloptr*reformatting of source files: macros and csvlog and copyright sections**Revision 1.1.2.1  2004/04/28 03:29:39  ddiego*migration towards new directory structure**Revision 1.3.2.3  2004/04/26 16:43:34  ddiego*checked in some minor fixes to some of the other*libraries due to switch over to unicode**Revision 1.3.2.2  2004/04/22 00:56:40  ddiego*fixed GCC complaint about too large of*an integer value in DateTime class**Revision 1.3.2.1  2004/04/06 17:19:16  ddiego*minor header changes to better facilitate compiling with*mingw. Also some minor changes to the devcpp FoundationKit project.**Revision 1.3  2004/02/16 20:38:17  ddiego*applied patch from Bobby Ward for further fixes for VC71 compiling**Revision 1.2  2004/01/20 01:54:56  ddiego*merged some more changes from dev branch, primarily changes to*teh RTTI API so that we now process enum sets correctly (i.e. a long*that is a mask made of enum values).**Revision 1.1.2.3  2004/01/17 18:47:31  ddiego*added a new example for DateTime calss and some other minor fixes to it**Revision 1.1.2.2  2004/01/17 06:09:50  ddiego*integrated the DateTime class into the VCF FoundationKit.**/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?