~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2008 Sun Microsystems
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/**
 * @file 
 *
 * Common functions for dealing with calendrical calculations
 */

#include "drizzled/global.h"
#include "drizzled/calendar.h"

/** Static arrays for number of days in a month and their "day ends" */
static const uint32_t __leap_days_in_month[12]=       {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
static const uint32_t __normal_days_in_month[12]=     {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
static const uint32_t __leap_days_to_end_month[13]=   {0, 31, 60, 91, 121, 151, 182, 213, 244, 274, 305, 335, 366};
static const uint32_t __normal_days_to_end_month[13]= {0, 31, 59, 90, 120, 150, 181, 212, 243, 273, 304, 334, 365};

/** 
 * Private utility macro for enabling a switch between
 * Gregorian and Julian leap year date arrays.
 */
#define __DAYS_IN_MONTH(y, c) (const uint32_t *) (IS_LEAP_YEAR((y),(c)) ? __leap_days_in_month : __normal_days_in_month)
#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)

/**
 * Calculates the Julian Day Number from the year, month 
 * and day at noon supplied in the Julian calendar.
 *
 * The following formula is used to calculate the Julian
 * Day Number from a date in the Julian Calendar.
 *
 * The months January to December are 1 to 12. 
 * Astronomical year numbering is used, thus 1 BC is 0, 2 BC is −1, 
 * and 4713 BC is −4712. In all divisions (except for JD) the floor 
 * function is applied to the quotient (for dates since 
 * March 1, −4800 all quotients are non-negative, so we can also 
 * apply truncation).
 *
 * a = (14 - month) / 12
 * y = year + 4800 - a
 * m = month + 12a - 3
 * JDN = day + ((153m + 2) / 5) + 365y + (y / 4) - 32083
 *
 * @cite http://en.wikipedia.org/wiki/Julian_day#Calculation
 *
 * @note
 *
 * Year month and day values are assumed to be valid.  This 
 * method does no bounds checking or validation.
 *
 * @param Year of date
 * @param Month of date
 * @param Day of date
 */
int64_t julian_day_number_from_julian_date(uint32_t year, uint32_t month, uint32_t day)
{
  int64_t day_number;
  int64_t a= (14 - month) / 12;
  int64_t y= year + 4800 - a;
  int64_t m= month + (12 * a) - 3;

  day_number= day + (((153 * m) + 2) / 5) + (365 * y) + (y / 4) - 32083;
  return day_number;
}

/**
 * Calculates the Julian Day Number from the year, month 
 * and day supplied.  The calendar used by the supplied
 * year, month, and day is assumed to be Gregorian Proleptic.
 *
 * The months January to December are 1 to 12. 
 * Astronomical year numbering is used, thus 1 BC is 0, 2 BC is −1, 
 * and 4713 BC is −4712. In all divisions (except for JD) the floor 
 * function is applied to the quotient (for dates since 
 * March 1, −4800 all quotients are non-negative, so we can also 
 * apply truncation).
 *
 * a = (14 - month) / 12
 * y = year + 4800 - a
 * m = month + 12a - 3
 * JDN = day + ((153m + 2) / 5) + 365y + (y / 4) - (y / 100) + (y / 400) - 32045
 *
 * @cite http://en.wikipedia.org/wiki/Julian_day#Calculation
 *
 * @note
 *
 * Year month and day values are assumed to be valid.  This 
 * method does no bounds checking or validation.
 *
 * @param Year of date
 * @param Month of date
 * @param Day of date
 */
int64_t julian_day_number_from_gregorian_date(uint32_t year, uint32_t month, uint32_t day)
{
  int64_t day_number;
  int64_t a= (14 - month) / 12;
  int64_t y= year + 4800 - a;
  int64_t m= month + (12 * a) - 3;

  day_number= day + (((153 * m) + 2) / 5) + (365 * y) + (y / 4) - (y / 100) + (y / 400) - 32045;
  return day_number;
}

/**
 * Translates an absolute day number to a 
 * Julian day number.  Note that a Julian day number
 * is not the same as a date in the Julian proleptic calendar.
 *
 * @param The absolute day number
 */
int64_t absolute_day_number_to_julian_day_number(int64_t absolute_day)
{
  return absolute_day + JULIAN_DAY_NUMBER_AT_ABSOLUTE_DAY_ONE;
}

/**
 * Translates a Julian day number to an 
 * absolute day number.  Note that a Julian day number
 * is not the same as a date in the Julian proleptic calendar.
 *
 * @param The Julian day number
 */
int64_t julian_day_number_to_absolute_day_number(int64_t julian_day)
{
  return julian_day - JULIAN_DAY_NUMBER_AT_ABSOLUTE_DAY_ONE;
}

/**
 * Given a supplied Julian Day Number, populates a year, month, and day
 * with the date in the Gregorian Proleptic calendar which corresponds to
 * the given Julian Day Number.
 *
 * @cite Algorithm from http://en.wikipedia.org/wiki/Julian_day
 *
 * @param Julian Day Number
 * @param Pointer to year to populate
 * @param Pointer to month to populate
 * @param Pointer to the day to populate
 */
void gregorian_date_from_julian_day_number(int64_t julian_day
                                         , uint32_t *year_out
                                         , uint32_t *month_out
                                         , uint32_t *day_out)
{
  int64_t j = julian_day + 32044;
  int64_t g = j / 146097;
  int64_t dg = j % 146097;
  int64_t c = (dg / 36524 + 1) * 3 / 4;
  int64_t dc = dg - c * 36524;
  int64_t b = dc / 1461;
  int64_t db = dc % 1461;
  int64_t a = (db / 365 + 1) * 3 / 4;
  int64_t da = db - a * 365;
  int64_t y = g * 400 + c * 100 + b * 4 + a;
  int64_t m = (da * 5 + 308) / 153 - 2;
  int64_t d = da - (m + 4) * 153 / 5 + 122;
  int64_t Y = y - 4800 + (m + 2) / 12;
  int64_t M = (m + 2) % 12 + 1;
  int64_t D = d + 1.5;

  /* Push out parameters */
  *year_out= (uint32_t) Y;
  *month_out= (uint32_t) M;
  *day_out= (uint32_t) D;
}

/**
 * Given a supplied Absolute Day Number, populates a year, month, and day
 * with the date in the Gregorian Proleptic calendar which corresponds to
 * the given Absolute Day Number.
 *
 * @param Absolute Day Number
 * @param Pointer to year to populate
 * @param Pointer to month to populate
 * @param Pointer to the day to populate
 */
void gregorian_date_from_absolute_day_number(int64_t absolute_day
                                           , uint32_t *year_out
                                           , uint32_t *month_out
                                           , uint32_t *day_out)
{
  gregorian_date_from_julian_day_number(
      absolute_day_number_to_julian_day_number(absolute_day)
    , year_out
    , month_out
    , day_out);
}

/**
 * Functions to calculate the number of days in a 
 * particular year.  The number of days in a year 
 * depends on the calendar used for the date.
 *
 * For the Julian proleptic calendar, a leap year 
 * is one which is evenly divisible by 4.
 *
 * For the Gregorian proleptic calendar, a leap year
 * is one which is evenly divisible by 4, and if
 * the year is evenly divisible by 100, it must also be evenly
 * divisible by 400.
 */

/**
 * Returns the number of days in a particular year
 * depending on the supplied calendar.
 *
 * @param year to evaluate
 * @param calendar to use
 */
inline uint32_t days_in_year(const uint32_t year, enum calendar calendar)
{
  if (calendar == GREGORIAN)
    return days_in_year_gregorian(year);
  return days_in_year_julian(year);
}

/**
 * Returns the number of days in a particular Julian calendar year.
 *
 * @param year to evaluate
 */
inline uint32_t days_in_year_julian(const uint32_t year)
{
  /* Short-circuit. No odd years can be leap years... */
  return (year & 3) == 0;
}

/**
 * Returns the number of days in a particular Gregorian year.
 *
 * @param year to evaluate
 */
inline uint32_t days_in_year_gregorian(const uint32_t year)
{
  /* Short-circuit. No odd years can be leap years... */
  if ((year & 1) == 1)
    return 365;
  return (            
            (year & 3) == 0 
            && (year % 100 || ((year % 400 == 0) && year)) 
            ? 366 
            : 365
         );
}

/**
 * Returns the number of the day in a week.
 *
 * Return values:
 *
 * Day            Day Number  Sunday first day?
 * -------------- ----------- -----------------
 * Sunday         0           true
 * Monday         1           true
 * Tuesday        2           true
 * Wednesday      3           true
 * Thursday       4           true
 * Friday         5           true
 * Saturday       6           true
 * Sunday         6           false
 * Monday         0           false
 * Tuesday        1           false
 * Wednesday      2           false
 * Thursday       3           false
 * Friday         4           false
 * Saturday       5           false
 *
 * @param Julian Day Number
 * @param Consider Sunday the first day of the week?
 */
uint32_t day_of_week(int64_t day_number
                   , bool sunday_is_first_day_of_week)
{
  uint32_t tmp= (uint32_t) (day_number % 7);
  /* 0 returned from above modulo is a Monday */
  if (sunday_is_first_day_of_week)
    tmp= (tmp == 6 ? 0 : tmp + 1);
  return tmp;
}

/**
 * Given a year, month, and day, returns whether the date is 
 * valid for the Gregorian proleptic calendar.
 *
 * @param The year
 * @param The month
 * @param The day
 */
bool is_valid_gregorian_date(uint32_t year, uint32_t month, uint32_t day)
{
  if (year < 1)
    return false;
  if (month != 2)
    return (day <= __normal_days_in_month[month - 1]);
  else
  {
    const uint32_t *p_months= __DAYS_IN_MONTH(year, (enum calendar) GREGORIAN);
    return (day <= p_months[1]);
  }
}

/**
 * Returns the number of days in a month, given
 * a year and a month in the Gregorian calendar.
 *
 * @param Year in Gregorian Proleptic calendar
 * @param Month in date
 */
uint32_t days_in_gregorian_year_month(uint32_t year, uint32_t month)
{
  const uint32_t *p_months= __DAYS_IN_MONTH(year, GREGORIAN);
  return p_months[month - 1];
}

/**
 * Returns whether the supplied date components are within the 
 * range of the UNIX epoch.
 *
 * Times in the range of 1970-01-01T00:00:00 to 2038-01-19T03:14:07
 *
 * @param Year
 * @param Month
 * @param Day
 * @param Hour
 * @param Minute
 * @param Second
 */
bool in_unix_epoch_range(uint32_t year
                       , uint32_t month
                       , uint32_t day
                       , uint32_t hour
                       , uint32_t minute
                       , uint32_t second)
{
  if (month == 0 || day == 0)
    return false;
  if (year < UNIX_EPOCH_MAX_YEARS
      && year >= UNIX_EPOCH_MIN_YEARS)
    return true;
  if (year < UNIX_EPOCH_MIN_YEARS)
    return false;
  if (year == UNIX_EPOCH_MAX_YEARS)
  {
    if (month > 1)
      return false;
    if (day > 19)
      return false;
    else if (day < 19)
      return true;
    else
    {
      /* We are on the final day of UNIX Epoch */
      uint32_t seconds= (hour * 60 * 60)
                      + (minute * 60)
                      + (second);
      if (seconds <= ((3 * 60 * 60) + (14 * 60) + 7))
        return true;
      return false;
    }
  }
  return false;
}

/**
 * Returns the number of the week from a supplied year, month, and
 * date in the Gregorian proleptic calendar.  We use strftime() and
 * the %U, %W, and %V format specifiers depending on the value
 * of the sunday_is_first_day_of_week parameter.
 *
 * @param Subject year
 * @param Subject month
 * @param Subject day
 * @param Is sunday the first day of the week?
 * @param Pointer to a uint32_t to hold the resulting year, which 
 *        may be incremented or decremented depending on flags
 */
uint32_t week_number_from_gregorian_date(uint32_t year
                                       , uint32_t month
                                       , uint32_t day
                                       , bool sunday_is_first_day_of_week)
{
  struct tm broken_time;

  broken_time.tm_year= year;
  broken_time.tm_mon= month - 1; /* struct tm has non-ordinal months */
  broken_time.tm_mday= day;

  /* fill out the rest of our tm fields. */
  (void) mktime(&broken_time);

  char result[3]; /* 3 is enough space for a max 2-digit week number */
  size_t result_len= strftime(result
                            , sizeof(result)
                            , (sunday_is_first_day_of_week ? "%U" : "%W")
                            , &broken_time);

  if (result_len != 0)
    return (uint32_t) atoi(result);
  return 0;
}

/**
 * Returns the ISO week number of a supplied year, month, and
 * date in the Gregorian proleptic calendar.  We use strftime() and
 * the %V format specifier to do the calculation, which yields a
 * correct ISO 8601:1988 week number.
 *
 * The final year_out parameter is a pointer to an integer which will
 * be set to the year in which the week belongs, according to ISO8601:1988, 
 * which may be different from the Gregorian calendar year.
 *
 * @see http://en.wikipedia.org/wiki/ISO_8601
 *
 * @param Subject year
 * @param Subject month
 * @param Subject day
 * @param Pointer to a uint32_t to hold the resulting year, which 
 *        may be incremented or decremented depending on flags
 */
uint32_t iso_week_number_from_gregorian_date(uint32_t year
                                           , uint32_t month
                                           , uint32_t day
                                           , uint32_t *year_out)
{
  struct tm broken_time;

  if (year_out != NULL)
    *year_out= year;

  broken_time.tm_year= year;
  broken_time.tm_mon= month - 1; /* struct tm has non-ordinal months */
  broken_time.tm_mday= day;

  /* fill out the rest of our tm fields. */
  (void) mktime(&broken_time);

  char result[3]; /* 3 is enough space for a max 2-digit week number */
  size_t result_len= strftime(result
                            , sizeof(result)
                            , "%V"
                            , &broken_time);


  if (result_len == 0)
    return 0; /* Not valid for ISO8601:1988 */

  uint32_t week_number= (uint32_t) atoi(result);

  /* 
   * ISO8601:1988 states that if the first week in January
   * does not contain 4 days, then the resulting week number
   * shall be 52 or 53, depending on the number of days in the
   * previous year.  In this case, we adjust the outbound
   * year parameter down a year.
   */
  if (year_out != NULL)
    if (week_number == 53 || week_number == 52)
      if (month == 1)
        *year_out--;

  return week_number;
}