24
24
* Common functions for dealing with calendrical calculations
29
#if TIME_WITH_SYS_TIME
30
# include <sys/time.h>
34
# include <sys/time.h>
27
#include "drizzled/global.h"
41
28
#include "drizzled/calendar.h"
46
30
/** Static arrays for number of days in a month and their "day ends" */
47
31
static const uint32_t __leap_days_in_month[12]= {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
48
32
static const uint32_t __normal_days_in_month[12]= {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
53
37
* Private utility macro for enabling a switch between
54
38
* Gregorian and Julian leap year date arrays.
56
inline static const uint32_t* days_in_month(uint32_t y, enum calendar c)
58
if (is_leap_year(y, c))
59
return __leap_days_in_month;
61
return __normal_days_in_month;
64
inline static const uint32_t* days_to_end_month(uint32_t y, enum calendar c)
66
if (is_leap_year(y, c))
67
return __leap_days_to_end_month;
69
return __normal_days_to_end_month;
40
#define __DAYS_IN_MONTH(y, c) (const uint32_t *) (IS_LEAP_YEAR((y),(c)) ? __leap_days_in_month : __normal_days_in_month)
41
#define __DAYS_TO_END_MONTH(y, c) (const uint32_t *) (IS_LEAP_YEAR((y),(c)) ? __leap_days_to_end_month : __normal_days_to_end_month)
44
* Calculates the Julian Day Number from the year, month
45
* and day at noon supplied in the Julian calendar.
47
* The following formula is used to calculate the Julian
48
* Day Number from a date in the Julian Calendar.
50
* The months January to December are 1 to 12.
51
* Astronomical year numbering is used, thus 1 BC is 0, 2 BC is −1,
52
* and 4713 BC is −4712. In all divisions (except for JD) the floor
53
* function is applied to the quotient (for dates since
54
* March 1, −4800 all quotients are non-negative, so we can also
57
* a = (14 - month) / 12
60
* JDN = day + ((153m + 2) / 5) + 365y + (y / 4) - 32083
62
* @cite http://en.wikipedia.org/wiki/Julian_day#Calculation
66
* Year month and day values are assumed to be valid. This
67
* method does no bounds checking or validation.
70
* @param Month of date
73
int64_t julian_day_number_from_julian_date(uint32_t year, uint32_t month, uint32_t day)
76
int64_t a= (14 - month) / 12;
77
int64_t y= year + 4800 - a;
78
int64_t m= month + (12 * a) - 3;
80
day_number= day + (((153 * m) + 2) / 5) + (365 * y) + (y / 4) - 32083;
74
85
* Calculates the Julian Day Number from the year, month
302
313
return (day <= __normal_days_in_month[month - 1]);
305
const uint32_t *p_months= days_in_month(year, (enum calendar) GREGORIAN);
316
const uint32_t *p_months= __DAYS_IN_MONTH(year, (enum calendar) GREGORIAN);
306
317
return (day <= p_months[1]);
317
328
uint32_t days_in_gregorian_year_month(uint32_t year, uint32_t month)
319
const uint32_t *p_months= days_in_month(year, GREGORIAN);
330
const uint32_t *p_months= __DAYS_IN_MONTH(year, GREGORIAN);
320
331
return p_months[month - 1];
451
466
uint32_t week_number= (uint32_t) atoi(result);
469
* ISO8601:1988 states that if the first week in January
470
* does not contain 4 days, then the resulting week number
471
* shall be 52 or 53, depending on the number of days in the
472
* previous year. In this case, we adjust the outbound
473
* year parameter down a year.
475
if (year_out != NULL)
476
if (week_number == 53 || week_number == 52)
453
480
return week_number;