drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
calendar.cc
Go to the documentation of this file.
1/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2008 Sun Microsystems, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
26
27#include <config.h>
28
29#include <cstdlib>
30#include <sys/time.h>
31#include <time.h>
32
33#include <drizzled/calendar.h>
34
35namespace drizzled
36{
37
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};
43
48inline static const uint32_t* days_in_month(uint32_t y, enum calendar c)
49{
50 if (is_leap_year(y, c))
52 else
53 return __normal_days_in_month;
54}
55
56inline static const uint32_t* days_to_end_month(uint32_t y, enum calendar c)
57{
58 if (is_leap_year(y, c))
59 return __leap_days_to_end_month;
60 else
61 return __normal_days_to_end_month;
62}
63
64
93int64_t julian_day_number_from_gregorian_date(uint32_t year, uint32_t month, uint32_t day)
94{
95 int64_t day_number;
96 int64_t a= (14 - month) / 12;
97 int64_t y= year + 4800 - a;
98 int64_t m= month + (12 * a) - 3;
99
100 day_number= day + (((153 * m) + 2) / 5) + (365 * y) + (y / 4) - (y / 100) + (y / 400) - 32045;
101 return day_number;
102}
103
111int64_t absolute_day_number_to_julian_day_number(int64_t absolute_day)
112{
113 return absolute_day + JULIAN_DAY_NUMBER_AT_ABSOLUTE_DAY_ONE;
114}
115
124{
125 return julian_day - JULIAN_DAY_NUMBER_AT_ABSOLUTE_DAY_ONE;
126}
127
141 , uint32_t *year_out
142 , uint32_t *month_out
143 , uint32_t *day_out)
144{
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);
160
161 /* Push out parameters */
162 *year_out= (uint32_t) Y;
163 *month_out= (uint32_t) M;
164 *day_out= (uint32_t) D;
165}
166
178 , uint32_t *year_out
179 , uint32_t *month_out
180 , uint32_t *day_out)
181{
184 , year_out
185 , month_out
186 , day_out);
187}
188
202
210inline uint32_t days_in_year(const uint32_t year, enum calendar calendar)
211{
212 if (calendar == GREGORIAN)
213 return days_in_year_gregorian(year);
214 return days_in_year_julian(year);
215}
216
222inline uint32_t days_in_year_julian(const uint32_t year)
223{
224 /* Short-circuit. No odd years can be leap years... */
225 return (year & 3) == 0;
226}
227
233inline uint32_t days_in_year_gregorian(const uint32_t year)
234{
235 /* Short-circuit. No odd years can be leap years... */
236 if ((year & 1) == 1)
237 return 365;
238 return (
239 (year & 3) == 0
240 && (year % 100 || ((year % 400 == 0) && year))
241 ? 366
242 : 365
243 );
244}
245
271uint32_t day_of_week(int64_t day_number
272 , bool sunday_is_first_day_of_week)
273{
274 uint32_t tmp= (uint32_t) (day_number % 7);
275 /* 0 returned from above modulo is a Monday */
276 if (sunday_is_first_day_of_week)
277 tmp= (tmp == 6 ? 0 : tmp + 1);
278 return tmp;
279}
280
289bool is_valid_gregorian_date(uint32_t year, uint32_t month, uint32_t day)
290{
291 if (year < 1)
292 return false;
293 if (month != 2)
294 return (day <= __normal_days_in_month[month - 1]);
295 else
296 {
297 const uint32_t *p_months= days_in_month(year, (enum calendar) GREGORIAN);
298 return (day <= p_months[1]);
299 }
300}
301
309uint32_t days_in_gregorian_year_month(uint32_t year, uint32_t month)
310{
311 const uint32_t *p_months= days_in_month(year, GREGORIAN);
312 return p_months[month - 1];
313}
314
328bool in_unix_epoch_range(uint32_t year,
329 uint32_t month,
330 uint32_t day,
331 uint32_t hour,
332 uint32_t minute,
333 uint32_t second)
334{
335 if (month == 0 || day == 0)
336 return false;
337
338 if (year < UNIX_EPOCH_MAX_YEARS
339 && year >= UNIX_EPOCH_MIN_YEARS)
340 return true;
341
342 if (year < UNIX_EPOCH_MIN_YEARS)
343 return false;
344
345 if (year == UNIX_EPOCH_MAX_YEARS)
346 {
347 if (month > 1)
348 {
349 return false;
350 }
351 if (day > 19)
352 {
353 return false;
354 }
355 else if (day < 19)
356 {
357 return true;
358 }
359 else
360 {
361 /* We are on the final day of UNIX Epoch */
362 uint32_t seconds= (hour * 60 * 60)
363 + (minute * 60)
364 + (second);
365 if (seconds <= ((3 * 60 * 60) + (14 * 60) + 7))
366 return true;
367 return false;
368 }
369 }
370 return false;
371}
372
387 , uint32_t month
388 , uint32_t day
389 , bool sunday_is_first_day_of_week)
390{
391 struct tm broken_time;
392
393 broken_time.tm_year= year;
394 broken_time.tm_mon= month - 1; /* struct tm has non-ordinal months */
395 broken_time.tm_mday= day;
396
397 /* fill out the rest of our tm fields. */
398 (void) mktime(&broken_time);
399
400 char result[3]; /* 3 is enough space for a max 2-digit week number */
401 size_t result_len= strftime(result
402 , sizeof(result)
403 , (sunday_is_first_day_of_week ? "%U" : "%W")
404 , &broken_time);
405
406 if (result_len != 0)
407 return (uint32_t) atoi(result);
408 return 0;
409}
410
430 , uint32_t month
431 , uint32_t day)
432{
433 struct tm broken_time;
434
435 broken_time.tm_year= year;
436 broken_time.tm_mon= month - 1; /* struct tm has non-ordinal months */
437 broken_time.tm_mday= day;
438
439 /* fill out the rest of our tm fields. */
440 (void) mktime(&broken_time);
441
442 char result[3]; /* 3 is enough space for a max 2-digit week number */
443 size_t result_len= strftime(result
444 , sizeof(result)
445 , "%V"
446 , &broken_time);
447
448
449 if (result_len == 0)
450 return 0; /* Not valid for ISO8601:1988 */
451
452 uint32_t week_number= (uint32_t) atoi(result);
453
454 return week_number;
455}
456
463uint32_t year_month_to_months(uint32_t year_month)
464{
465 if (year_month == 0)
466 return 0L;
467
468 uint32_t years= year_month / 100;
469 if (years < CALENDAR_YY_PART_YEAR)
470 years+= 2000;
471 else if (years < 100)
472 years+= 1900;
473
474 uint32_t months= year_month % 100;
475 return (years * 12) + (months - 1);
476}
477
484uint32_t months_to_year_month(uint32_t months)
485{
486 if (months == 0L)
487 return 0L;
488
489 uint32_t years= (months / 12);
490
491 if (years < 100)
492 years+= (years < CALENDAR_YY_PART_YEAR) ? 2000 : 1900;
493
494 return (years * 100) + (months % 12) + 1;
495}
496
497} /* namespace drizzled */
TODO: Rename this file - func.h is stupid.
int64_t absolute_day_number_to_julian_day_number(int64_t absolute_day)
Definition calendar.cc:111
uint32_t iso_week_number_from_gregorian_date(uint32_t year, uint32_t month, uint32_t day)
Definition calendar.cc:429
uint32_t days_in_year_julian(const uint32_t year)
Definition calendar.cc:222
void gregorian_date_from_julian_day_number(int64_t julian_day, uint32_t *year_out, uint32_t *month_out, uint32_t *day_out)
Definition calendar.cc:140
uint32_t months_to_year_month(uint32_t months)
Definition calendar.cc:484
bool is_valid_gregorian_date(uint32_t year, uint32_t month, uint32_t day)
Definition calendar.cc:289
uint32_t week_number_from_gregorian_date(uint32_t year, uint32_t month, uint32_t day, bool sunday_is_first_day_of_week)
Definition calendar.cc:386
int64_t julian_day_number_from_gregorian_date(uint32_t year, uint32_t month, uint32_t day)
Definition calendar.cc:93
uint32_t days_in_year_gregorian(const uint32_t year)
Definition calendar.cc:233
uint32_t year_month_to_months(uint32_t year_month)
Definition calendar.cc:463
static bool is_leap_year(uint32_t y, enum calendar c)
Definition calendar.h:316
uint32_t day_of_week(int64_t day_number, bool sunday_is_first_day_of_week)
Definition calendar.cc:271
uint32_t days_in_year(const uint32_t year, enum calendar calendar)
Definition calendar.cc:210
uint32_t days_in_gregorian_year_month(uint32_t year, uint32_t month)
Definition calendar.cc:309
void gregorian_date_from_absolute_day_number(int64_t absolute_day, uint32_t *year_out, uint32_t *month_out, uint32_t *day_out)
Definition calendar.cc:177
static const uint32_t __leap_days_in_month[12]
Definition calendar.cc:39
bool in_unix_epoch_range(uint32_t year, uint32_t month, uint32_t day, uint32_t hour, uint32_t minute, uint32_t second)
Definition calendar.cc:328
int64_t julian_day_number_to_absolute_day_number(int64_t julian_day)
Definition calendar.cc:123