~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/calendar.cc

  • Committer: Brian Aker
  • Date: 2009-06-05 23:10:06 UTC
  • mto: This revision was merged to the branch mainline in revision 1055.
  • Revision ID: brian@gaz-20090605231006-01nyw7pfpj2z2v8p
Remove guts in parser for LOCK TABLE.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 * Common functions for dealing with calendrical calculations
25
25
 */
26
26
 
27
 
#include "config.h"
28
 
 
29
 
#if TIME_WITH_SYS_TIME
30
 
# include <sys/time.h>
31
 
# include <time.h>
32
 
#else
33
 
# if HAVE_SYS_TIME_H
34
 
#  include <sys/time.h>
35
 
# else
36
 
#  include <time.h>
37
 
# endif
38
 
#endif
39
 
#include <cstdlib>
40
 
 
 
27
#include "drizzled/global.h"
41
28
#include "drizzled/calendar.h"
42
29
 
43
 
namespace drizzled
44
 
{
45
 
 
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.
55
39
 */
56
 
inline static const uint32_t* days_in_month(uint32_t y, enum calendar c) 
57
 
{
58
 
  if (is_leap_year(y, c))
59
 
    return __leap_days_in_month;
60
 
  else
61
 
    return __normal_days_in_month;
62
 
}
63
 
 
64
 
inline static const uint32_t* days_to_end_month(uint32_t y, enum calendar c) 
65
 
{
66
 
  if (is_leap_year(y, c))
67
 
    return __leap_days_to_end_month;
68
 
  else
69
 
    return __normal_days_to_end_month;
70
 
}
71
 
 
 
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)
 
42
 
 
43
/**
 
44
 * Calculates the Julian Day Number from the year, month 
 
45
 * and day at noon supplied in the Julian calendar.
 
46
 *
 
47
 * The following formula is used to calculate the Julian
 
48
 * Day Number from a date in the Julian Calendar.
 
49
 *
 
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 
 
55
 * apply truncation).
 
56
 *
 
57
 * a = (14 - month) / 12
 
58
 * y = year + 4800 - a
 
59
 * m = month + 12a - 3
 
60
 * JDN = day + ((153m + 2) / 5) + 365y + (y / 4) - 32083
 
61
 *
 
62
 * @cite http://en.wikipedia.org/wiki/Julian_day#Calculation
 
63
 *
 
64
 * @note
 
65
 *
 
66
 * Year month and day values are assumed to be valid.  This 
 
67
 * method does no bounds checking or validation.
 
68
 *
 
69
 * @param Year of date
 
70
 * @param Month of date
 
71
 * @param Day of date
 
72
 */
 
73
int64_t julian_day_number_from_julian_date(uint32_t year, uint32_t month, uint32_t day)
 
74
{
 
75
  int64_t day_number;
 
76
  int64_t a= (14 - month) / 12;
 
77
  int64_t y= year + 4800 - a;
 
78
  int64_t m= month + (12 * a) - 3;
 
79
 
 
80
  day_number= day + (((153 * m) + 2) / 5) + (365 * y) + (y / 4) - 32083;
 
81
  return day_number;
 
82
}
72
83
 
73
84
/**
74
85
 * Calculates the Julian Day Number from the year, month 
302
313
    return (day <= __normal_days_in_month[month - 1]);
303
314
  else
304
315
  {
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]);
307
318
  }
308
319
}
316
327
 */
317
328
uint32_t days_in_gregorian_year_month(uint32_t year, uint32_t month)
318
329
{
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];
321
332
}
322
333
 
427
438
 */
428
439
uint32_t iso_week_number_from_gregorian_date(uint32_t year
429
440
                                           , uint32_t month
430
 
                                           , uint32_t day)
 
441
                                           , uint32_t day
 
442
                                           , uint32_t *year_out)
431
443
{
432
444
  struct tm broken_time;
433
445
 
 
446
  if (year_out != NULL)
 
447
    *year_out= year;
 
448
 
434
449
  broken_time.tm_year= year;
435
450
  broken_time.tm_mon= month - 1; /* struct tm has non-ordinal months */
436
451
  broken_time.tm_mday= day;
450
465
 
451
466
  uint32_t week_number= (uint32_t) atoi(result);
452
467
 
 
468
  /* 
 
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.
 
474
   */
 
475
  if (year_out != NULL)
 
476
    if (week_number == 53 || week_number == 52)
 
477
      if (month == 1)
 
478
        *year_out--;
 
479
 
453
480
  return week_number;
454
481
}
455
482
 
492
519
 
493
520
  return (years * 100) + (months % 12) + 1;
494
521
}
495
 
 
496
 
} /* namespace drizzled */