uint8_t time_get_days_in_month(months_t month, uint16_t year) { switch (month) { case January: return 31; case Februrary: if (time_is_leap_year(year)) return 29; else return 28; case March: return 31; case April: return 30; case May: return 31; case Juni: return 30; case Juli: return 31; case August: return 31; case September: return 30; case October: return 31; case November: return 30; case December: return 31; default: return 0; } return 0; }
/** * time_day_of_year: * @day: The day. * @month: The month. * @year: The year. * * Returns the 1-based day number within the year of the specified date. * Year is the normal year, e.g. 2001. Month is 0 to 11. * * Return value: the day of the year. */ int time_day_of_year (int day, int month, int year) { int i; for (i = 0; i < month; i++) { day += days_in_month[i]; if (i == 1 && time_is_leap_year (year)) day++; } return day; }
/** * time_days_in_month: * @year: The year. * @month: The month. * * Returns the number of days in the month. Year is the normal year, e.g. 2001. * Month is 0 (Jan) to 11 (Dec). * * Return value: number of days in the given month/year. */ int time_days_in_month (int year, int month) { int days; g_return_val_if_fail (year >= 1900, 0); g_return_val_if_fail ((month >= 0) && (month < 12), 0); days = days_in_month[month]; if (month == 1 && time_is_leap_year (year)) days++; return days; }
// Static Method to return ISO WeekNumber (1-53) for a given year int16_t time_get_weeknumber(uint8_t day, uint8_t month, uint16_t year) { int16_t yyyy = year; int16_t mm= month; int16_t dd = day; // Declare other required variables int16_t day_of_year_number; int16_t jan_1_weekday; int16_t week_number = 0, weekday; int16_t i,j,k,l,m,n; int16_t mnth[12] = {0,31,59,90,120,151,181,212,243,273,304,334}; int16_t year_number; // Set DayofYear Number for yyyy mm dd day_of_year_number = dd + mnth[mm-1]; // Increase of Dayof Year Number by 1, if year is leapyear and month is february if ((time_is_leap_year(yyyy) == true) && (mm == 2)) day_of_year_number += 1; // Find the Jan1WeekDay for year i = (yyyy - 1) % 100; j = (yyyy - 1) - i; k = i + i/4; jan_1_weekday = 1 + (((((j / 100) % 4) * 5) + k) % 7); // Calcuate the WeekDay for the given date l = day_of_year_number + (jan_1_weekday - 1); weekday = 1 + ((l - 1) % 7); // Find if the date falls in YearNumber set WeekNumber to 52 or 53 if ((day_of_year_number <= (8 - jan_1_weekday)) && (jan_1_weekday > 4)) { year_number = yyyy - 1; if ((jan_1_weekday == 5) || ((jan_1_weekday == 6) && (jan_1_weekday > 4))) week_number = 53; else week_number = 52; } else year_number = yyyy; // Set WeekNumber to 1 to 53 if date falls in YearNumber if (year_number == yyyy) { if (time_is_leap_year(yyyy)==true) m = 366; else m = 365; if ((m - day_of_year_number) < (4-weekday)) { year_number = yyyy + 1; week_number = 1; } } if (year_number==yyyy) { n = day_of_year_number + (7 - weekday) + (jan_1_weekday -1); week_number = n / 7; if (jan_1_weekday > 4) week_number -= 1; } return (week_number); }