static void DoCalendarOneMonth(void) { int y, m, d, mm, yy; if (!DoSimpleCalendar) WriteCalHeader(); if (PsCal) { FromJulian(JulianToday, &y, &m, &d); printf("%s\n", PSBEGIN); printf("%s %d %d %d %d\n", MonthName[m], y, DaysInMonth(m, y), (JulianToday+1) % 7, MondayFirst); printf("%s %s %s %s %s %s %s\n", DayName[6], DayName[0], DayName[1], DayName[2], DayName[3], DayName[4], DayName[5]); mm = m-1; if (mm<0) { mm = 11; yy = y-1; } else yy=y; printf("%s %d\n", MonthName[mm], DaysInMonth(mm,yy)); mm = m+1; if (mm>11) { mm = 0; yy = y+1; } else yy=y; printf("%s %d\n", MonthName[mm], DaysInMonth(mm,yy)); } while (WriteCalendarRow()) continue; if (PsCal) printf("%s\n", PSEND); if (!DoSimpleCalendar) WriteCalTrailer(); }
CTDate CTDate::operator+(int d) { if (d < 0) { return *this - (0 - d); } CTDate temp(*this); temp.m_day += d; while (temp.m_day > DaysInMonth(temp.m_year, temp.m_month)) { temp.m_day -= DaysInMonth(temp.m_year, temp.m_month); if (temp.m_month == 12) { temp.m_month = 1; temp.m_year++; } else { temp.m_month++; } } return temp; }
Date & Date::AddDays(int count) { if (count < 0) { return SubDays(-count); } day_ += count; int dim = DaysInMonth(year_, month_); while (day_ > dim) { day_ -= dim; ++month_; if (month_ > MONTHES_IN_YEAR) { month_ = 1; ++year_; } dim = DaysInMonth(year_, month_); } return *this; }
void EMDate :: _rollDay() { int16 daysInMonth = DaysInMonth(); if (daysInMonth < 1) { fMonth = 1; daysInMonth = DaysInMonth(); } //printf("\t\t_rollDay() : %i / %i\n", fDay, daysInMonth); if (fDay < 1) { // going backwards! --fMonth; _rollMonth(); fDay += DaysInMonth(); } else if (fDay > daysInMonth) { ++fMonth; fDay = daysInMonth - fDay; daysInMonth = DaysInMonth(); if (fDay > daysInMonth) fDay = daysInMonth; if (fDay < 1) // result of fDay > daysInMonth fDay = -fDay; _rollMonth(); } if (!IsDayValid()) _rollDay(); // should never recurse more than once }
EMDate& EMDate :: operator -= (const EMDate& other) { if (!IsValid() || !other.IsValid()) return *this; // you can't subtract from an invalid date if (other.fDay > 0) { if (IsDayValid()) { fDay -= other.fDay; _rollDay(); _rollMonth(); } } if (other.IsMonthValid()) { if (IsMonthValid()) { int16 daysInPrevMonth = DaysInMonth(); fMonth -= other.fMonth; _rollMonth(); if (fDay == daysInPrevMonth) fDay = DaysInMonth(); if (fDay > DaysInMonth()) fDay = DaysInMonth(); } } if (other.IsYearValid()) { if (IsYearValid()) fYear -= other.fYear; } _enforceKeepDay(); return *this; }
const Date & Date::Set(int nMonth, int nDay, int nYear) { month = (unsigned char)nMonth; year = nYear < 0 ? 9999 : nYear; year = nYear > 9999 ? 0 : nYear; day = (unsigned char)(nDay < DaysInMonth() ? nDay : DaysInMonth()); mdy_to_julian(); return *this; }
/* ** Convert a time_t (UTC) to a tm structure in UTC time. ** Lua only calls this once (from os_date). */ RTEXP struct tm *gmtime( const time_t *timer ) { static struct tm stm; SYSTEMTIME syst; int secs; int tdays; secs = *timer; /* Calculate day of week. 1970-01-01 was a Thursday. (day 4 in SYSTEMTIME format)*/ tdays = secs / SECONDS_IN_DAY; syst.wDayOfWeek = 4 + (tdays % 7); if(syst.wDayOfWeek > 6) syst.wDayOfWeek -= 7; syst.wYear = 1970; while(secs >= SECONDS_IN_YEAR) { if(IsLeap(syst.wYear) && secs >= SECONDS_IN_LEAP_YEAR) { secs -= SECONDS_IN_LEAP_YEAR; syst.wYear++; } else { secs -= SECONDS_IN_YEAR; syst.wYear++; } } syst.wMonth = 1; while(secs) { if(secs >= DaysInMonth(syst.wMonth, syst.wYear) * SECONDS_IN_DAY) secs -= (DaysInMonth(syst.wMonth++, syst.wYear) * SECONDS_IN_DAY); else break; } syst.wDay = 1; while(secs) { if(secs >= SECONDS_IN_DAY) { secs -= SECONDS_IN_DAY; syst.wDay++; } else break; } syst.wHour = 0; while(secs) { if(secs >= 3600) { secs -= 3600; syst.wHour++; } else break; } syst.wMinute = 0; while(secs) { if(secs >= 60) { secs -= 60; syst.wMinute++; } else break; } syst.wSecond = secs; syst.wMilliseconds = 0; return tm_struct_from_systime( &syst ); }
FDateTime::FDateTime( int32 Year, int32 Month, int32 Day, int32 Hour, int32 Minute, int32 Second, int32 Millisecond ) { check((Year >= 1) && (Year <= 9999)); check((Month >= 1) && (Month <= 12)); check((Day >= 1) && (Day <= DaysInMonth(Year, Month))); check((Hour >= 0) && (Hour <= 23)); check((Minute >= 0) && (Minute <= 59)); check((Second >= 0) && (Second <= 59)); check((Millisecond >= 0) && (Millisecond <= 999)); int32 TotalDays = 0; if ((Month > 2) && IsLeapYear(Year)) { ++TotalDays; } Year--; // the current year is not a full year yet Month--; // the current month is not a full month yet TotalDays += Year * 365; TotalDays += Year / 4; // leap year day every four years... TotalDays -= Year / 100; // ...except every 100 years... TotalDays += Year / 400; // ...but also every 400 years TotalDays += DaysToMonth[Month]; // days in this year up to last month TotalDays += Day - 1; // days in this month minus today Ticks = TotalDays * ETimespan::TicksPerDay + Hour * ETimespan::TicksPerHour + Minute * ETimespan::TicksPerMinute + Second * ETimespan::TicksPerSecond + Millisecond * ETimespan::TicksPerMillisecond; }
static void MovingLightModel_Tasks() { if (g_moving_light.lamp_state == LAMP_STRIKE && CoarseTimer_HasElapsed(g_moving_light.lamp_strike_time, LAMP_STRIKE_DELAY)) { g_moving_light.lamp_state = LAMP_ON; g_moving_light.lamp_strikes++; } if (CoarseTimer_HasElapsed(g_moving_light.clock_timer, ONE_SECOND)) { g_moving_light.clock_timer = CoarseTimer_GetTime(); g_moving_light.second++; if (g_moving_light.second >= 60u) { g_moving_light.second = 0u; g_moving_light.minute++; } if (g_moving_light.minute >= 60u) { g_moving_light.minute = 0u; g_moving_light.hour++; } if (g_moving_light.hour >= 24u) { g_moving_light.hour = 0u; g_moving_light.day++; } if (g_moving_light.day > DaysInMonth(g_moving_light.year, g_moving_light.month)) { g_moving_light.day = 1u; g_moving_light.month++; } if (g_moving_light.month > 12u) { g_moving_light.month = 1u; g_moving_light.year++; } } }
int MovingLightModel_SetClock(const RDMHeader *header, const uint8_t *param_data) { if (header->param_data_length != sizeof(uint16_t) + 5 * sizeof(uint8_t)) { return RDMResponder_BuildNack(header, NR_FORMAT_ERROR); } const uint16_t year = ExtractUInt16(param_data); const uint8_t month = param_data[2]; if (year < 2003u || month == 0u || month > 12u) { return RDMResponder_BuildNack(header, NR_DATA_OUT_OF_RANGE); } const uint8_t day = param_data[3]; const uint8_t hour = param_data[4]; const uint8_t minute = param_data[5]; const uint8_t second = param_data[6]; // We don't support leap seconds for now. if (day == 0u || day > DaysInMonth(year, month) || hour > 24u || minute > 59u || second > 59u) { return RDMResponder_BuildNack(header, NR_DATA_OUT_OF_RANGE); } g_moving_light.year = year; g_moving_light.month = month; g_moving_light.day = day; g_moving_light.hour = hour; g_moving_light.minute = minute; g_moving_light.second = second; return RDMResponder_BuildSetAck(header); }
void CCalendarHelper::GetCalendar(int year, int month, DayTraffic calendar[CALENDAR_HEIGHT][CALENDAR_WIDTH]) { memset(calendar, 0, sizeof(DayTraffic)*CALENDAR_HEIGHT*CALENDAR_WIDTH); int days{ DaysInMonth(year, month) }; int first_weak_day{ CaculateWeekDay(year, month, 1) }; int i{}, j{}; for (int n{}; n < 37; n++) { if (n < first_weak_day) { calendar[i][j].day = 0; } else { int day = n - first_weak_day + 1; if (day <= days) calendar[i][j].day = day; } j++; if (j >= 7) { j = 0; i++; } } }
void ASF_LegacyManager::ConvertISODateToMSDate ( std::string& source, std::string* dest ) { XMP_DateTime date; SXMPUtils::ConvertToDate ( source, &date ); SXMPUtils::ConvertToUTCTime ( &date ); XMP_Int64 creationDate; creationDate = date.nanoSecond / 100; creationDate += (XMP_Int64 ( date.second) * (10*1000*1000) ); creationDate += (XMP_Int64 ( date.minute) * 60 * (10*1000*1000) ); creationDate += (XMP_Int64 ( date.hour) * 3600 * (10*1000*1000) ); XMP_Int32 days = (date.day - 1); --date.month; while ( date.month >= 1 ) { days += DaysInMonth ( date.year, date.month ); --date.month; } --date.year; while ( date.year >= 1601 ) { days += (IsLeapYear ( date.year) ? 366 : 365 ); --date.year; } creationDate += (XMP_Int64 ( days) * 86400 * (10*1000*1000) ); creationDate = GetUns64LE ( &creationDate ); dest->assign ( (const char*)&creationDate, 8 ); }
/** * name: IsValid * class: MAnnivDate * desc: compare the current date with the given one in st * param: st - SYSTEMTIME to compare with * return: number of days the st differs from the class value **/ __inline BYTE MAnnivDate::IsValid() const { return ( Month() > 0 && Month() < 13 && Day() > 0 && Day() <= DaysInMonth(Month()) ); }
FILETIME FromGlkDate(const glkdate_t* date) { glsi32 carry = 0; glsi32 micros = NormalizeField(date->microsec,1000000,carry); SYSTEMTIME st; ::ZeroMemory(&st,sizeof st); st.wSecond = (WORD)NormalizeField(date->second + carry,60,carry); st.wMinute = (WORD)NormalizeField(date->minute + carry,60,carry); st.wHour = (WORD)NormalizeField(date->hour + carry,24,carry); glsi32 days = date->day + carry; st.wMonth = 1 + (WORD)NormalizeField(date->month - 1,12,carry); st.wYear = (WORD)(date->year + carry); while (days <= 0) days += LastMonth(st.wMonth,st.wYear); while (days > DaysInMonth(st.wMonth,st.wYear)) days -= NextMonth(st.wMonth,st.wYear); st.wDay = (WORD)days; FILETIME ft; if (::SystemTimeToFileTime(&st,&ft)) { ULONGLONG ticks = FileTimeToValue(ft); ticks += (micros * 10); return ValueToFileTime(ticks); } return BadTime; }
CTDate CTDate::operator-(int d) { if (d < 0) { return *this + (0 - d); } CTDate temp(*this); temp.m_day -= d; while (temp.m_day <= 0) { if (temp.m_month == 1) { temp.m_month = 12; temp.m_year--; } else { temp.m_month--; } temp.m_day += DaysInMonth(temp.m_year, temp.m_month); } return temp; }
/** * name: IsValid * class: MAnnivDate * desc: compare the current date with the given one in st * param: st - SYSTEMTIME to compare with * return: number of days the st differs from the class value **/ __inline BOOLEAN MAnnivDate::IsValid() const { return ( Year() > 1600 && Month() > 0 && Month() < 13 && Day() > 0 && Day() <= DaysInMonth(Month()) ); }
bool EMDate :: IsDayValid() const { int maxDays = DaysInMonth(); if (maxDays == 0) maxDays = 31; return fDay > 0 && fDay < maxDays + 1; }
//! Calculate day prior to specified //! \param[in,out] year Year of month //! \param[in,out] month Month of day //! \param[in,out] day Day of interest void CalculatePriorDay(scxyear &year, scxmonth &month, scxday &day) { if (day <= 1) { CalculatePriorMonth(year, month); day += DaysInMonth(year, month) - 1; } else { --day; } }
EMDate& EMDate :: operator += (const EMDate& other) { if (!other.IsValid()) // we don't care if we are valid! return *this; if (other.fDay > 0) { if (IsDayValid() == false) { fDay = other.fDay; _rollDay(); } else { fDay += other.fDay; _rollDay(); _rollMonth(); } } if (other.IsMonthValid()) { if (IsMonthValid() == false) { fMonth = other.fMonth; _rollMonth(); } else { int16 daysInPrevMonth = DaysInMonth(); fMonth += other.fMonth; _rollMonth(); if (fDay == daysInPrevMonth) fDay = DaysInMonth(); if (fDay > DaysInMonth()) fDay = DaysInMonth(); } } if (other.IsYearValid()) { if (IsYearValid() == false) fYear = other.fYear; else fYear += other.fYear; } _enforceKeepDay(); return *this; }
//! Calculate day after specified //! \param[in,out] year Year of month //! \param[in,out] month Month of day //! \param[in,out] day Day of interest void CalculateNextDay(scxyear &year, scxmonth &month, scxday &day) { unsigned int daysInMonth = DaysInMonth(year, month); if (day >= daysInMonth) { day -= daysInMonth - 1; CalculateNextMonth(year, month); } else { ++day; } }
WORD MTime::DayOfYear() const { WORD daysResult = 0; WORD i; for (i = 0; i < _SysTime.wMonth; i++) daysResult += DaysInMonth(i); daysResult += _SysTime.wDay; return daysResult; }
BYTE MTime::IsValid() const { return ( _SysTime.wYear > 1600 && _SysTime.wMonth > 0 && _SysTime.wMonth < 13 && _SysTime.wDay > 0 && _SysTime.wDay <= DaysInMonth(_SysTime.wMonth) && _SysTime.wHour < 25 && _SysTime.wMinute < 60 && _SysTime.wSecond < 60 ); }
unsigned RedDate::DaysFromStartOfYear(void) const { unsigned retdays = 0; for (unsigned m=1; m<month; m++) retdays += DaysInMonth(m, year); retdays += date; return retdays; }
Date & Date::SubYears(int count) { if (count < 0) { return AddYears(-count); } year_ -= count; day_ = Min(day_, DaysInMonth(year_, month_)); return *this; }
int32 FDateTime::GetDayOfYear( ) const { int32 Year, Month, Day; GetDate(Year, Month, Day); for (int32 CurrentMonth = 1; CurrentMonth < Month; ++CurrentMonth) { Day += DaysInMonth(Year, CurrentMonth); } return Day; }
bool EMDate :: _enforceKeepDay() { if (fKeepDay < 1 || fKeepDay > 31) return false; if (fDay != fKeepDay) { fDay = fKeepDay; int16 daysInMonth = DaysInMonth(); if (fDay > daysInMonth) fDay = daysInMonth; return true; } return false; }
Date & Date::SubMonthes(int count) { if (count < 0) { return AddMonthes(-count); } month_ -= count; if (month_ < 1) { year_ -= (MONTHES_IN_YEAR - month_) / MONTHES_IN_YEAR; month_ = MONTHES_IN_YEAR - (- month_) % MONTHES_IN_YEAR; } day_ = Min(day_, DaysInMonth(year_, month_)); return *this; }
Date & Date::AddMonthes(int count) { if (count < 0) { return SubMonthes(-count); } month_ += count; if (month_ > MONTHES_IN_YEAR) { year_ += (month_ - 1) / MONTHES_IN_YEAR; month_ = (month_ - 1) % MONTHES_IN_YEAR + 1; } day_ = Min(day_, DaysInMonth(year_, month_)); return *this; }
Date::Date (const char *dat) { char *tmp, *tmp2; int nlen=strlen(dat); tmp = new char[nlen+1]; // TML - We need to make a copy of 'dat' because strtok() modifies // the content of its first parameter!!! // strcpy(tmp, dat); // // Possible date string formats are those that are generated by the // Date class itself! The corresponding possible string lengths are // also listed (with ranges from shortest to longest string for that // format). // // MDY: 03/23/1993 => 6-10,13-17 // EUROPEAN: 23 March 1993 => 13, 20 // FULL,ABBR.: Tue, Mar 23, 1993 => 16-17, 23-24 // FULL: Tuesday, March 23, 1993 => 22-23, 29-30 // // These dates may also have B.C.E. appended at the end, thus we have // the second set of string lengths with 7 more characters! // month = (unsigned char)atoi(strtok(tmp,"/")); day = (unsigned char)atoi(strtok(NULL,"/")); year = atoi(strtok(NULL," ")); // // Verify values! // if ((month < 1) || (month > 12) || (day < 1) || (day > (unsigned char) DaysInMonth())) { month = day = 0; year = 0; } delete [] tmp; mdy_to_julian (); }
/* ** Convert from suppled SYSTEMTIME structure to a tm */ RTEXP struct tm *tm_struct_from_systime( SYSTEMTIME *systime ) { static struct tm gtm; int m; gtm.tm_sec = systime->wSecond; gtm.tm_min = systime->wMinute; gtm.tm_hour = systime->wHour; gtm.tm_mday = systime->wDay; gtm.tm_mon = systime->wMonth -1; gtm.tm_year = systime->wYear - 1900; gtm.tm_wday = systime->wDayOfWeek; gtm.tm_yday = 0; m = 1; while(m < systime->wMonth) gtm.tm_yday += DaysInMonth(m++, systime->wYear); gtm.tm_yday += (systime->wDay -1); gtm.tm_isdst = IsDaylightSaving(>m); return >m; }