当前位置: 首页>>代码示例>>C++>>正文


C++ Date::DaysInMonth方法代码示例

本文整理汇总了C++中Date::DaysInMonth方法的典型用法代码示例。如果您正苦于以下问题:C++ Date::DaysInMonth方法的具体用法?C++ Date::DaysInMonth怎么用?C++ Date::DaysInMonth使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Date的用法示例。


在下文中一共展示了Date::DaysInMonth方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: Difference

// difference *this - OtherDate
unsigned long Date::Difference(Date &OtherDate, TimeUnit Unit, bool RoundUp) const {
	unsigned long Result = 0;
	
	switch (Unit) {
		case YEAR:
			Result = Data[YEAR] - OtherDate.Data[YEAR];
			break;
			
		case MONTH:
			Result = (Data[YEAR] - OtherDate.Data[YEAR])*12 + Data[MONTH] - OtherDate.Data[MONTH];
			break;
			
		case DAY:
			if (OtherDate.Data[YEAR] == Data[YEAR]) {
				if (OtherDate.Data[MONTH] == Data[MONTH]) {
					Result = Data[DAY] - OtherDate.Data[DAY];
				} else {
					Result = (OtherDate.DaysInMonth() - OtherDate.Data[DAY]) + Data[DAY];
					
					for (char i=OtherDate.Data[MONTH]+1; i<Data[MONTH]; ++i)
						Result += OtherDate.DaysInMonth(i);
				}
			} else {
				// number of days in the starting year
				Result = OtherDate.DaysInMonth() - OtherDate.Data[DAY];
				for (char i=OtherDate.Data[MONTH]+1; i<=12; ++i)
					Result += OtherDate.DaysInMonth(i);
				
				// number of days in years between the starting and ending years
				for (short i=OtherDate.Data[YEAR]+1; i<Data[YEAR]; ++i)
					Result += IsLeapYear(i) ? 366 : 365;
				
				// number of days in the ending year
				for (char i=1; i<Data[MONTH]; ++i)
					Result += DaysInMonth(i);
				Result += Data[DAY];
			}
			
			break;
			
		case HOUR:
			if (Data[DAY] == OtherDate.Data[DAY]) {
				Result = Data[HOUR] - OtherDate.Data[HOUR];
			} else {
				Result = 24 - OtherDate.Data[HOUR];
				Result += (Difference(OtherDate, DAY, false) - 1) * 24;
				Result += Data[HOUR];
			}
			break;
			
		case MINUTE:
			if (Data[HOUR] == OtherDate.Data[HOUR]) {
				Result = Data[MINUTE] - OtherDate.Data[MINUTE];
			} else {
				Result = 60 - OtherDate.Data[MINUTE];
				Result += (Difference(OtherDate, HOUR, false) - 1) * 60;
				Result += Data[MINUTE];
			}
			break;
			
		case SECOND:
			if (Data[MINUTE] == OtherDate.Data[MINUTE]) {
				Result = Data[SECOND] - OtherDate.Data[SECOND];
			} else {
				Result = 60 - OtherDate.Data[SECOND];
				Result += (Difference(OtherDate, MINUTE, false) - 1) * 60;
				Result += Data[SECOND];
			}
			break;
			
		case WEEK:
			Result = Difference(OtherDate, DAY, false)/7 + 1;
			break;
	}
	
	if (RoundUp == true && Unit < SECOND && Compare(OtherDate, (TimeUnit)(Unit+1)) > 0)
		++Result;
	return Result;
}
开发者ID:jgonera,项目名称:icalendarlib,代码行数:80,代码来源:date.cpp


注:本文中的Date::DaysInMonth方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。