int DateTime::DayOfYear() const { int year, month, day; GetDateFromTimeValue( Value(), year, month, day); const unsigned* monthTable = IsLeapYear(year) ? monthStartLeap : monthStart; return monthTable[ month - 1 ] + day; }
int TDateTime::IsLeapYear() { unsigned int year=0; unsigned int month=0; unsigned int day=0; DecodeDate(year, month, day); return IsLeapYear(year); }
static double DayFromMonth(double year, double month) { int iMonth = (int) MathUtils::floor(month); if (iMonth < 0 || iMonth >= 12) { return MathUtils::kNaN; } return DayFromYear((int)year) + kMonthOffset[(int)IsLeapYear((int)year)][iMonth]; }
int Date::DayOfYear(int year, int month, int day) { static const int DAYS_BEFORE[2][13] = { { -1, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }, { -1, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 } }; return DAYS_BEFORE[IsLeapYear(year)][month] + day - 1; }
int Date::DaysInMonth(int year, int month) { static const int DAYS_IN_MONTH[2][13] = { { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } }; return DAYS_IN_MONTH[IsLeapYear(year)][month]; }
Calendar Calendar::GetAfterTwoMonthDay() const { if (this->Month == 12) { if (!IsLeapYear(this->Year + 1)) return this->Day >= 28 ? Calendar(this->Year + 1, 2, 28) : Calendar(this->Year + 1, 2, this->Day); else return this->Day >= 29 ? Calendar(this->Year + 1, 2, 29) : Calendar(this->Year + 1, 2, this->Day); } else if (this->Month == 11) return Calendar(this->Year + 1, 1, this->Day); else if (this->Month == 7 && this->Day == 31) return Calendar(this->Year, 9, 30); else return Calendar(this->Year, this->Month + 2, this->Day); }
Calendar Calendar::GetAfterAWeekDay() const { auto AfterAWeekDate = [this](const int AbleToSimpleAddBorderDay) { return this->Day <= AbleToSimpleAddBorderDay ? Calendar(this->Year, this->Month, this->Day + 7) : Calendar(this->Year, Month + 1, this->Day - AbleToSimpleAddBorderDay); }; if (this->Month == 12 && this->Day > 24) return Calendar(this->Year + 1, 1, this->Day - 24); else if (!IsNo31Month(this->Month)) return AfterAWeekDate(24); else if (this->Month != 2) return AfterAWeekDate(23); else return AfterAWeekDate((IsLeapYear(this->Year) ? 22 : 21)); }
int32 FDateTime::DaysInYear( int32 Year ) { if (IsLeapYear(Year)) { return 366; } return 365; }
void NextDay(){ day++; if(day>DayOfMonth[month][IsLeapYear(year)]){ day=1;month++; } if(month>12){ month=1;year++; } }
STATIC BOOLEAN ValidDay ( IN EFI_TIME time ) { if (time.Day > DayOfMonth[time.Month - 1]) { return FALSE; } // // Pay attention to month==2 // if (time.Month == 2 && ((IsLeapYear (time) && time.Day > 29) || (!IsLeapYear (time) && time.Day > 28))) { return FALSE; } return TRUE; }
// //////////////////////////////////////////////////////////////////////////// inline unsigned int MonthDayMax(unsigned int in_year, unsigned int in_month) { return((!YearMonthOk(in_year, in_month)) ? 0 : (((in_month == 1) || (in_month == 3) || (in_month == 5) || (in_month == 7) || (in_month == 8) || (in_month == 10) || (in_month == 12)) ? 31 : ((in_month == 4) || (in_month == 6) || (in_month == 9) || (in_month == 11)) ? 30 : (28 + ((IsLeapYear(in_year)) ? 1 : 0)))); }
STATIC BOOLEAN EFIAPI IsDayValid ( IN EFI_TIME *Time ) { ASSERT (Time->Day >= 1); ASSERT (Time->Day <= mDayOfMonth[Time->Month - 1]); ASSERT (Time->Month != 2 || IsLeapYear (Time) || Time->Day <= 28); if (Time->Day < 1 || Time->Day > mDayOfMonth[Time->Month - 1] || (Time->Month == 2 && !IsLeapYear (Time) && Time->Day > 28)) { return FALSE; } return TRUE; }
int CMyCalendar::MonthDays(int year,int month) { if(month==2) { if(IsLeapYear(year)) return 29; return 28; } return sMonthDays[month]; }
unsigned CTimer::GetDaysOfMonth (unsigned nMonth, unsigned nYear) { if ( nMonth == 1 && IsLeapYear (nYear)) { return 29; } return s_nDaysOfMonth[nMonth]; }
//returns the length of any month, ie length of November would be 30 int cTimeAndDate::LengthOfMonth(int month) const { int ret=-1; switch(month) { case 9: case 4: case 6: case 11: ret=30; break; case 1: case 3: case 5: case 7: case 8: case 10: case 12: ret=31; break; case 2: if(IsLeapYear()) ret=29; else ret=28; break; default: ret=-1; //invalid month } return ret; }
/****************************************************************************** * RtlTimeFieldsToTime [NTDLL.@] * */ BOOLEAN WINAPI RtlTimeFieldsToTime( PTIME_FIELDS tfTimeFields, PLARGE_INTEGER Time) { int CurYear, CurMonth; long long int rcTime; TIME_FIELDS TimeFields = *tfTimeFields; rcTime = 0; /* FIXME: normalize the TIME_FIELDS structure here */ while (TimeFields.Second >= SECSPERMIN) { NormalizeTimeFields(&TimeFields.Second, &TimeFields.Minute, SECSPERMIN); } while (TimeFields.Minute >= MINSPERHOUR) { NormalizeTimeFields(&TimeFields.Minute, &TimeFields.Hour, MINSPERHOUR); } while (TimeFields.Hour >= HOURSPERDAY) { NormalizeTimeFields(&TimeFields.Hour, &TimeFields.Day, HOURSPERDAY); } while (TimeFields.Day > MonthLengths[IsLeapYear(TimeFields.Year)][TimeFields.Month - 1]) { NormalizeTimeFields(&TimeFields.Day, &TimeFields.Month, SECSPERMIN); } while (TimeFields.Month > MONSPERYEAR) { NormalizeTimeFields(&TimeFields.Month, &TimeFields.Year, MONSPERYEAR); } /* FIXME: handle calendar corrections here */ for (CurYear = EPOCHYEAR; CurYear < TimeFields.Year; CurYear++) { rcTime += YearLengths[IsLeapYear(CurYear)]; } for (CurMonth = 1; CurMonth < TimeFields.Month; CurMonth++) { rcTime += MonthLengths[IsLeapYear(CurYear)][CurMonth - 1]; } rcTime += TimeFields.Day - 1; rcTime *= SECSPERDAY; rcTime += TimeFields.Hour * SECSPERHOUR + TimeFields.Minute * SECSPERMIN + TimeFields.Second; rcTime *= TICKSPERSEC; rcTime += TimeFields.Milliseconds * TICKSPERMSEC; *Time = *(LARGE_INTEGER *)&rcTime; return TRUE; }
/** * Converts a tuple of Year, Month and Day to a Date. * @param year is a number between 0..MAX_YEAR * @param month is a number between 0..11 * @param day is a number between 1..31 */ Date ConvertYMDToDate(Year year, Month month, Day day) { /* Day-offset in a leap year */ int days = _accum_days_for_month[month] + day - 1; /* Account for the missing of the 29th of February in non-leap years */ if (!IsLeapYear(year) && days >= ACCUM_MAR) days--; return DAYS_TILL(year) + days; }
int DayRank(int year, int month, int date) { int days = 0, i; int flag = IsLeapYear(year); if (flag) MONTH[1] = 29; else MONTH[1] = 28; for (i = 0; i < month - 1; ++i) days += MONTH[i]; days += date; return days; }
int main(int argc, char* argv[]){ int year = 0; if(argc == 2) year = atoi(argv[1]); if(IsLeapYear(year)) printf("%d is a leap year\n", year); else printf("%d is not a leap year\n", year); return 0; }
int Date::Get() const { if(IsNull(*this)) return Null; int y400 = (year / 400 ) - 2; int ym = year - y400 * 400; return y400 * (400 * 365 + 100 - 3) + ym * 365 + s_month_off[month - 1] + (day - 1) + (ym - 1) / 4 - (ym - 1) / 100 + (ym - 1) / 400 + 1 + (month > 2) * IsLeapYear(ym); }
int32 FDateTime::DaysInMonth( int32 Year, int32 Month ) { check((Month >= 1) && (Month <= 12)); if ((Month == EMonthOfYear::February) && IsLeapYear(Year)) { return 29; } return DaysPerMonth[Month]; }
static bool IsTheLastDayOfMonth(short year, short month, short day){ // int days=30; if(month<8&&month%2==1){ days=31; } if(month>=8&&month%2==0){ days=31; } if(month==2&&IsLeapYear(year)){ days=29; } if(month==2&&!IsLeapYear(year)){ days=28; } return day==days; }
/*************************************************************************************************** * @fn MT_SysSetUtcTime * * @brief Set the OSAL UTC Time. UTC rollover is: 06:28:16 02/07/2136 * * @param pBuf - pointer to time parameters * * @return None ***************************************************************************************************/ void MT_SysSetUtcTime(uint8 *pBuf) { uint8 cmdId; uint8 retStat; UTCTime utcSecs; /* Parse header */ cmdId = pBuf[MT_RPC_POS_CMD1]; pBuf += MT_RPC_FRAME_HDR_SZ; utcSecs = osal_build_uint32( pBuf, 4 ); if ( utcSecs == 0 ) { UTCTimeStruct utc; /* Skip past UTC time */ pBuf += 4; /* Get time and date parameters */ utc.hour = *pBuf++; utc.minutes = *pBuf++; utc.seconds = *pBuf++; utc.month = (*pBuf++) - 1; utc.day = (*pBuf++) - 1; utc.year = osal_build_uint16 ( pBuf ); if ((utc.hour < 24) && (utc.minutes < 60) && (utc.seconds < 60) && (utc.month < 12) && (utc.day < 31) && (utc.year > 1999) && (utc.year < 2136)) { /* Got past the course filter, now check for leap year */ if ((utc.month != 1) || (utc.day < (IsLeapYear( utc.year ) ? 29 : 28))) { /* Numbers look reasonable, convert to UTC */ utcSecs = osal_ConvertUTCSecs( &utc ); } } } if ( utcSecs == 0 ) { /* Bad parameter(s) */ retStat = ZInvalidParameter; } else { /* Parameters accepted, set the time */ osal_setClock( utcSecs ); retStat = ZSuccess; } /* Build and send back the response */ MT_BuildAndSendZToolResponse(((uint8)MT_RPC_CMD_SRSP | (uint8)MT_RPC_SYS_SYS), cmdId, 1, &retStat); }
/* * Determine the number of days in a specific month. */ static int DaysInMonth(ILInt64 year, ILInt64 month) { if(IsLeapYear(year) && month == 2) { return 29; } else { return daysForEachMonth[(int)(month - 1)]; } }
static VOID WINAPI RtlTimeToTimeFields( const LARGE_INTEGER *liTime, PTIME_FIELDS TimeFields) { const int *Months; int SecondsInDay, DeltaYear; int LeapYear, CurMonth; long int Days; LONGLONG Time = liTime->QuadPart; /* Extract millisecond from time and convert time into seconds */ TimeFields->Milliseconds = (CSHORT) ((Time % TICKSPERSEC) / TICKSPERMSEC); Time = Time / TICKSPERSEC; /* The native version of RtlTimeToTimeFields does not take leap seconds * into account */ /* Split the time into days and seconds within the day */ Days = Time / SECSPERDAY; SecondsInDay = Time % SECSPERDAY; /* compute time of day */ TimeFields->Hour = (CSHORT) (SecondsInDay / SECSPERHOUR); SecondsInDay = SecondsInDay % SECSPERHOUR; TimeFields->Minute = (CSHORT) (SecondsInDay / SECSPERMIN); TimeFields->Second = (CSHORT) (SecondsInDay % SECSPERMIN); /* compute day of week */ TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK); /* compute year */ /* FIXME: handle calendar modifications */ TimeFields->Year = EPOCHYEAR; DeltaYear = Days / DAYSPERQUADRICENTENNIUM; TimeFields->Year += DeltaYear * 400; Days -= DeltaYear * DAYSPERQUADRICENTENNIUM; DeltaYear = Days / DAYSPERNORMALCENTURY; TimeFields->Year += DeltaYear * 100; Days -= DeltaYear * DAYSPERNORMALCENTURY; DeltaYear = Days / DAYSPERNORMALQUADRENNIUM; TimeFields->Year += DeltaYear * 4; Days -= DeltaYear * DAYSPERNORMALQUADRENNIUM; DeltaYear = Days / DAYSPERNORMALYEAR; TimeFields->Year += DeltaYear; Days -= DeltaYear * DAYSPERNORMALYEAR; LeapYear = IsLeapYear(TimeFields->Year); /* Compute month of year */ Months = MonthLengths[LeapYear]; for (CurMonth = 0; Days >= (long) Months[CurMonth]; CurMonth++) Days = Days - (long) Months[CurMonth]; TimeFields->Month = (CSHORT) (CurMonth + 1); TimeFields->Day = (CSHORT) (Days + 1); }
/***** Listing 14-3. The julian() Method. (Added in Chapter 14) Purpose: Determine the numbers of days between the given date and Jan 1 of the same year. Algorithm taken from C Programmer's Toolkit, Jack Purdum, Que Corp., 1993, p.257. Parameters: int day The day to test int month The month to test int year The year to test (e.g., 2015) Return value: int The number of days, including the one given *****/ int Dates::julian(int day, int month, int year) { static int runsum[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}; int total; total = runsum[month - 1] + day; if (month > 2) { total += IsLeapYear(year); // Adjust for leap year } return total; }
/*---------------------------------------------------------------------------------------------- Fill in the SilTimeInfo structure from this time. ----------------------------------------------------------------------------------------------*/ void SilTime::GetTimeInfo(SilTimeInfo * psti, bool fUtc) const { AssertPtr(psti); int dyear; int day; int msecDay; int yday; int64 msec = m_msec; if (!fUtc) msec = g_tmm.ToLcl(msec); // Decompose m_msec into which 400 year period it is in (relative to T0) and // the millisecond within the 400 year period. psti->year = (int)FloorDiv(msec, kmsecPerPeriod) * kyearPerPeriod + kyearBase; msec = ModPos(msec, kmsecPerPeriod); Assert(0 <= 0 && msec < kmsecPerPeriod); // Find the day within the 400 year period. day = (int)(msec / kmsecPerDay); Assert(0 <= day && day < kdayPerPeriod); // Get the time within the day. msecDay = (int)(msec - day * (int64)kmsecPerDay); Assert(0 <= msecDay && msecDay < kmsecPerDay); // Find the year within the 400 year period (dyear) and the day within that year (yday). Assert(0 <= day && day < 146097); dyear = YearFromDayInPeriod(day, &yday); Assert(0 <= dyear && dyear < 400); Assert(0 <= yday && (yday < 365 || yday == 365 && SilTime::IsLeapYear(dyear + kyearBase))); // Add dyear into the year. psti->year += dyear; // Find the month and day in the month. const int * prgday = g_rgyday[IsLeapYear(psti->year)]; for (psti->ymon = 1; yday >= prgday[psti->ymon]; psti->ymon++) ; psti->mday = yday - prgday[psti->ymon - 1] + 1; // Calculate the week day. // kdayMonday is used because T0 is a Monday. Note that kdayPerPeriod is divisible by 7 // so we don't have to adjust the day of the week. psti->wday = ModPos(day + kwdayMonday, kdayPerWeek); // Fill in the time. psti->hour = msecDay / kmsecPerHour; psti->min = (msecDay % kmsecPerHour) / kmsecPerMin; psti->sec = (msecDay % kmsecPerMin) / kmsecPerSec; psti->msec = msecDay % kmsecPerSec; }
// Months void MyDate::SubtractMonths(int lessMonths) { if (lessMonths > GetMonth()){ lessMonths -= GetMonth(); if (IsLeapYear() && GetMonth() > 2) _days--; _days -= (numDaysToMonth[GetMonth()]); } for (int i = 0; i < lessMonths; i++) { _days -= DaysInThisMonth((GetMonth() - i) % 12); } }
int ASF_LegacyManager::DaysInMonth ( XMP_Int32 year, XMP_Int32 month ) { static short daysInMonth[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec int days = daysInMonth [ month ]; if ( (month == 2) && IsLeapYear ( year ) ) days += 1; return days; }
/** * Convert a year, month, and day-of-month, given in the proleptic * Gregorian calendar, to 1970 epoch days. * @param year Gregorian year, with 0 == 1 BCE, -1 == 2 BCE, etc. * @param month 0-based month, with 0==Jan * @param dom 1-based day of month * @return the day number, with day 0 == Jan 1 1970 */ Int64 Grego::FieldsToDay( /* [in] */ Int32 year, /* [in] */ Int32 month, /* [in] */ Int32 dom) { Int32 y = year - 1; Int64 julian = 365 * y + FloorDivide(y, 4) + (JULIAN_1_CE - 3) + // Julian cal FloorDivide(y, 400) - FloorDivide(y, 100) + 2 + // => Gregorian cal DAYS_BEFORE[month + (IsLeapYear(year) ? 12 : 0)] + dom; // => month/dom return julian - JULIAN_1970_CE; // JD => epoch day }