39static const uint32_t
__leap_days_in_month[12]= {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
40static const uint32_t __normal_days_in_month[12]= {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
41static const uint32_t __leap_days_to_end_month[13]= {0, 31, 60, 91, 121, 151, 182, 213, 244, 274, 305, 335, 366};
42static const uint32_t __normal_days_to_end_month[13]= {0, 31, 59, 90, 120, 150, 181, 212, 243, 273, 304, 334, 365};
48inline static const uint32_t* days_in_month(uint32_t y,
enum calendar c)
53 return __normal_days_in_month;
56inline static const uint32_t* days_to_end_month(uint32_t y,
enum calendar c)
59 return __leap_days_to_end_month;
61 return __normal_days_to_end_month;
96 int64_t a= (14 - month) / 12;
97 int64_t y= year + 4800 - a;
98 int64_t m= month + (12 * a) - 3;
100 day_number= day + (((153 * m) + 2) / 5) + (365 * y) + (y / 4) - (y / 100) + (y / 400) - 32045;
113 return absolute_day + JULIAN_DAY_NUMBER_AT_ABSOLUTE_DAY_ONE;
125 return julian_day - JULIAN_DAY_NUMBER_AT_ABSOLUTE_DAY_ONE;
142 , uint32_t *month_out
145 int64_t j = julian_day + 32044;
146 int64_t g = j / 146097;
147 int64_t dg = j % 146097;
148 int64_t c = (dg / 36524 + 1) * 3 / 4;
149 int64_t dc = dg - c * 36524;
150 int64_t b = dc / 1461;
151 int64_t db = dc % 1461;
152 int64_t a = (db / 365 + 1) * 3 / 4;
153 int64_t da = db - a * 365;
154 int64_t y = g * 400 + c * 100 + b * 4 + a;
155 int64_t m = (da * 5 + 308) / 153 - 2;
156 int64_t d = da - (m + 4) * 153 / 5 + 122;
157 int64_t Y = y - 4800 + (m + 2) / 12;
158 int64_t M = (m + 2) % 12 + 1;
159 int64_t D = (int64_t)((
double)d + 1.5);
162 *year_out= (uint32_t) Y;
163 *month_out= (uint32_t) M;
164 *day_out= (uint32_t) D;
179 , uint32_t *month_out
225 return (year & 3) == 0;
240 && (year % 100 || ((year % 400 == 0) && year))
272 ,
bool sunday_is_first_day_of_week)
274 uint32_t tmp= (uint32_t) (day_number % 7);
276 if (sunday_is_first_day_of_week)
277 tmp= (tmp == 6 ? 0 : tmp + 1);
294 return (day <= __normal_days_in_month[month - 1]);
297 const uint32_t *p_months= days_in_month(year, (
enum calendar) GREGORIAN);
298 return (day <= p_months[1]);
311 const uint32_t *p_months= days_in_month(year, GREGORIAN);
312 return p_months[month - 1];
335 if (month == 0 || day == 0)
338 if (year < UNIX_EPOCH_MAX_YEARS
339 && year >= UNIX_EPOCH_MIN_YEARS)
342 if (year < UNIX_EPOCH_MIN_YEARS)
345 if (year == UNIX_EPOCH_MAX_YEARS)
362 uint32_t seconds= (hour * 60 * 60)
365 if (seconds <= ((3 * 60 * 60) + (14 * 60) + 7))
389 ,
bool sunday_is_first_day_of_week)
391 struct tm broken_time;
393 broken_time.tm_year= year;
394 broken_time.tm_mon= month - 1;
395 broken_time.tm_mday= day;
398 (void) mktime(&broken_time);
401 size_t result_len= strftime(result
403 , (sunday_is_first_day_of_week ?
"%U" :
"%W")
407 return (uint32_t) atoi(result);
433 struct tm broken_time;
435 broken_time.tm_year= year;
436 broken_time.tm_mon= month - 1;
437 broken_time.tm_mday= day;
440 (void) mktime(&broken_time);
443 size_t result_len= strftime(result
452 uint32_t week_number= (uint32_t) atoi(result);
468 uint32_t years= year_month / 100;
469 if (years < CALENDAR_YY_PART_YEAR)
471 else if (years < 100)
474 uint32_t months= year_month % 100;
475 return (years * 12) + (months - 1);
489 uint32_t years= (months / 12);
492 years+= (years < CALENDAR_YY_PART_YEAR) ? 2000 : 1900;
494 return (years * 100) + (months % 12) + 1;
TODO: Rename this file - func.h is stupid.
int64_t absolute_day_number_to_julian_day_number(int64_t absolute_day)
uint32_t iso_week_number_from_gregorian_date(uint32_t year, uint32_t month, uint32_t day)
uint32_t days_in_year_julian(const uint32_t year)
void gregorian_date_from_julian_day_number(int64_t julian_day, uint32_t *year_out, uint32_t *month_out, uint32_t *day_out)
uint32_t months_to_year_month(uint32_t months)
bool is_valid_gregorian_date(uint32_t year, uint32_t month, uint32_t day)
uint32_t week_number_from_gregorian_date(uint32_t year, uint32_t month, uint32_t day, bool sunday_is_first_day_of_week)
int64_t julian_day_number_from_gregorian_date(uint32_t year, uint32_t month, uint32_t day)
uint32_t days_in_year_gregorian(const uint32_t year)
uint32_t year_month_to_months(uint32_t year_month)
static bool is_leap_year(uint32_t y, enum calendar c)
uint32_t day_of_week(int64_t day_number, bool sunday_is_first_day_of_week)
uint32_t days_in_year(const uint32_t year, enum calendar calendar)
uint32_t days_in_gregorian_year_month(uint32_t year, uint32_t month)
void gregorian_date_from_absolute_day_number(int64_t absolute_day, uint32_t *year_out, uint32_t *month_out, uint32_t *day_out)
static const uint32_t __leap_days_in_month[12]
bool in_unix_epoch_range(uint32_t year, uint32_t month, uint32_t day, uint32_t hour, uint32_t minute, uint32_t second)
int64_t julian_day_number_to_absolute_day_number(int64_t julian_day)