~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/calendar.h

  • Committer: Lee
  • Date: 2008-10-30 22:02:01 UTC
  • mto: (572.1.2 devel)
  • mto: This revision was merged to the branch mainline in revision 573.
  • Revision ID: lbieber@lbieber-desktop-20081030220201-elb6qprbzpn7c5a4
add my name to the AUTHORS file

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
21
 
/**
22
 
 * @file 
23
 
 *
24
 
 * Structures and functions for:
25
 
 *
26
 
 * Calculating day number in Gregorian and Julian proleptic calendars.
27
 
 * Converting between day numbers and dates in the calendars.
28
 
 * Converting between different calendars.
29
 
 * Calculating differences between dates.
30
 
 *
31
 
 * Works used in research:
32
 
 *
33
 
 * @cite "Calendrical Calculations", Dershowitz and Reingold
34
 
 * @cite ISO 8601 http://en.wikipedia.org/wiki/ISO_8601
35
 
 * @cite http://www.ddj.com/hpc-high-performance-computing/197006254 
36
 
 * @cite http://en.wikipedia.org/wiki/Julian_day#Calculation
37
 
 */
38
 
 
39
 
#ifndef DRIZZLED_CALENDAR_H
40
 
#define DRIZZLED_CALENDAR_H
41
 
 
42
 
#define JULIAN_DAY_NUMBER_AT_ABSOLUTE_DAY_ONE INT64_C(1721425)
43
 
 
44
 
#define DAYS_IN_NORMAL_YEAR INT32_C(365)
45
 
#define DAYS_IN_LEAP_YEAR INT32_C(366)
46
 
 
47
 
#define GREGORIAN_START_YEAR 1582
48
 
#define GREGORIAN_START_MONTH 10
49
 
#define GREGORIAN_START_DAY 15
50
 
 
51
 
#define UNIX_EPOCH_MIN_YEARS 1970
52
 
#define UNIX_EPOCH_MAX_YEARS 2038
53
 
 
54
 
#define CALENDAR_YY_PART_YEAR 70
55
 
 
56
 
/**
57
 
 * The following constants define the system of calculating the number
58
 
 * of days in various periods of time in the Gregorian calendar.
59
 
 *
60
 
 * Leap years (years containing 366 days) occur:
61
 
 *
62
 
 * - When the year is evenly divisible by 4
63
 
 * - If the year is evenly divisible by 100, it must also
64
 
 *   be evenly divisible by 400.
65
 
 */
66
 
#define GREGORIAN_DAYS_IN_400_YEARS UINT32_C(146097)
67
 
#define GREGORIAN_DAYS_IN_100_YEARS UINT32_C(36524)
68
 
#define GREGORIAN_DAYS_IN_4_YEARS   UINT32_C(1461)
69
 
 
70
 
namespace drizzled
71
 
