1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
8
* Jay Pipes <jay.pipes@sun.com>
10
* This program is free software; you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation; either version 2 of the License, or
13
* (at your option) any later version.
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
20
* You should have received a copy of the GNU General Public License
21
* along with this program; if not, write to the Free Software
22
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28
* Defines the API for dealing with temporal data inside the server.
30
* The Temporal class is the base class for all data of any temporal
31
* type. A number of derived classes define specialized classes
32
* representng various date, date-time, time, or timestamp types.
34
* All Temporal derived classes are ValueObjects. That is to say that
35
* Temporal class instances are not part of the Item hierarchy and serve
36
* <em>only</em> to represent a time or date-related piece of data.
40
* Low-level calendrical calculations are done via routines in the
43
* @see drizzled/calendar.cc
46
#ifndef DRIZZLED_TEMPORAL_H
47
#define DRIZZLED_TEMPORAL_H
49
#define DRIZZLE_MAX_SECONDS 59
50
#define DRIZZLE_MAX_MINUTES 59
51
#define DRIZZLE_MAX_HOURS 23
52
#define DRIZZLE_MAX_DAYS 31
53
#define DRIZZLE_MAX_MONTHS 12
54
#define DRIZZLE_MAX_YEARS_SQL 9999
55
#define DRIZZLE_MAX_YEARS_EPOCH 2038
56
#define DRIZZLE_MIN_SECONDS 0
57
#define DRIZZLE_MIN_MINUTES 0
58
#define DRIZZLE_MIN_HOURS 0
59
#define DRIZZLE_MIN_DAYS 1
60
#define DRIZZLE_MIN_MONTHS 1
61
#define DRIZZLE_MIN_YEARS_SQL 1
62
#define DRIZZLE_MIN_YEARS_EPOCH 1970
64
#define DRIZZLE_SECONDS_IN_MINUTE 60
65
#define DRIZZLE_SECONDS_IN_HOUR (60*60)
66
#define DRIZZLE_SECONDS_IN_DAY (60*60*24)
67
#define DRIZZLE_NANOSECONDS_IN_MICROSECOND 1000
69
#define DRIZZLE_YY_PART_YEAR 70
74
#include "drizzled/global.h"
75
#include "drizzled/calendar.h"
77
/* Outside forward declarations */
84
* Base class for all temporal data classes.
89
enum calendar _calendar;
96
time_t _epoch_seconds;
99
/** Returns number of seconds in time components (hour + minute + second) */
100
uint64_t _cumulative_seconds_in_time() const;
103
virtual ~Temporal() {}
105
/** Returns the calendar component. */
106
inline enum calendar calendar() const {return _calendar;}
107
/** Returns the nanoseconds component. */
108
inline uint32_t nseconds() const {return _nseconds;}
109
/** Sets the useconds component. */
110
inline void set_useconds(const uint32_t usecond) {_useconds= usecond;}
111
/** Returns the microsseconds component. */
112
inline uint32_t useconds() const {return _useconds;}
113
/** Sets the epoch_seconds component. */
114
virtual void set_epoch_seconds();
115
/** Returns the UNIX epoch seconds component. */
116
inline time_t epoch_seconds() const {return _epoch_seconds;}
117
/** Sets the seconds component. */
118
inline void set_seconds(const uint32_t second) {_seconds= second;}
119
/** Returns the seconds component. */
120
inline uint32_t seconds() const {return _seconds;}
121
/** Sets the days component. */
122
inline void set_minutes(const uint32_t minute) {_minutes= minute;}
123
/** Returns the minutes component. */
124
inline uint32_t minutes() const {return _minutes;}
125
/** Sets the hours component. */
126
inline void set_hours(const uint32_t hour) {_hours= hour;}
127
/** Returns the hours component. */
128
inline uint32_t hours() const {return _hours;}
129
/** Sets the days component. */
130
inline void set_days(const uint32_t day) {_days= day;}
131
/** Returns the days component. */
132
inline uint32_t days() const {return _days;}
133
/** Sets the months component. */
134
inline void set_months(const uint32_t month) {_months= month;}
135
/** Returns the months component. */
136
inline uint32_t months() const {return _months;}
137
/** Sets the years component. */
138
inline void set_years(const uint32_t year) {_years= year;}
139
/** Returns the years component. */
140
inline uint32_t years() const {return _years;}
142
/** Returns whether the temporal value is valid as a date. */
143
virtual bool is_valid_date() const= 0;
144
/** Returns whether the temporal value is valid as a datetime. */
145
virtual bool is_valid_datetime() const= 0;
146
/** Returns whether the temporal value is valid as a time. */
147
virtual bool is_valid_time() const= 0;
148
/** Returns whether the temporal value is valid as a UNIX timestamp. */
149
virtual bool is_valid_timestamp() const= 0;
152
* Returns whether the temporal
153
* value is valid. Each subclass defines what is
154
* valid for the range of temporal data it contains.
156
virtual bool is_valid() const= 0;
159
* All Temporal derived classes must implement
160
* conversion routines for converting to and from
161
* a string. Subclasses implement other conversion
162
* routines, but should always follow these notes:
164
* 1) Ensure that ALL from_xxx methods call is_valid()
165
* 2) Ensure that ALL to_xxx methods are void returns and
166
* do not call is_valid()
168
* This minimizes the repeated bounds-checking to
169
* just the conversion from_xxx routines.
172
friend class TemporalFormat;
176
* Class representing temporal components in a valid
177
* SQL date range, with no time component
179
class Date: public Temporal
183
* Comparison operator overloads to compare a Date against
184
* another Date value.
186
* @param Date to compare against.
188
bool operator==(const Date &rhs);
189
bool operator!=(const Date &rhs);
190
bool operator>(const Date &rhs);
191
bool operator>=(const Date &rhs);
192
bool operator<(const Date &rhs);
193
bool operator<=(const Date &rhs);
195
* Operator overload for adding/subtracting another Date
196
* (or subclass) to/from this temporal. When subtracting
197
* or adding two Dates, we return a new Date instance.
199
* @param Temporal instance to add/subtract to/from
201
const Date operator-(const Date &rhs);
202
const Date operator+(const Date &rhs);
203
Date& operator+=(const Date &rhs);
204
Date& operator-=(const Date &rhs);
206
virtual bool is_valid_date() const {return is_valid();}
207
virtual bool is_valid_datetime() const {return is_valid();}
208
virtual bool is_valid_time() const {return false;}
209
virtual bool is_valid_timestamp() const {return is_valid() && in_unix_epoch();}
210
/** Returns whether the temporal value is valid date. */
211
virtual bool is_valid() const;
212
/* Returns whether the Date (or subclass) instance is in the Unix Epoch. */
213
virtual bool in_unix_epoch() const;
216
* Fills a supplied char string with a
217
* string representation of the Date
220
* @param C-String to fill.
221
* @param Length of filled string (out param)
223
virtual void to_string(char *to, size_t *to_len) const;
226
* Attempts to populate the Date instance based
227
* on the contents of a supplied string.
229
* Returns whether the conversion was
232
* @param String to convert from
233
* @param Length of supplied string
235
virtual bool from_string(const char *from, size_t from_len);
238
* Fills a supplied 8-byte integer pointer with an
239
* integer representation of the Date
242
* @param Integer to fill.
244
virtual void to_int64_t(int64_t *to) const;
247
* Fills a supplied 4-byte integer pointer with an
248
* integer representation of the Date
251
* @param Integer to fill.
253
virtual void to_int32_t(int32_t *to) const;
256
* Attempts to populate the Date instance based
257
* on the contents of a supplied 4-byte integer.
259
* Returns whether the conversion was
262
* @param Integer to convert from
264
virtual bool from_int32_t(const int32_t from);
267
* Attempts to populate the Date instance based
268
* on the contents of a supplied Julian Day Number
270
* Returns whether the conversion was
273
* @param Integer to convert from
275
virtual bool from_julian_day_number(const int64_t from);
278
* Fills a supplied tm pointer with an
279
* representation of the Date
284
virtual void to_tm(struct tm *to) const;
287
* Attempts to populate the Date instance based
288
* on the contents of a supplied pointer to struct tm
291
* Returns whether the conversion was
294
* @param Pointe rto the struct tm to convert from
296
virtual bool from_tm(const struct tm *from);
299
* Attempts to convert the Date value into
302
* @param Pointer to a time_t to convert to
304
virtual void to_time_t(time_t *to) const;
307
* Attempts to populate the Date instance based
308
* on the contents of a supplied time_t
310
* Returns whether the conversion was
313
* @param time_t to convert from
315
virtual bool from_time_t(const time_t from);
318
* Fills a supplied my_decimal with a representation of
321
* @param Pointer to the my_decimal to fill
323
virtual void to_decimal(my_decimal *to) const;
326
/* Forward declare needed for friendship */
330
* Class representing temporal components having only
331
* a time component, with no date structure
333
class Time: public Temporal
338
* Comparison operator overloads to compare a Time against
339
* another Time value.
341
* @param Time to compare against.
343
bool operator==(const Time &rhs);
344
bool operator!=(const Time &rhs);
345
bool operator>(const Time &rhs);
346
bool operator>=(const Time &rhs);
347
bool operator<(const Time &rhs);
348
bool operator<=(const Time &rhs);
350
* Operator to add/subtract a Time from a Time.
351
* We can return a Time new temporal instance.
353
* @param Temporal instance to add/subtract to/from
355
const Time operator-(const Time &rhs);
356
const Time operator+(const Time &rhs);
357
Time& operator-=(const Time &rhs);
358
Time& operator+=(const Time &rhs);
360
virtual bool is_valid_date() const {return false;}
361
virtual bool is_valid_datetime() const {return false;}
362
virtual bool is_valid_time() const {return is_valid();}
363
virtual bool is_valid_timestamp() const {return false;}
364
/** Returns whether the temporal value is valid date. */
365
virtual bool is_valid() const;
368
* Fills a supplied char string with a
369
* string representation of the Time
372
* @param C-String to fill.
373
* @param Length of filled string (out param)
375
virtual void to_string(char *to, size_t *to_len) const;
378
* Attempts to populate the Time instance based
379
* on the contents of a supplied string.
381
* Returns whether the conversion was
384
* @param String to convert from
385
* @param Length of supplied string
387
virtual bool from_string(const char *from, size_t from_len);
390
* Fills a supplied 4-byte integer pointer with an
391
* integer representation of the Time
394
* @param Integer to fill.
396
virtual void to_int32_t(int32_t *to) const;
399
* Attempts to populate the Time instance based
400
* on the contents of a supplied 4-byte integer.
402
* Returns whether the conversion was
405
* @param Integer to convert from
407
virtual bool from_int32_t(const int32_t from);
410
* Fills a supplied my_decimal with a representation of
413
* @param Pointer to the my_decimal to fill
415
virtual void to_decimal(my_decimal *to) const;
417
friend class DateTime;
421
* Class representing temporal components in a valid
422
* SQL datetime range, including a time component
424
class DateTime: public Date
428
* Comparison operator overloads to compare a DateTime against
429
* another DateTime value.
431
* @param DateTime to compare against.
433
bool operator==(const DateTime &rhs);
434
bool operator!=(const DateTime &rhs);
435
bool operator>(const DateTime &rhs);
436
bool operator>=(const DateTime &rhs);
437
bool operator<(const DateTime &rhs);
438
bool operator<=(const DateTime &rhs);
440
* Operator to add/subtract a Time from a Time.
441
* We can return a Time new temporal instance.
443
* @param Temporal instance to add/subtract to/from
445
const DateTime operator-(const Time &rhs);
446
const DateTime operator+(const Time &rhs);
447
DateTime& operator-=(const Time &rhs);
448
DateTime& operator+=(const Time &rhs);
450
* Operator overload for adding/subtracting another DateTime
451
* (or subclass) to/from this temporal. When subtracting
452
* or adding two DateTimes, we return a new DateTime instance.
454
* @param Temporal instance to add/subtract to/from
456
const DateTime operator-(const DateTime &rhs);
457
const DateTime operator+(const DateTime &rhs);
458
DateTime& operator+=(const DateTime &rhs);
459
DateTime& operator-=(const DateTime &rhs);
461
/* Returns whether the DateTime (or subclass) instance is in the Unix Epoch. */
462
bool in_unix_epoch() const;
463
/** Returns whether the temporal value is valid datetime. */
464
virtual bool is_valid() const;
467
* It's not possible to convert to and from a DateTime and
468
* a 4-byte integer, so let us know if we try and do it!
470
inline void to_int32_t(int32_t *) {assert(0);}
471
inline bool from_int32_t(int32_t) {assert(0); return false;}
474
* Fills a supplied char string with a
475
* string representation of the DateTime
478
* @param C-String to fill.
479
* @param Length of filled string (out param)
481
virtual void to_string(char *to, size_t *to_len) const;
484
* Attempts to populate the DateTime instance based
485
* on the contents of a supplied string.
487
* Returns whether the conversion was
490
* @param String to convert from
491
* @param Length of supplied string
493
virtual bool from_string(const char *from, size_t from_len);
496
* Fills a supplied 8-byte integer pointer with an
497
* integer representation of the DateTime
500
* @param Integer to fill.
502
virtual void to_int64_t(int64_t *to) const;
505
* Attempts to populate the DateTime instance based
506
* on the contents of a supplied time_t
508
* Returns whether the conversion was
511
* @param time_t to convert from
513
virtual bool from_time_t(const time_t from);
516
* Attempts to populate the DateTime instance based
517
* on the contents of a supplied 8-byte integer.
519
* Returns whether the conversion was
522
* @param Integer to convert from
524
virtual bool from_int64_t(const int64_t from);
527
* Fills a supplied tm pointer with an
528
* representation of the DateTime
533
virtual void to_tm(struct tm *to) const;
536
* Fills a supplied my_decimal with a representation of
537
* the DateTime value.
539
* @param Pointer to the my_decimal to fill
541
virtual void to_decimal(my_decimal *to) const;
545
* Class representing temporal components in the UNIX epoch
547
class Timestamp: public DateTime
551
* Comparison operator overloads to compare a Timestamp against
552
* another Timestamp value.
554
* @param Timestamp to compare against.
556
bool operator==(const Timestamp &rhs);
557
bool operator!=(const Timestamp &rhs);
558
bool operator>(const Timestamp &rhs);
559
bool operator>=(const Timestamp &rhs);
560
bool operator<(const Timestamp &rhs);
561
bool operator<=(const Timestamp &rhs);
563
inline bool is_valid_timestamp() const {return is_valid();}
564
/** Returns whether the temporal value is valid timestamp. */
565
virtual bool is_valid() const;
568
* Attempts to convert the Timestamp value into
571
* @param Pointer to a time_t to convert to
573
virtual void to_time_t(time_t *to) const;
577
* Class representing temporal components in the UNIX epoch
578
* with an additional microsecond component.
580
class MicroTimestamp: public Timestamp
583
/** Returns whether the temporal value is valid micro-timestamp. */
584
bool is_valid() const;
587
* Fills a supplied char string with a
588
* string representation of the MicroTimestamp
591
* @param C-String to fill.
592
* @param Length of filled string (out param)
594
virtual void to_string(char *to, size_t *to_len) const;
597
* Fills a supplied timeval pointer with an
598
* representation of the MicroTimestamp
601
* Returns whether the conversion was
604
* @param timeval to fill.
606
virtual void to_timeval(struct timeval *to) const;
610
* Class representing temporal components in the UNIX epoch
611
* with an additional nanosecond component.
613
class NanoTimestamp: public Timestamp
616
/** Returns whether the temporal value is valid nano-timestamp. */
617
bool is_valid() const;
620
* Fills a supplied timespec pointer with an
621
* representation of the NanoTimestamp
624
* Returns whether the conversion was
627
* @param timespec to fill.
629
void to_timespec(struct timespec *to) const;
632
} /* end namespace drizzled */
634
#endif /* DRIZZLED_TEMPORAL_H */