1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008-2009 Sun Microsystems
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.
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.
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
24
* Defines the API for dealing with temporal data inside the server.
26
* The Temporal class is the base class for all data of any temporal
27
* type. A number of derived classes define specialized classes
28
* representng various date, date-time, time, or timestamp types.
30
* All Temporal derived classes are ValueObjects. That is to say that
31
* Temporal class instances are not part of the Item hierarchy and serve
32
* <em>only</em> to represent a time or date-related piece of data.
36
* Low-level calendrical calculations are done via routines in the
39
* @see drizzled/calendar.cc
42
#ifndef DRIZZLED_TEMPORAL_H
43
#define DRIZZLED_TEMPORAL_H
45
#define DRIZZLE_MAX_SECONDS 59
46
#define DRIZZLE_MAX_MINUTES 59
47
#define DRIZZLE_MAX_HOURS 23
48
#define DRIZZLE_MAX_DAYS 31
49
#define DRIZZLE_MAX_MONTHS 12
50
#define DRIZZLE_MAX_YEARS_SQL 9999
51
#define DRIZZLE_MAX_YEARS_EPOCH 2038
52
#define DRIZZLE_MIN_SECONDS 0
53
#define DRIZZLE_MIN_MINUTES 0
54
#define DRIZZLE_MIN_HOURS 0
55
#define DRIZZLE_MIN_DAYS 1
56
#define DRIZZLE_MIN_MONTHS 1
57
#define DRIZZLE_MIN_YEARS_SQL 1
58
#define DRIZZLE_MIN_YEARS_EPOCH 1970
60
#define DRIZZLE_SECONDS_IN_MINUTE 60
61
#define DRIZZLE_SECONDS_IN_HOUR (60*60)
62
#define DRIZZLE_SECONDS_IN_DAY (60*60*24)
63
#define DRIZZLE_NANOSECONDS_IN_MICROSECOND 1000
65
#define DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING 40
67
#define DRIZZLE_YY_PART_YEAR 70
69
#include "drizzled/calendar.h"
74
/* Outside forward declarations */
80
/* Forward declaration needed */
81
class TemporalInterval;
82
class TemporalIntervalYear;
83
class TemporalIntervalDayOrLess;
84
class TemporalIntervalDayOrWeek;
85
class TemporalIntervalYearMonth;
88
* Base class for all temporal data classes.
93
enum calendar _calendar;
100
time_t _epoch_seconds;
103
/** Set on some operator overloads. Indicates that an overflow occurred. */
105
/** Returns number of seconds in time components (hour + minute + second) */
106
uint64_t _cumulative_seconds_in_time() const;
107
/** Resets all temporal components to zero */
110
_years= _months= _days= _hours= _minutes=
111
_seconds= _epoch_seconds= _useconds= _nseconds= 0;
116
virtual ~Temporal() {}
118
/** Returns the calendar component. */
119
inline enum calendar calendar() const {return _calendar;}
120
/** Sets the nseconds component. */
121
inline void set_nseconds(const uint32_t nsecond) {_nseconds= nsecond;}
122
/** Returns the nanoseconds component. */
123
inline uint32_t nseconds() const {return _nseconds;}
124
/** Sets the useconds component. */
125
inline void set_useconds(const uint32_t usecond) {_useconds= usecond;}
126
/** Returns the microsseconds component. */
127
inline uint32_t useconds() const {return _useconds;}
129
* Sets the epoch_seconds component automatically,
130
* based on the temporal's components.
132
void set_epoch_seconds();
133
/** Sets the epch_seconds component manually. */
134
inline void set_epoch_seconds(const uint32_t epoch_second)
135
{_epoch_seconds= epoch_second;}
136
/** Returns the UNIX epoch seconds component. */
137
inline time_t epoch_seconds() const {return _epoch_seconds;}
138
/** Sets the seconds component. */
139
inline void set_seconds(const uint32_t second) {_seconds= second;}
140
/** Returns the seconds component. */
141
inline uint32_t seconds() const {return _seconds;}
142
/** Sets the days component. */
143
inline void set_minutes(const uint32_t minute) {_minutes= minute;}
144
/** Returns the minutes component. */
145
inline uint32_t minutes() const {return _minutes;}
146
/** Sets the hours component. */
147
inline void set_hours(const uint32_t hour) {_hours= hour;}
148
/** Returns the hours component. */
149
inline uint32_t hours() const {return _hours;}
150
/** Sets the days component. */
151
inline void set_days(const uint32_t day) {_days= day;}
152
/** Returns the days component. */
153
inline uint32_t days() const {return _days;}
154
/** Sets the months component. */
155
inline void set_months(const uint32_t month) {_months= month;}
156
/** Returns the months component. */
157
inline uint32_t months() const {return _months;}
158
/** Sets the years component. */
159
inline void set_years(const uint32_t year) {_years= year;}
160
/** Returns the years component. */
161
inline uint32_t years() const {return _years;}
162
/** Returns whether the overflow flag was set
163
* (which can occur during an overloaded operator's execution) */
164
inline bool overflow() const {return _overflow;}
166
/** Returns whether the temporal value is valid as a date. */
167
virtual bool is_valid_date() const= 0;
168
/** Returns whether the temporal value is valid as a datetime. */
169
virtual bool is_valid_datetime() const= 0;
170
/** Returns whether the temporal value is valid as a time. */
171
virtual bool is_valid_time() const= 0;
172
/** Returns whether the temporal value is valid as a UNIX timestamp. */
173
virtual bool is_valid_timestamp() const= 0;
176
* Returns whether the temporal
177
* value is valid. Each subclass defines what is
178
* valid for the range of temporal data it contains.
180
virtual bool is_valid() const= 0;
183
* All Temporal derived classes must implement
184
* conversion routines for converting to and from
185
* a string. Subclasses implement other conversion
186
* routines, but should always follow these notes:
188
* 1) Ensure that ALL from_xxx methods call is_valid()
189
* 2) Ensure that ALL to_xxx methods are void returns and
190
* do not call is_valid()
192
* This minimizes the repeated bounds-checking to
193
* just the conversion from_xxx routines.
195
friend class TemporalFormat;
198
/* Forward declaration needed */
204
* Class representing temporal components in a valid
205
* SQL date range, with no time component
207
class Date: public Temporal
210
Date() :Temporal() {}
212
* Comparison operator overloads to compare a Date against
213
* another Date value.
215
* @param Date to compare against.
217
virtual bool operator==(const Date &rhs);
218
virtual bool operator!=(const Date &rhs);
219
virtual bool operator>(const Date &rhs);
220
virtual bool operator>=(const Date &rhs);
221
virtual bool operator<(const Date &rhs);
222
virtual bool operator<=(const Date &rhs);
225
* Comparison operator overloads to compare a Date against
228
* @param DateTime to compare against.
230
virtual bool operator==(const DateTime &rhs);
231
virtual bool operator!=(const DateTime &rhs);
232
virtual bool operator>(const DateTime &rhs);
233
virtual bool operator>=(const DateTime &rhs);
234
virtual bool operator<(const DateTime &rhs);
235
virtual bool operator<=(const DateTime &rhs);
238
* Comparison operator overloads to compare this against
241
* @param Timestamp to compare against.
243
virtual bool operator==(const Timestamp &rhs);
244
virtual bool operator!=(const Timestamp &rhs);
245
virtual bool operator>(const Timestamp &rhs);
246
virtual bool operator>=(const Timestamp &rhs);
247
virtual bool operator<(const Timestamp &rhs);
248
virtual bool operator<=(const Timestamp &rhs);
251
* Operator overload for adding/subtracting another Date
252
* (or subclass) to/from this temporal. When subtracting
253
* or adding two Dates, we return a new Date instance.
255
* @param Date instance to add/subtract to/from
257
const Date operator-(const Date &rhs);
258
const Date operator+(const Date &rhs);
259
Date& operator+=(const Date &rhs);
260
Date& operator-=(const Date &rhs);
263
* Operator to add/subtract a Time from a Time.
264
* We can return a Time new temporal instance.
266
* @param Temporal instance to add/subtract to/from
268
const Date operator-(const Time &rhs);
269
const Date operator+(const Time &rhs);
270
Date& operator-=(const Time &rhs);
271
Date& operator+=(const Time &rhs);
275
* Operator overload for adding/subtracting a DateTime
276
* (or subclass) to/from this temporal. When subtracting
277
* or adding two Dates, we return a new Date instance.
279
* @param DateTime instance to add/subtract to/from
281
const Date operator-(const DateTime &rhs);
282
const Date operator+(const DateTime &rhs);
283
Date& operator+=(const DateTime &rhs);
284
Date& operator-=(const DateTime &rhs);
288
* Operator overload for when a DateTime instance is
289
* assigned to a Date. We do a copy of the DateTime's
290
* date-related components.
292
* @param The DateTime to copy from
294
Date& operator=(const DateTime &rhs);
296
virtual bool is_valid_date() const {return is_valid();}
297
virtual bool is_valid_datetime() const {return is_valid();}
298
virtual bool is_valid_time() const {return false;}
299
virtual bool is_valid_timestamp() const
301
return is_valid() && in_unix_epoch();
304
/** Returns whether the temporal value is valid date. */
305
virtual bool is_valid() const;
306
/* Returns whether the Date (or subclass) instance is in the Unix Epoch. */
307
virtual bool in_unix_epoch() const;
310
* Fills a supplied char string with a
311
* string representation of the Date
314
* @param C-String to fill.
315
* @param Length of to C-String
316
* @returns length of string written (including trailing '\0').
317
* If output was truncated, returns length that would have
320
virtual int to_string(char *to, size_t to_len) const;
323
* Maximum length of C-String needed to represent type
326
static const int MAX_STRING_LENGTH= 11;
329
* Attempts to populate the Date instance based
330
* on the contents of a supplied string.
332
* Returns whether the conversion was
335
* @param String to convert from
336
* @param Length of supplied string (not including trailing '\0').
338
virtual bool from_string(const char *from, size_t from_len);
341
* Fills a supplied 8-byte integer pointer with an
342
* integer representation of the Date
345
* @param Integer to fill.
347
virtual void to_int64_t(int64_t *to) const;
350
* Fills a supplied 4-byte integer pointer with an
351
* integer representation of the Date
354
* @param Integer to fill.
356
virtual void to_int32_t(int32_t *to) const;
359
* Attempts to populate the Date instance based
360
* on the contents of a supplied 4-byte integer.
362
* Returns whether the conversion was
365
* @param Integer to convert from
367
virtual bool from_int32_t(const int32_t from);
370
* Fills a supplied int64_t with the Julian Day Number
371
* representation of this Date.
373
* @note Julian Day Number != julian day!
375
* Julian Day Number is the monotonically increasing number
376
* of days from the start of the Julian calendar (~4713 B.C.)
378
* julian day is the ordinal day number of a day in a year.
380
* @param int64_t to fill
382
void to_julian_day_number(int64_t *to) const;
385
* Attempts to populate the Date instance based
386
* on the contents of a supplied Julian Day Number
388
* Returns whether the conversion was
391
* @param Integer to convert from
393
bool from_julian_day_number(const int64_t from);
396
* Fills a supplied tm pointer with an
397
* representation of the Date
402
virtual void to_tm(struct tm *to) const;
405
* Attempts to populate the Date instance based
406
* on the contents of a supplied pointer to struct tm
409
* Returns whether the conversion was
412
* @param Pointe rto the struct tm to convert from
414
virtual bool from_tm(const struct tm *from);
417
* Attempts to convert the Date value into
420
* @param Pointer to a time_t to convert to
422
virtual void to_time_t(time_t *to) const;
425
* Attempts to populate the Date instance based
426
* on the contents of a supplied time_t
428
* Returns whether the conversion was
431
* @param time_t to convert from
433
virtual bool from_time_t(const time_t from);
436
* Fills a supplied my_decimal with a representation of
439
* @param Pointer to the my_decimal to fill
441
virtual void to_decimal(my_decimal *to) const;
443
friend class TemporalInterval;
444
friend class Timestamp;
447
/* Forward declare needed for friendship */
451
* Class representing temporal components having only
452
* a time component, with no date structure
454
class Time: public Temporal
457
Time() :Temporal() {}
458
/* Maximum number of seconds in 23:59:59 (24 * 60 * 60) */
459
static const uint32_t MAX_CUMULATIVE_SECONDS= 86400L;
462
* Comparison operator overloads to compare a Time against
463
* another Time value.
465
* @param Time to compare against.
467
bool operator==(const Time &rhs);
468
bool operator!=(const Time &rhs);
469
bool operator>(const Time &rhs);
470
bool operator>=(const Time &rhs);
471
bool operator<(const Time &rhs);
472
bool operator<=(const Time &rhs);
474
* Operator to add/subtract a Time from a Time.
475
* We can return a Time new temporal instance.
477
* @param Temporal instance to add/subtract to/from
479
const Time operator-(const Time &rhs);
480
const Time operator+(const Time &rhs);
481
Time& operator-=(const Time &rhs);
482
Time& operator+=(const Time &rhs);
484
bool is_valid_date() const {return false;}
485
bool is_valid_datetime() const {return false;}
486
bool is_valid_time() const {return is_valid();}
487
bool is_valid_timestamp() const {return false;}
488
/** Returns whether the temporal value is valid date. */
489
bool is_valid() const;
492
* Fills a supplied char string with a
493
* string representation of the Time
496
* @param C-String to fill
497
* @param Length of to C-String
498
* @returns length of string written (not including trailing '\0').
499
* If output was truncated, returns length that would have
502
int to_string(char *to, size_t to_len) const;
505
* Maximum length of C-String needed to represent type
508
static const int MAX_STRING_LENGTH= 9;
512
* Attempts to populate the Time instance based
513
* on the contents of a supplied string.
515
* Returns whether the conversion was
518
* @param String to convert from
519
* @param Length of supplied string
521
bool from_string(const char *from, size_t from_len);
524
* Fills a supplied 4-byte integer pointer with an
525
* integer representation of the Time
528
* @param Integer to fill.
530
void to_int32_t(int32_t *to) const;
533
* Attempts to populate the Time instance based
534
* on the contents of a supplied 4-byte integer.
536
* Returns whether the conversion was
539
* @param Integer to convert from
541
bool from_int32_t(const int32_t from);
544
* Attempts to populate the Time instance based
545
* on the contents of a supplied time_t
547
* Returns whether the conversion was
552
* We can only convert *from* a time_t, not back
553
* to a time_t since it would be a lossy conversion.
555
* @param time_t to convert from
557
bool from_time_t(const time_t from);
560
* Fills a supplied my_decimal with a representation of
563
* @param Pointer to the my_decimal to fill
565
void to_decimal(my_decimal *to) const;
568
friend class DateTime;
572
* Class representing temporal components in a valid
573
* SQL datetime range, including a time component
575
class DateTime: public Date
578
DateTime() :Date() {}
580
friend class TemporalInterval;
582
/** Returns whether the DateTime (or subclass) instance
583
* is in the Unix Epoch.
585
bool in_unix_epoch() const;
586
/** Returns whether the temporal value is valid datetime. */
587
virtual bool is_valid() const;
590
* It's not possible to convert to and from a DateTime and
591
* a 4-byte integer, so let us know if we try and do it!
593
void to_int32_t(int32_t *) const {assert(0);}
594
bool from_int32_t(int32_t) {assert(0); return false;}
597
* Fills a supplied char string with a
598
* string representation of the DateTime
601
* @param C-String to fill
602
* @param Length of to C-String
603
* @returns length of string written (not including trailing '\0').
604
* If output was truncated, returns length that would have
607
virtual int to_string(char *to, size_t to_len) const;
610
* Maximum length of C-String needed to represent type
613
static const int MAX_STRING_LENGTH= 27;
616
* Attempts to populate the DateTime instance based
617
* on the contents of a supplied string.
619
* Returns whether the conversion was
622
* @param String to convert from
623
* @param Length of supplied string
625
bool from_string(const char *from, size_t from_len);
628
* Fills a supplied 8-byte integer pointer with an
629
* integer representation of the DateTime
632
* @param Integer to fill.
634
void to_int64_t(int64_t *to) const;
637
* Attempts to populate the DateTime instance based
638
* on the contents of a supplied time_t
640
* Returns whether the conversion was
643
* @param time_t to convert from
645
bool from_time_t(const time_t from);
648
* Attempts to populate the DateTime instance based
649
* on the contents of a supplied 8-byte integer.
651
* Returns whether the conversion was
654
* @param Integer to convert from
655
* @param convert if conversion to canonical representation
656
* should be attempted
658
bool from_int64_t(const int64_t from, bool convert);
660
bool from_int64_t(const int64_t from) {
661
return from_int64_t(from, true);
665
* Fills a supplied tm pointer with an
666
* representation of the DateTime
671
void to_tm(struct tm *to) const;
674
* Fills a supplied my_decimal with a representation of
675
* the DateTime value.
677
* @param Pointer to the my_decimal to fill
679
void to_decimal(my_decimal *to) const;
681
friend class Timestamp;
685
* Class representing temporal components in the UNIX epoch
687
class Timestamp: public DateTime
690
Timestamp() :DateTime() {}
693
* Comparison operator overloads to compare this against
696
* @param Timestamp to compare against.
698
bool operator==(const Date &rhs);
699
bool operator!=(const Date &rhs);
700
bool operator>(const Date &rhs);
701
bool operator>=(const Date &rhs);
702
bool operator<(const Date &rhs);
703
bool operator<=(const Date &rhs);
706
* Comparison operator overloads to compare this against
709
* @param DateTime to compare against.
711
bool operator==(const DateTime &rhs);
712
bool operator!=(const DateTime &rhs);
713
bool operator>(const DateTime &rhs);
714
bool operator>=(const DateTime &rhs);
715
bool operator<(const DateTime &rhs);
716
bool operator<=(const DateTime &rhs);
719
* Comparison operator overloads to compare this against
720
* another Timestamp value.
722
* @param Timestamp to compare against.
724
bool operator==(const Timestamp &rhs);
725
bool operator!=(const Timestamp &rhs);
726
bool operator>(const Timestamp &rhs);
727
bool operator>=(const Timestamp &rhs);
728
bool operator<(const Timestamp &rhs);
729
bool operator<=(const Timestamp &rhs);
731
bool is_valid_timestamp() const {return is_valid();}
732
/** Returns whether the temporal value is valid timestamp. */
733
virtual bool is_valid() const;
736
* Attempts to convert the Timestamp value into
739
* @param Pointer to a time_t to convert to
741
void to_time_t(time_t *to) const;
745
* Operator overload to an output stream for a Timestamp.
747
std::ostream& operator<<(std::ostream& os, const Timestamp& subject);
750
* Class representing temporal components in the UNIX epoch
751
* with an additional microsecond component.
753
class MicroTimestamp: public Timestamp
756
MicroTimestamp() :Timestamp() {}
757
/** Returns whether the temporal value is valid micro-timestamp. */
758
bool is_valid() const;
761
* Fills a supplied char string with a
762
* string representation of the MicroTimestamp
765
* @param C-String to fill
766
* @param Length of to C-String
767
* @returns length of string written (not including trailing '\0').
768
* If output was truncated, returns length that would have
771
int to_string(char *to, size_t to_len) const;
774
* Maximum length of C-String needed to represent type
777
static const int MAX_STRING_LENGTH= 27;
780
* Fills a supplied timeval pointer with an
781
* representation of the MicroTimestamp
784
* Returns whether the conversion was
787
* @param timeval to fill.
789
void to_timeval(struct timeval *to) const;
793
* Class representing temporal components in the UNIX epoch
794
* with an additional nanosecond component.
796
class NanoTimestamp: public Timestamp
799
NanoTimestamp() :Timestamp() {}
800
/** Returns whether the temporal value is valid nano-timestamp. */
801
bool is_valid() const;
804
* Fills a supplied timespec pointer with an
805
* representation of the NanoTimestamp
808
* Returns whether the conversion was
811
* @param timespec to fill.
813
void to_timespec(struct timespec *to) const;
816
} /* end namespace drizzled */
818
#endif /* DRIZZLED_TEMPORAL_H */