/*! \brief Get the year, month, and day in month from a week in year \details An ISO week-numbering year has 52 or 53 full weeks (364 or 371 days) \param year [in, out] Gregorian year \param weekInYear [in] Gregorian week in a year \param month [out] Gregorian month \param dayInMonth [out] Gregorian day in month \returns \c True if success or \c False if error */ bool GetDayAndMonthFromWeekInYear(int *year, int weekInYear, int *month, int *dayInMonth) { int daysInMonth; int weeksInYear; *month = ISO_FIRST_MONTH_IN_YEAR; bool hasChanged; do { hasChanged = false; weeksInYear = GetNumberOfWeeksInYear(*year); if (weekInYear > weeksInYear) { weekInYear -= weeksInYear; (*year)++; hasChanged = true; } } while(hasChanged); int dayInYear = (weekInYear - 1) * ISO_DAYS_A_WEEK + 1; // Since the first day of week 1 in a year in the Gregorian calendar is not usually January 1st we need to handle the offset static int t[] = {0, 0, -1, -2, -3, 3, 2, 1}; int jan1DayOfWeek = GetDayOfTheWeek(*year, 1, 1); dayInYear += t[jan1DayOfWeek]; if (dayInYear <= 0) { // dayInYear is in the previous year (*year)--; dayInYear += GetDaysInYear(*year); } else { int daysInYear = GetDaysInYear(*year); if (dayInYear > daysInYear) { // dayInYear is in the next year (*year)++; dayInYear -= daysInYear; } } if (!GetDayAndMonthFromDayInYear(*year, dayInYear, month, dayInMonth)) return false; return true; }
int Date::ComputeDaysFromYearStart(int year, int month, int day) { int days=day-1; for(int i=month;month>1;month--) { if(month==12) days+=30; else if(month==11) days+=31; else if(month==10) days+=30; else if(month==9) days+=31; else if(month==8) days+=31; else if(month==7) days+=30; else if(month==6) days+=31; else if(month==5) days+=30; else if(month==4) days+=31; else if(month==3) { if(GetDaysInYear(year)==366) days+=29; else days+=28; } else if(month==2) days+=31; } return days; }
static void FirstSetYearAndDayOfYear(RtcTime * self) { int days = self->daysSince1980; int year = STARTING_YEAR; int daysInYear = GetDaysInYear(year); while (days > daysInYear) { year++; days -= daysInYear; daysInYear = GetDaysInYear(year); } self->dayOfYear = days; self->year = year; }
CDate::CDate( int num ) { if ( num < 0 ) { isNull = true; } else { isNull = false; year = 1; day = 1; month = 1; while ( num >= GetDaysInYear( year ) ) { num -= GetDaysInYear( year++ ); } while ( num >= GetDaysInMonth( month, year ) ) { num -= GetDaysInMonth( month++, year ); } day += num; } }
int GetDaysFrom1984To(int day, int month, int year) { int y; int32 days=0; if (year<1984) return -1; for (y=1984; y<year;y++) { days+=GetDaysInYear(y); } days+=GetDOY(day, month, year); return days; }
CDate::operator int() const { if ( isNull ) { return -1; } else { int i, num = 0; for ( i=1; i<year; i++ ) { num += GetDaysInYear( i ); } for ( i=1; i<month; i++ ) { num += GetDaysInMonth( i, year ); } num += ( day - 1 ); return num; } }
void Date::NextDay(int n ) { day_+=n; bool OK=true; while(OK) { if(year_%4==0&&month_==2) { if(year_%100!=0||year_%400==0) { if(day_>29) { month_++; day_-=29; } } if(year_%100==0) { if(day_>29) { month_++; day_-=29; } } } if(month_==1||month_==3||month_==5||month_==7||month_==8||month_==10||month_==12) { if(day_>31) { month_++; day_-=31; } if(month_>12) { month_=1; year_++; } } else if(month_==4||month_==6||month_==9||month_==11) { if(day_>30) { month_++; day_-=30; } } if(month_==2) { if(day_>28) { month_++; day_-=30; } } if(day_<=0) { if(month_!=1) { month_--; if(month_==1||month_==3||month_==5||month_==7||month_==8||month_==10) day_+=31; else if(month_==4||month_==6||month_==9||month_==11) day_+=30; else if(month_==2) { if(GetDaysInYear(year_)==366) day_+=29; else day_+=28; } } if(month_==1) { month_=12; year_--; day_+=31; } } if(month_==1||month_==3||month_==5||month_==7||month_==8||month_==10||month_==12) { if(day_<=31&&day_>0) OK=false; } else if(month_==4||month_==6||month_==9||month_==11&&day_<=30&&day_>0) { if(day_<=30&&day_>0) OK=false; } else if(month_==2&&GetDaysInYear(year_)==366) { if(day_<=29&&day_>0) OK=false; } else if(month_==2&&GetDaysInYear(year_)==365) { if(day_<=28&&day_>0) OK=false; } } }