{
72
 
 
73
 
/**
74
 
 * Different calendars supported by the temporal library
75
 
 */
76
 
enum calendar
77
 
{
78
 
  GREGORIAN= 1, 
79
 
  JULIAN= 2, 
80
 
  HEBREW= 3, 
81
 
  ISLAM= 4
82
 
};
83
 
 
84
 
 
85
 
/**
86
 
 * Calculates the Julian Day Number from the year, month 
87
 
 * and day supplied for a Gregorian Proleptic calendar date.
88
 
 *
89
 
 * @note
90
 
 *
91
 
 * Year month and day values are assumed to be valid.  This 
92
 
 * method does no bounds checking or validation.
93
 
 *
94
 
 * @param Year of date
95
 
 * @param Month of date
96
 
 * @param Day of date
97
 
 */
98
 
int64_t julian_day_number_from_gregorian_date(uint32_t year, uint32_t month, uint32_t day);
99
 
 
100
 
/**
101
 
 * Translates an absolute day number to a 
102
 
 * Julian day number.
103
 
 *
104
 
 * @param The absolute day number
105
 
 */
106
 
int64_t absolute_day_number_to_julian_day_number(int64_t absolute_day);
107
 
 
108
 
/**
109
 
 * Translates a Julian day number to an 
110
 
 * absolute day number.  
111
 
 *
112
 
 * @param The Julian day number
113
 
 */
114
 
int64_t julian_day_number_to_absolute_day_number(int64_t julian_day);
115
 
 
116
 
/**
117
 
 * Given a supplied Julian Day Number, populates a year, month, and day
118
 
 * with the date in the Gregorian Proleptic calendar which corresponds to
119
 
 * the given Julian Day Number.
120
 
 *
121
 
 * @param Julian Day Number
122
 
 * @param Pointer to year to populate
123
 
 * @param Pointer to month to populate
124
 
 * @param Pointer to the day to populate
125
 
 */
126
 
void gregorian_date_from_julian_day_number(int64_t julian_day
127
 
                                         , uint32_t *year_out
128
 
                                         , uint32_t *month_out
129
 
                                         , uint32_t *day_out);
130
 
 
131
 
/**
132
 
 * Given a supplied Absolute Day Number, populates a year, month, and day
133
 
 * with the date in the Gregorian Proleptic calendar which corresponds to
134
 
 * the given Absolute Day Number.
135
 
 *
136
 
 * @param Absolute Day Number
137
 
 * @param Pointer to year to populate
138
 
 * @param Pointer to month to populate
139
 
 * @param Pointer to the day to populate
140
 
 */
141
 
void gregorian_date_from_absolute_day_number(int64_t absolute_day
142
 
                                           , uint32_t *year_out
143
 
                                           , uint32_t *month_out
144
 
                                           , uint32_t *day_out);
145
 
 
146
 
/**
147
 
 * Returns the number of days in a particular year.
148
 
 *
149
 
 * @param year to evaluate
150
 
 * @param calendar to use
151
 
 */
152
 
uint32_t days_in_year(uint32_t year, enum calendar calendar);
153
 
 
154
 
/**
155
 
 * Returns the number of days in a particular Gregorian Proleptic calendar year.
156
 
 *
157
 
 * @param year to evaluate
158
 
 */
159
 
uint32_t days_in_year_gregorian(uint32_t year);
160
 
 
161
 
/**
162
 
 * Returns the number of days in a particular Julian Proleptic calendar year.
163
 
 *
164
 
 * @param year to evaluate
165
 
 */
166
 
uint32_t days_in_year_julian(uint32_t year);
167
 
 
168
 
/**
169
 
 * Returns the number of leap years that have
170
 
 * occurred in the Julian Proleptic calendar
171
 
 * up to the supplied year.
172
 
 *
173
 
 * @param year to evaluate (1 - 9999)
174
 
 */
175
 
int32_t number_of_leap_years_julian(uint32_t year);
176
 
 
177
 
/**
178
 
 * Returns the number of leap years that have
179
 
 * occurred in the Gregorian Proleptic calendar
180
 
 * up to the supplied year.
181
 
 *
182
 
 * @param year to evaluate (1 - 9999)
183
 
 */
184
 
int32_t number_of_leap_years_gregorian(uint32_t year);
185
 
 
186
 
/**
187
 
 * Returns the number of days in a month, given
188
 
 * a year and a month in the Gregorian calendar.
189
 
 *
190
 
 * @param Year in Gregorian Proleptic calendar
191
 
 * @param Month in date
192
 
 */
193
 
uint32_t days_in_gregorian_year_month(uint32_t year, uint32_t month);
194
 
 
195
 
inline static bool num_leap_years(uint32_t y, enum calendar c) 
196
 
{
197
 
  return (c == GREGORIAN                
198
 
          ? number_of_leap_years_gregorian(y) 
199
 
          : number_of_leap_years_julian(y));
200
 
}
201
 
 
202
 
/**
203
 
 * Returns the number of the day in a week.
204
 
 *
205
 
 * @see temporal_to_number_days()
206
 
 *
207
 
 * Return values:
208
 
 *
209
 
 * Day            Day Number  Sunday first day?
210
 
 * -------------- ----------- -----------------
211
 
 * Sunday         0           true
212
 
 * Monday         1           true
213
 
 * Tuesday        2           true
214
 
 * Wednesday      3           true
215
 
 * Thursday       4           true
216
 
 * Friday         5           true
217
 
 * Saturday       6           true
218
 
 * Sunday         6           false
219
 
 * Monday         0           false
220
 
 * Tuesday        1           false
221
 
 * Wednesday      2           false
222
 
 * Thursday       3           false
223
 
 * Friday         4           false
224
 
 * Saturday       5           false
225
 
 *
226
 
 * @param Number of days since start of Gregorian calendar.
227
 
 * @param Consider Sunday the first day of the week?
228
 
 */
229
 
uint32_t day_of_week(int64_t day_number, bool sunday_is_first_day_of_week);
230
 
 
231
 
/**
232
 
 * Given a year, month, and day, returns whether the date is 
233
 
 * valid for the Gregorian proleptic calendar.
234
 
 *
235
 
 * @param The year
236
 
 * @param The month
237
 
 * @param The day
238
 
 */
239
 
bool is_valid_gregorian_date(uint32_t year, uint32_t month, uint32_t day);
240
 
 
241
 
/**
242
 
 * Returns whether the supplied date components are within the 
243
 
 * range of the UNIX epoch.
244
 
 *
245
 
 * Times in the range of 1970-01-01T00:00:00 to 2038-01-19T03:14:07
246
 
 *
247
 
 * @param Year
248
 
 * @param Month
249
 
 * @param Day
250
 
 * @param Hour
251
 
 * @param Minute
252
 
 * @param Second
253
 
 */
254
 
bool in_unix_epoch_range(uint32_t year
255
 
                       , uint32_t month
256
 
                       , uint32_t day
257
 
                       , uint32_t hour
258
 
                       , uint32_t minute
259
 
                       , uint32_t second);
260
 
 
261
 
/**
262
 
 * Returns the number of the week from a supplied year, month, and
263
 
 * date in the Gregorian proleptic calendar.  We use strftime() and
264
 
 * the %U, %W, and %V format specifiers depending on the value
265
 
 * of the sunday_is_first_day_of_week parameter.
266
 
 *
267
 
 * @param Subject year
268
 
 * @param Subject month
269
 
 * @param Subject day
270
 
 * @param Is sunday the first day of the week?
271
 
 * @param Pointer to a uint32_t to hold the resulting year, which 
272
 
 *        may be incremented or decremented depending on flags
273
 
 */
274
 
uint32_t week_number_from_gregorian_date(uint32_t year
275
 
                                       , uint32_t month
276
 
                                       , uint32_t day
277
 
                                       , bool sunday_is_first_day_of_week);
278
 
 
279
 
/**
280
 
 * Returns the ISO week number of a supplied year, month, and
281
 
 * date in the Gregorian proleptic calendar.  We use strftime() and
282
 
 * the %V format specifier to do the calculation, which yields a
283
 
 * correct ISO 8601:1988 week number.
284
 
 *
285
 
 * The final year_out parameter is a pointer to an integer which will
286
 
 * be set to the year in which the week belongs, according to ISO8601:1988, 
287
 
 * which may be different from the Gregorian calendar year.
288
 
 *
289
 
 * @see http://en.wikipedia.org/wiki/ISO_8601
290
 
 *
291
 
 * @param Subject year
292
 
 * @param Subject month
293
 
 * @param Subject day
294
 
 */
295
 
uint32_t iso_week_number_from_gregorian_date(uint32_t year
296
 
                                           , uint32_t month
297
 
                                           , uint32_t day);
298
 
/**
299
 
 * Takes a number in the form [YY]YYMM and converts it into
300
 
 * a number of months.
301
 
 *
302
 
 * @param Period in the form [YY]YYMM
303
 
 */
304
 
uint32_t year_month_to_months(uint32_t year_month);
305
 
 
306
 
/**
307
 
 * Takes a number of months and converts it to
308
 
 * a period in the form YYYYMM.
309
 
 *
310
 
 * @param Number of months
311
 
 */
312
 
uint32_t months_to_year_month(uint32_t months);
313
 
 
314
 
/**
315
 
 * Simple function returning whether the supplied year
316
 
 * is a leap year in the supplied calendar.
317
 
 *
318
 
 * @param Year to evaluate
319
 
 * @param Calendar to use
320
 
 */
321
 
inline static bool is_leap_year(uint32_t y, enum calendar c)
322
 
{
323
 
  return (days_in_year(y, c) == 366);
324
 
}
325
 
 
326
 
/**
327
 
 * Simple function returning whether the supplied year
328
 
 * is a leap year in the Gregorian proleptic calendar.
329
 
 */
330
 
inline static bool is_gregorian_leap_year(uint32_t y)
331
 
{
332
 
  return (days_in_year_gregorian(y) == 366);
333
 
}
334
 
 
335
 
/**
336
 
 * Simple function returning whether the supplied year
337
 
 * is a leap year in the Julian proleptic calendar.
338
 
 */
339
 
inline static bool is_julian_leap_year(uint32_t y) 
340
 
{
341
 
  return (days_in_year_julian(y) == 366);
342
 
}
343
 
 
344
 
} /* namespace drizzled */
345
 
 
346
 
#endif /* DRIZZLED_CALENDAR_H */