int num_days_in_month(int year, int month) { if(month < 1 || month > 12) return 0; // invalid month if(month == 2) return (is_leap_year(year) ? 29 : 28); if(month == 4 || month == 6 || month == 9 || month == 11) return 30; return 31; }
unsigned int Date::get_days_in_month() const { switch( get_month() ) { case 4: case 6: case 9: case 11: return 30; case 2: return is_leap_year() ? 29 : 28; } return 31; }
uint16_t DS1307new::_corrected_year_day_number(void) { uint8_t a; uint16_t corrected_ydn = ydn; a = is_leap_year(year); if ( corrected_ydn > (uint8_t)(((uint8_t)59)+a) ) { corrected_ydn += 2; corrected_ydn -= a; } corrected_ydn += 91; return corrected_ydn; }
// Calculate and return the Julian day associated with the given // month and day of the year. int julian_day( int month, int day, int year ) { int jday = 0; for ( unsigned int m=1; m<month; ++m ) { jday += DaysInMonth[m]; if ( m == 2 && is_leap_year(year)) { ++jday; // February 29th } } jday += day; return jday; }
bool KCalendarSystemHebrew::setYMD(TQDate & date, int y, int m, int d) const { if( y < minValidYear() || y > maxValidYear() ) return false; if( m < 1 || m > (is_leap_year(y) ? 13 : 12) ) return false; if( d < 1 || d > hndays(m,y) ) return false; class h_date * gd = hebrewToGregorian( y, m, d ); return date.setYMD(gd->hd_year, gd->hd_mon + 1, gd->hd_day + 1); }
static time_t timegm(struct tm *tm) { static const unsigned month_start[2][12] = { { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }, { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 }, }; time_t ret = 0; int i; for (i = 70; i < tm->tm_year; ++i) ret += is_leap_year(i) ? 366 : 365; ret += month_start[is_leap_year(tm->tm_year)][tm->tm_mon]; ret += tm->tm_mday - 1; ret *= 24; ret += tm->tm_hour; ret *= 60; ret += tm->tm_min; ret *= 60; ret += tm->tm_sec; return ret; }
/* Prototype: uint16_t to_century_day_number(uint16_t y, uint16_t ydn) Description: Calculate days since January, 1st, 2000 Arguments: y year, e.g. 2011 for year 2011 ydn year day number (1st of Jan has the number 1) */ uint16_t to_century_day_number(uint16_t y, uint16_t ydn) { uint16_t cdn; cdn = ydn; cdn--; while( y > 2000 ) { y--; cdn += 365; cdn += is_leap_year(y); } return cdn; }
void write_cal_page(FILE* fi, int year, int month, double width, double height) { struct tm time_input; char heading_text[100] = {'\0'}; char day_text[100] = {'\0'}; char buffer[100] = {'\0'}; double x_pos, y_pos, y_pos2; int i; month_days[1] = is_leap_year(year) ? 29 : 28; time_input.tm_sec = 0; time_input.tm_min = 0; time_input.tm_hour = 12; time_input.tm_mday = 1; time_input.tm_mon = month-1; time_input.tm_year = year-1900; mktime(&time_input); /* correct other values */ /* init document */ init_svg_document(fi, width, height); /* draw first line (month - year) */ strcpy(heading_text, month_name[month-1]); strcat(heading_text, " - "); sprintf(buffer, "%d", year); strcat(heading_text, buffer); draw_text(fi, width/2, 0.75*HEAD_PART*height, heading_text, 0.5*HEAD_PART*height, "middle", "black"); /* draw day lines */ y_pos2 = HEAD_PART*height; for(i = 0; i < month_days[month-1]; ++i) { y_pos = y_pos2; y_pos2 = HEAD_PART*height + (i+1) * ((1-HEAD_PART)*height) / month_days[month-1]; x_pos = BORDER_PART*width; sprintf(buffer, "%02d", i+1); strcpy(day_text, buffer); strcat(day_text, ", "); strcat(day_text, weekday[(time_input.tm_wday+i) % 7]); if((time_input.tm_wday+i) % 7 != 0) { draw_text(fi, x_pos, y_pos+0.85*((1-HEAD_PART)*height) / month_days[month-1], day_text, 0.8*((1-HEAD_PART)*height) / month_days[month-1], "left", "black"); draw_line(fi, 0, y_pos2, width, y_pos2, 1, "black"); } else { draw_text(fi, x_pos, y_pos+0.85*((1-HEAD_PART)*height) / month_days[month-1], day_text, 0.8*((1-HEAD_PART)*height) / month_days[month-1], "left", "red"); draw_line(fi, 0, y_pos2, width, y_pos2, 3, "black"); } } /* finish document */ finish_svg_document(fi); month_days[1] = 28; }
/** * Returns the number of minutes passed from the start of year b to the start * of year a. */ int minutes_between_years(const struct datetime *a, const struct datetime *b) { int days = 0; int i; for(i = b->year; i < a->year; i++) { if (is_leap_year(i) == 1) { days += 366; } else { days += 365; } } return days * 24 * 60; }
int cmos::Time::day_of_year() { static std::array<uint8_t, 13> days_in_normal_month = {{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }}; int res = f.day_of_month; auto month = f.month; while (month --> 0) res += days_in_normal_month[ month ]; if (is_leap_year(year()) && f.month > 2) res += 1; return res; }
/** * @brief get rtc counter, thr return value can be the param to the RTC_SetCounter(seccount) directly * @param year: * @param month: * @param day: day * @param hour: * @param min: * @param sec: * @retval second counter */ uint32 get_rtc_counter(uint16 year,uint8 month,uint8 day,uint8 hour,uint8 min,uint8 sec) { uint16 t; uint32 seccount; if(year<1970||year>2099)return 1; for(t=1970;t<year;t++) //add all the second of the year before from 1970 { if(is_leap_year(t))seccount+=31622400;//the amount of second of leap year else seccount+=31536000; //common year } month-=1; for(t=0;t<month;t++) //add all the second of the month before { seccount+=(uint32)day_month_table[t]*86400; if(is_leap_year(year)&&t==1)seccount+=86400; } seccount+=(uint32)(day-1)*86400; seccount+=(uint32)hour*3600; seccount+=(uint32)min*60; seccount+=(uint32)sec; return seccount; }
int valid_date(int day, int month, int year) { unsigned short monthlen[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (month < 1 || day < 1 || month > 12){ return 0; } if (is_leap_year(year) && month == 2) { monthlen[1]++; } if (day>monthlen[month-1] || day < 1) { return 0; } return 1; }
// iterate days in a year, keeping track of sundays on 1st day of month // start [in,out]: week day of 1st jan void process_one_year(int year, int &start, int &sundays) { bool leap = is_leap_year(year); const int *months = leap ? days_in_months_leap : days_in_months; for (int month = 0; month < MONTHS; month++) { for (int day = 0; day < months[month]; day++, incr_day_of_week(start)) { if (0 == day && 0 == start) sundays++; } } }
void seconds_to_rtc(uint64_t seconds, fuchsia_hardware_rtc_Time* rtc) { // subtract local epoch offset to get to rtc time uint64_t epoch = seconds - local_epoch; rtc->seconds = epoch % 60; epoch /= 60; rtc->minutes = epoch % 60; epoch /= 60; rtc->hours = epoch % 24; epoch /= 24; for (rtc->year = local_epoch_year;; rtc->year++) { uint32_t days_per_year = 365; if (is_leap_year(rtc->year)) { days_per_year++; } if (epoch < days_per_year) { break; } epoch -= days_per_year; } for (rtc->month = 1;; rtc->month++) { uint32_t days_per_month = days_in_month[rtc->month]; if ((rtc->month == 2) && is_leap_year(rtc->year)) { days_per_month++; } if (epoch < days_per_month) { break; } epoch -= days_per_month; } // remaining epoch is a whole number of days so just make it one-indexed rtc->day = epoch + 1; }
int Julian::days_in_month(int y,int m) const{ if(m<0 || m>12){ throw std::out_of_range("month_out_of_range"); } switch (m){ case 1: return 31; break; case 2: if(is_leap_year(y)){ return 29; }else{ return 28; } break; case 3: return 31; break; case 4: return 30; break; case 5: return 31; break; case 6: return 30; break; case 7: return 31; break; case 8: return 31; break; case 9: return 30; break; case 10: return 31; break; case 11: return 30; break; case 12: return 31; break; } return -1; }
time_t mktime(struct tm *pt) { time_t res = 0; int unix_year; time_t year_secs; pt->tm_wday = 0; pt->tm_yday = 0; if (tm_out_of_range(pt)) { errno = EOVERFLOW; return (time_t)(-1); } unix_year = (1900 + pt->tm_year) - 1970; if (unix_year != 0) { if (unix_year < 0) { res += get_year_seconds(abs(unix_year) - 1, 1971 + unix_year); } else { res += get_year_seconds(unix_year, 1970); } } pt->tm_yday = get_yday(pt); if (unix_year < 0) { year_secs = pt->tm_yday * 86400; year_secs += pt->tm_hour * 3600; year_secs += pt->tm_min * 60; year_secs += pt->tm_sec; if (is_leap_year(1970 + unix_year)) { res += (31622400 - year_secs); } else { res += (31536000 - year_secs); } res = -res; } else { res += pt->tm_yday * 86400; res += pt->tm_hour * 3600; res += pt->tm_min * 60; res += pt->tm_sec; } pt->tm_wday = get_week_day(res / 86400); _tz_add_offset(&res); return res; }
int days_in_month(int month, int year) { if (month == 4 || month == 6 || month == 9|| month == 11) { return 30; } else if (month == 2) { if (is_leap_year(year)) return 29; else return 28; } else { return 31; } }
uint16_t calculate_days_into_year(const TM * tm) { uint16_t days = s_days_into_year[tm->tm_mon] + tm->tm_mday; if (is_leap_year(tm->tm_year + 1900)) { if (days > FEB28TH_DAYS_INTO_YEAR) { days++; //There's an extra day (Feb 29th) not accounted for months days array } } days--; // Subtract 1 because tm->tm_mday is 1-31, and first day of year is 0th day return days; }
static int days_in_month(int year, int month) { switch(month) { case september: case april: case june: case november: return 30; default: return 31; case february: return is_leap_year(year) ? 29 : 28; } }
// ******************************************************************* // FUNCTION: number_days // // DESCRIPTION: This function will take a month and year and return // the number of days in that month. // // PARAMETERS: month - the month // year - the year // // OUTPUTS: int - number of days in the month // // CALLS: is_leap_year // ******************************************************************* int number_days (int month, int year){ bool is_leap_year (int year); // an array with the number of days in each month in a regular year const int days_per_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (is_leap_year(year) && month == 2) { // if it's a leapyear and the user is requesting the days in feb return 29; } else { // if it's not a leap year return days_per_month[month - 1]; } }
TEST test_is_leap_year() { ASSERT_EQ(is_leap_year(2012), true); ASSERT_EQ(is_leap_year(2013), false); ASSERT_EQ(is_leap_year(2014), false); ASSERT_EQ(is_leap_year(2015), false); ASSERT_EQ(is_leap_year(2016), true); ASSERT_EQ(is_leap_year(2017), false); PASS(); }
void unix_seconds_to_time(UNIX_TIMESTAMP sec, TM * tm) { uint16_t day; GREGORIAN_YEAR year; uint16_t days_in_year; uint8_t month; bool bFoundYear = false; bool bIsLeapYear = false; tm->tm_sec = sec % 60; sec /= 60; tm->tm_min = sec % 60; sec /= 60; tm->tm_hour = sec % 24; day = sec / 24; tm->tm_wday = (day + FIRST_DAY_OF_WEEK_1_JAN_1970) % 7; // weekday year = FIRST_UNIX_YEAR_GR; while (!bFoundYear) { bIsLeapYear = is_leap_year(year); days_in_year = bIsLeapYear ? 366 : 365; bFoundYear = day < days_in_year; if (!bFoundYear) { day -= days_in_year; year++; } } tm->tm_yday = day; /* Convert from year to C year (offset to 1900) */ tm->tm_year = year - 1900; for(month = 0; day >= days_in_month(month, bIsLeapYear); month++ ) { day -= days_in_month(month, bIsLeapYear); } tm->tm_mon = month; // 0..11 tm->tm_mday = day + 1; // 1..31 }
int main(int argc, char** argv) { FILE *fin = fopen("friday.in", "r"); FILE *fout = fopen("friday.out", "w"); int n; fscanf(fin, "%d", &n); int thirteenths[DAYS];/* SAT, SUN, MON, TUE, WED, THU, FRI */ thirteenths[0] = 0; thirteenths[1] = 0; thirteenths[2] = 0; thirteenths[3] = 0; thirteenths[4] = 0; thirteenths[5] = 0; thirteenths[6] = 0; int days_month[MONTHS] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int day = FIRST_THIRTEEN; int i, j; for (i = YEAR_START; i < n + YEAR_START; i++) { if (is_leap_year(i)) { days_month[1] = 29; } else { days_month[1] = 28; } for (j = 0; j < MONTHS; j++) { thirteenths[day]++; day += days_month[j]; day %= DAYS; } } fprintf( fout, "%d %d %d %d %d %d %d\n", thirteenths[0], thirteenths[1], thirteenths[2], thirteenths[3], thirteenths[4], thirteenths[5], thirteenths[6] ); fclose(fin); fclose(fout); return 0; }
static uint8_t month_days( uint16_t year, uint8_t month) { /* note: start with a zero in the first element to save us from a month - 1 calculation in the lookup */ int month_days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; /* February */ if ((month == 2) && is_leap_year(year)) return 29; else if (month >= 1 && month <= 12) return (uint8_t) month_days[month]; else return 0; }
static int days_in_month (int y, int m) { if (m == 0 || m == 2 || m == 4 || m == 6 || m == 7 || m == 9 || m == 11) return 31; else if (m == 8 || m == 5 || m == 3 || m == 10) return 30; else if (m == 1) { if (is_leap_year (y)) return 29; else return 28; } return -1; // if arrive here @m was out of range }
void add_yeardays(ha_time_t * a_time, int extra) { if (a_time->has->yeardays == FALSE) { crm_trace("has->yeardays == FALSE"); return; } if (extra < 0) { sub_yeardays(a_time, -extra); } else { do_add_field(a_time, yeardays, extra, (is_leap_year(a_time->years) ? 366 : 365), add_ordinalyears); } convert_from_ordinal(a_time); }
bool calc_mday_mon( unsigned yday, unsigned *mday, unsigned *mon, unsigned year ) { assert( (int)yday < days_in_year( year ) ); unsigned const *const ym = yday_mon[ is_leap_year( year ) ]; for ( unsigned m = 1; m <= 12; ++m ) if ( ym[ m ] > yday ) { --m; if ( mday ) *mday = yday - ym[ m ] + 1; if ( mon ) *mon = m; return true; } return false; }
int32_t date::days_of_month(int32_t __year, int32_t __month) { ufc_assert (__month >= 1 && __month <= 12); switch (__month) { case 2: return is_leap_year(__year)? 29: 28; case 4: case 6: case 9: case 11: return 30; default: return 31; } }
void time_increment_seconds(TM * tm) { if (!tm) { return; } incrementwithrollover(tm->tm_sec, 59); if (tm->tm_sec == 0) { incrementwithrollover(tm->tm_min, 59); } else { return; } if (tm->tm_min == 0) { incrementwithrollover(tm->tm_hour, 23); } else { return; } bool bIsLeapYear = is_leap_year(tm->tm_year); if (tm->tm_hour == 0) { incrementwithrollover(tm->tm_wday, (int)SAT); incrementwithrollover(tm->tm_mday, (int)days_in_month(tm->tm_mon, bIsLeapYear)); incrementwithrollover(tm->tm_yday, bIsLeapYear ? 365 : 364); } else { return; } if (tm->tm_mday == 0) { tm->tm_mday = 1; // Month starts at 1, not 0 incrementwithrollover(tm->tm_mon, 11); } else { return; } }
// Ok int KCalendarSystemHebrew::month(const TQDate& date) const { class h_date *sd = toHebrew(date); int month = sd->hd_mon; if ( is_leap_year( sd->hd_year ) ) { if( month == 13 /*AdarI*/ ) month = 6; else if( month == 14 /*AdarII*/ ) month = 7; else if ( month > 6 && month < 13 ) ++month; } return month; }