29 #if TIME_WITH_SYS_TIME
30 # include <sys/time.h>
34 # include <sys/time.h>
47 static const uint32_t
__leap_days_in_month[12]= {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
48 static const uint32_t __normal_days_in_month[12]= {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
49 static const uint32_t __leap_days_to_end_month[13]= {0, 31, 60, 91, 121, 151, 182, 213, 244, 274, 305, 335, 366};
50 static const uint32_t __normal_days_to_end_month[13]= {0, 31, 59, 90, 120, 150, 181, 212, 243, 273, 304, 334, 365};
56 inline static const uint32_t* days_in_month(uint32_t y,
enum calendar c)
61 return __normal_days_in_month;
64 inline static const uint32_t* days_to_end_month(uint32_t y,
enum calendar c)
67 return __leap_days_to_end_month;
69 return __normal_days_to_end_month;
104 int64_t a= (14 - month) / 12;
105 int64_t y= year + 4800 - a;
106 int64_t m= month + (12 * a) - 3;
108 day_number= day + (((153 * m) + 2) / 5) + (365 * y) + (y / 4) - (y / 100) + (y / 400) - 32045;
121 return absolute_day + JULIAN_DAY_NUMBER_AT_ABSOLUTE_DAY_ONE;
133 return julian_day - JULIAN_DAY_NUMBER_AT_ABSOLUTE_DAY_ONE;
150 , uint32_t *month_out
153 int64_t j = julian_day + 32044;
154 int64_t g = j / 146097;
155 int64_t dg = j % 146097;
156 int64_t c = (dg / 36524 + 1) * 3 / 4;
157 int64_t dc = dg - c * 36524;
158 int64_t b = dc / 1461;
159 int64_t db = dc % 1461;
160 int64_t a = (db / 365 + 1) * 3 / 4;
161 int64_t da = db - a * 365;
162 int64_t y = g * 400 + c * 100 + b * 4 + a;
163 int64_t m = (da * 5 + 308) / 153 - 2;
164 int64_t d = da - (m + 4) * 153 / 5 + 122;
165 int64_t Y = y - 4800 + (m + 2) / 12;
166 int64_t M = (m + 2) % 12 + 1;
167 int64_t D = (int64_t)((
double)d + 1.5);
170 *year_out= (uint32_t) Y;
171 *month_out= (uint32_t) M;
172 *day_out= (uint32_t) D;
187 , uint32_t *month_out
220 if (calendar == GREGORIAN)
233 return (year & 3) == 0;
248 && (year % 100 || ((year % 400 == 0) && year))
280 ,
bool sunday_is_first_day_of_week)
282 uint32_t tmp= (uint32_t) (day_number % 7);
284 if (sunday_is_first_day_of_week)
285 tmp= (tmp == 6 ? 0 : tmp + 1);
302 return (day <= __normal_days_in_month[month - 1]);
305 const uint32_t *p_months= days_in_month(year, (
enum calendar) GREGORIAN);
306 return (day <= p_months[1]);
319 const uint32_t *p_months= days_in_month(year, GREGORIAN);
320 return p_months[month - 1];
343 if (month == 0 || day == 0)
346 if (year < UNIX_EPOCH_MAX_YEARS
347 && year >= UNIX_EPOCH_MIN_YEARS)
350 if (year < UNIX_EPOCH_MIN_YEARS)
353 if (year == UNIX_EPOCH_MAX_YEARS)
370 uint32_t seconds= (hour * 60 * 60)
373 if (seconds <= ((3 * 60 * 60) + (14 * 60) + 7))
397 ,
bool sunday_is_first_day_of_week)
399 struct tm broken_time;
401 broken_time.tm_year= year;
402 broken_time.tm_mon= month - 1;
403 broken_time.tm_mday= day;
406 (void) mktime(&broken_time);
409 size_t result_len= strftime(result
411 , (sunday_is_first_day_of_week ?
"%U" :
"%W")
415 return (uint32_t) atoi(result);
441 struct tm broken_time;
443 broken_time.tm_year= year;
444 broken_time.tm_mon= month - 1;
445 broken_time.tm_mday= day;
448 (void) mktime(&broken_time);
451 size_t result_len= strftime(result
460 uint32_t week_number= (uint32_t) atoi(result);
476 uint32_t years= year_month / 100;
477 if (years < CALENDAR_YY_PART_YEAR)
479 else if (years < 100)
482 uint32_t months= year_month % 100;
483 return (years * 12) + (months - 1);
497 uint32_t years= (months / 12);
500 years+= (years < CALENDAR_YY_PART_YEAR) ? 2000 : 1900;
502 return (years * 100) + (months % 12) + 1;
uint32_t days_in_gregorian_year_month(uint32_t year, uint32_t month)
bool is_valid_gregorian_date(uint32_t year, uint32_t month, uint32_t day)
static bool is_leap_year(uint32_t y, enum calendar c)
TODO: Rename this file - func.h is stupid.
uint32_t year_month_to_months(uint32_t year_month)
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)
static const uint32_t __leap_days_in_month[12]
uint32_t months_to_year_month(uint32_t months)
uint32_t days_in_year_julian(const uint32_t year)
uint32_t days_in_year(const uint32_t year, enum calendar calendar)
void gregorian_date_from_absolute_day_number(int64_t absolute_day, uint32_t *year_out, uint32_t *month_out, uint32_t *day_out)
void gregorian_date_from_julian_day_number(int64_t julian_day, uint32_t *year_out, uint32_t *month_out, uint32_t *day_out)
bool in_unix_epoch_range(uint32_t year, uint32_t month, uint32_t day, uint32_t hour, uint32_t minute, uint32_t second)
uint32_t iso_week_number_from_gregorian_date(uint32_t year, uint32_t month, uint32_t day)
int64_t absolute_day_number_to_julian_day_number(int64_t absolute_day)
uint32_t day_of_week(int64_t day_number, bool sunday_is_first_day_of_week)
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_to_absolute_day_number(int64_t julian_day)