void Date::settm( int aDay, int aMonth, int aYear, int aSecond ) throw( DateException ) { if( aMonth > 11 ) throw DateException( "Out of range for month" ); if( aDay > getDaysInMonth( aMonth, aYear ) ) throw DateException( "Out of range for day" ); if( aSecond > 86400 ) throw DateException( "Out of range for seconds" ); Time.tm_sec = aSecond % 60; Time.tm_min = aSecond / 60; Time.tm_hour = aSecond / 3600; Time.tm_mday = aDay; Time.tm_mon = aMonth; Time.tm_year = aYear - 1900; int centuryNumber; /* Formula for calculating the day of the week */ /* d + m + y + (last 2 digits of year) / 4 + c */ /* d = day, m = month, y = year, c = century number */ /* the century number is 6 if the two significant */ /* digits of the year are divisible by 4, then the */ /* subsequent centuries are 4, 2, 0. */ /* ( 2000 => 6, 2100 => 4, 2200 => 2, 2300 => 0 ) */ /* check out wiki: "Determination of the day of the week" */ centuryNumber = 6 - ( ( aYear / 100 ) % 4 ); Time.tm_wday = ( aDay + aMonth + aYear + ( ( aYear % 100 ) / 4 ) + centuryNumber ) % 7; /* To get the day in the year, add the number of */ /* days in each month before the current month */ /* then add the day of the current month to that */ /* total. */ int yday = 0; for( int i = 0; i < aMonth; ++i ) { yday += getDaysInMonth( i, getYear() ); } yday += aDay; Time.tm_yday = yday; /* I don't want to worry about day light savings */ Time.tm_isdst = -1; } /* end Date::init() */
int main(int argc, uint8_t *argv[]) { uint64_t year, dayOfMonth, daysInMonth, sundays; month_t monthOfYear; /** * 1900-01-01 is a Monday. */ week_day_t weekDay = MON; dayOfMonth = 1; sundays = 0; for (year = 1900; year <= 2000; year++) { for (monthOfYear = JAN; monthOfYear < MONTHS_IN_YEAR; monthOfYear++) { daysInMonth = getDaysInMonth(year, monthOfYear); for (dayOfMonth = 1; dayOfMonth <= daysInMonth; dayOfMonth++) { weekDay = (weekDay + 1) % DAYS_IN_WEEK; if (year > 1900 && dayOfMonth == 1 && weekDay == SUN) { sundays += 1; } } } } printf("Sundays: %" PRIu64 "\n", sundays); return 0; }
int main (int argc, char *argv[]) { int leapYear; int doomsday; int month; int daysInMonth; int day; int dayOfWeek; //get leap and non-leap years for (leapYear = 0; leapYear < 2; leapYear++) { // get doomsdays for each day of the week for (doomsday = 0; doomsday < 7; doomsday++) { // for every month for (month = 1; month <= 12; month++) { daysInMonth = getDaysInMonth (leapYear, month); // for each day of the month print an assert for (day = 1; day <= daysInMonth; day++) { dayOfWeek = getDayOfWeek (leapYear, doomsday, month, day, dayOfWeek); printAssert(leapYear, doomsday, month, day, dayOfWeek); } } } } return EXIT_SUCCESS; }
unsigned int getDayCountUpToMonthWithYear(unsigned int month, unsigned int year) { const bool ly = isLeapYear(year); int days = 0; while (--month > 0) days += getDaysInMonth(month, ly); return days; }
void Date::adjustDay( int aAmount ) throw( DateException ) { int addDays = aAmount + getDay(); int daysInMonth = getDaysInMonth( getMonth(), getYear() ); while( addDays >= daysInMonth ) { adjustMonth( 1 ); addDays = addDays - daysInMonth; /* month should have been moved up 1 */ daysInMonth = getDaysInMonth( getMonth(), getYear() ); } setDay( addDays ); } /* end Date::adjustDay() */
time_t getEndOfMonth(const time_t& rawtime) { struct tm * timeInfo = localtime(&rawtime); timeInfo->tm_sec = 59; timeInfo->tm_min = 59; timeInfo->tm_hour = 23; timeInfo->tm_mday = getDaysInMonth(rawtime); timeInfo->tm_isdst = -1; return mktime(timeInfo); }
int calculateDayOfYear2() { setDate2(); int i = 0; int endMonth = date2[0]; int endDay = date2[1]; int days; for (i = 0; i < endMonth; i++) { days = getDaysInMonth(i) + days; } int totalDays = days + endDay; return totalDays; }
int main() { // 0 - Monday, 1 - Tuesday, ... , 6 - Sunday int dayOfWeek = 365 % 7; int sum = 0; for (int year = 1901; year <= 2000; year++) { for (int month = 0; month < 12; month++) { // Increase sum if the first day of the month is Sunday. if (dayOfWeek == 6) sum++; // Goto the next month. dayOfWeek = (dayOfWeek + getDaysInMonth(month, year)) % 7; } } std::cout << sum << '\n'; }
//! Function to create the calendar date from the year and the number of days in the year boost::gregorian::date convertYearAndDaysInYearToDate( const int year, const int daysInYear ) { // Go to day 1 at 01-01 convention int daysLeft = daysInYear + 1; // Loop over all months (starting at January until month in which daysinYear is situated is found) int currentMonth = 1; int daysInCurrentMonth; bool isConverged = 0; while( !isConverged ) { // Determine days in current month daysInCurrentMonth = getDaysInMonth( currentMonth, year ); if( daysInCurrentMonth < daysLeft ) { // Subtract days in current month from days left. daysLeft -= daysInCurrentMonth; // Update month and check consistency currentMonth++; if( currentMonth > 12 ) { std::cerr<<"Error when converting year and days in year to date, month number has exceeded 12"<<std::endl; } } else { isConverged = 1; } } // Create date and return boost::gregorian::date date( year, currentMonth, daysLeft ); if( date.day_of_year( ) != daysInYear + 1 ) { std::cerr<<"Error when converting year and days in year to date, inconsistent output"<<std::endl; } return date; }
int calculateTotalDaysBetween() { int counter =0; setDate1(); setDate2(); int y = date1[2]; int d = date1[1]; int m = date1[0]; int flag = 0; int dm; do { for (m = date1[0]; m <= 11; m++) {//while the current month is less than or equal to twelve dm = getDaysInMonth(m); // get the amount of days in this current month for (d = date1[1]; d <= dm; d++) { // while the current day is less than days in month date1[1] = d; date1[0] = m; date1[2] = y; counter++; if ((date2[0] == m)&&(date2[1] == d)&&(date2[2] == y)) { // if the amount of days we are calculating to is reached, flag = 1; //set the flag value to 1 and break break; } } if (flag == 1) { // upon exiting inner loop check if the exit is due to amount of days reached flag = 2; // if so set flag value to 2 break; } date1[1] = 1; d = 1; } if (flag == 2) { // upon exiting outer loop check if the exit is due to amount of days reached break; // if so, break } y++; date1[0] = 0; m = 0; //set month back to 0, increment year } while (!(date1[0] == date2[0])&&!(date1[1] == date1[2])&&!(date1[2] == date2[2])); return counter; }
void calculateCalendarDate() { setDate1(); int daysFrom = getNumber(); int y = date1[2]; int d = date1[1]; int m = date1[0]; //gets start month int flag = 0; int counter = 0; int dm; while (counter <= daysFrom) { for (m = date1[0]; m <= 11; m++) {//while the current month is less than or equal to twelve dm = getDaysInMonth(m); // get the amount of days in this current month for (d = date1[1]; d <= dm; d++) { // while the current day is less than days in month date1[1] = d; date1[0] = m; date1[2] = y; counter++; if (counter > daysFrom) { // if the amount of days we are calculating to is reached, flag = 1; //set the flag value to 1 and break break; } } if (flag == 1) { // upon exiting inner loop check if the exit is due to amount of days reached flag = 2; // if so set flag value to 2 break; } date1[1] = 1; d = 1; } if (flag == 2) { // upon exiting outer loop check if the exit is due to amount of days reached break; // if so, break } y++; date1[0] = 0; m = 0; //set month back to 0, increment year } }
void Months::SetYM(int year, int month){ itsYear = year; itsMonth = month; itsMaxDay = getDaysInMonth(year, month); }
int main(int argc, const char* argv[]) { if(argc < 2) { printError(); return 0; } int iMonth = 0; int iYear = 0; sscanf(argv[1], "%d.%d", &iMonth, &iYear); if(iYear <= 1900 || iMonth > 12 || iMonth <= 0) { printError(); return 0; } // Set environment's default locale setlocale(LC_ALL, ""); // Init console colors HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo( hstdout, &csbi ); int iFirstWeekDay = getFirstDayOfWeek(); int iMonthStartWeekDay = getMonthStartDay(iYear, iMonth); int iDaysInMonth = getDaysInMonth(iYear, iMonth); wchar_t Buf[255]; int iRow = 0; bool bFillCalendar = false; int iCurrentWeekDay = iFirstWeekDay; int iCurrentMonthDay = 1; while(iCurrentMonthDay <= iDaysInMonth) { iCurrentWeekDay = iFirstWeekDay; for(int i = 0; i < 7; i++) { if(iRow == 0) { // Setting white color on black background SetConsoleTextAttribute(hstdout, 0xF0); GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + iCurrentWeekDay, Buf, 255); wprintf(L"%4.3ls ", Buf); } else if(iRow == 1) { // Setting white color on black background SetConsoleTextAttribute(hstdout, 0x7F); if(iMonthStartWeekDay == iCurrentWeekDay) { bFillCalendar = true; } } // Filling blank space before and after selected month if(!bFillCalendar && iRow != 0) { wprintf(L"%5s", " "); } if(bFillCalendar) { // Setting days color if(iCurrentWeekDay == 5 || iCurrentWeekDay == 6) { // Sundays and saturdays are light red! SetConsoleTextAttribute(hstdout, 0x7C); } else { // Other days are white SetConsoleTextAttribute(hstdout, 0x71); } if(isToday(iYear, iMonth, iCurrentMonthDay)) { // Printing "today" in the brackets swprintf(Buf, L"[%2d]", iCurrentMonthDay); wprintf(L"%5.4s", Buf); } else { wprintf(L"%4d ", iCurrentMonthDay); } iCurrentMonthDay++; if(iCurrentMonthDay > iDaysInMonth) { bFillCalendar = false; } } if(iCurrentWeekDay == 6) { iCurrentWeekDay = 0; } else { iCurrentWeekDay++; } } SetConsoleTextAttribute(hstdout, csbi.wAttributes); wprintf(L"\r\n"); iRow++; } printf("Press any key to continue..."); getch(); return 0; }