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, Inc.
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_SECONDS_WITH_LEAP 61
47
#define DRIZZLE_MAX_MINUTES 59
48
#define DRIZZLE_MAX_HOURS 23
49
#define DRIZZLE_MAX_DAYS 31
50
#define DRIZZLE_MAX_MONTHS 12
51
#define DRIZZLE_MAX_YEARS_SQL 9999
52
#define DRIZZLE_MAX_YEARS_EPOCH 2038
53
#define DRIZZLE_MIN_SECONDS 0
54
#define DRIZZLE_MIN_MINUTES 0
55
#define DRIZZLE_MIN_HOURS 0
56
#define DRIZZLE_MIN_DAYS 1
57
#define DRIZZLE_MIN_MONTHS 1
58
#define DRIZZLE_MIN_YEARS_SQL 1
59
#define DRIZZLE_MIN_YEARS_EPOCH 1970
61
#define DRIZZLE_SECONDS_IN_MINUTE 60
62
#define DRIZZLE_SECONDS_IN_HOUR (60*60)
63
#define DRIZZLE_SECONDS_IN_DAY (60*60*24)
64
#define DRIZZLE_NANOSECONDS_IN_MICROSECOND 1000
66
#define DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING 40
68
#define DRIZZLE_YY_PART_YEAR 70
70
#include <drizzled/calendar.h>
75
/* Outside forward declarations */
83
/* Forward declaration needed */
84
class TemporalInterval;
85
class TemporalIntervalYear;
86
class TemporalIntervalDayOrLess;
87
class TemporalIntervalDayOrWeek;
88
class TemporalIntervalYearMonth;
91
* Base class for all temporal data classes.
96
enum calendar _calendar;
103
time_t _epoch_seconds;
106
/** Set on some operator overloads. Indicates that an overflow occurred. */
108
/** Returns number of seconds in time components (hour + minute + second) */
109
uint64_t _cumulative_seconds_in_time() const;
110
/** Resets all temporal components to zero */
113
_years= _months= _days= _hours= _minutes=
114
_seconds= _epoch_seconds= _useconds= _nseconds= 0;
119
virtual ~Temporal() {}
121
/** Returns the calendar component. */
122
inline enum calendar calendar() const {return _calendar;}
123
/** Sets the nseconds component. */
124
inline void set_nseconds(const uint32_t nsecond) {_nseconds= nsecond;}
125
/** Returns the nanoseconds component. */
126
inline uint32_t nseconds() const {return _nseconds;}
127
/** Sets the useconds component. */
128
inline void set_useconds(const uint32_t usecond) {_useconds= usecond;}
129
/** Returns the microsseconds component. */
130
inline uint32_t useconds() const {return _useconds;}
132
* Sets the epoch_seconds component automatically,
133
* based on the temporal's components.
135
void set_epoch_seconds();
136
/** Sets the epch_seconds component manually. */
137
inline void set_epoch_seconds(const uint32_t epoch_second)
138
{_epoch_seconds= epoch_second;}
139
/** Returns the UNIX epoch seconds component. */
140
inline time_t epoch_seconds() const {return _epoch_seconds;}
141
/** Sets the seconds component. */
142
inline void set_seconds(const uint32_t second) {_seconds= second;}
143
/** Returns the seconds component. */
144
inline uint32_t seconds() const {return _seconds;}
145
/** Sets the days component. */
146
inline void set_minutes(const uint32_t minute) {_minutes= minute;}
147
/** Returns the minutes component. */
148
inline uint32_t minutes() const {return _minutes;}
149
/** Sets the hours component. */
150
inline void set_hours(const uint32_t hour) {_hours= hour;}
151
/** Returns the hours component. */
152
inline uint32_t hours() const {return _hours;}
153
/** Sets the days component. */
154
inline void set_days(const uint32_t day) {_days= day;}
155
/** Returns the days component. */
156
inline uint32_t days() const {return _days;}
157
/** Sets the months component. */
158
inline void set_months(const uint32_t month) {_months= month;}
159
/** Returns the months component. */
160
inline uint32_t months() const {return _months;}
161
/** Sets the years component. */
162
inline void set_years(const uint32_t year) {_years= year;}
163
/** Returns the years component. */
164
inline uint32_t years() const {return _years;}
165
/** Returns whether the overflow flag was set
166
* (which can occur during an overloaded operator's execution) */
167
inline bool overflow() const {return _overflow;}
169
/** Returns whether the temporal value is valid as a date. */
170
virtual bool is_valid_date() const= 0;
171
/** Returns whether the temporal value is valid as a datetime. */
172
virtual bool is_valid_datetime() const= 0;
173
/** Returns whether the temporal value is valid as a time. */
174
virtual bool is_valid_time() const= 0;
175
/** Returns whether the temporal value is valid as a UNIX timestamp. */
176
virtual bool is_valid_timestamp() const= 0;
179
* Returns whether the temporal
180
* value is valid. Each subclass defines what is
181
* valid for the range of temporal data it contains.
183
virtual bool is_valid() const= 0;
186
* All Temporal derived classes must implement
187
* conversion routines for converting to and from
188
* a string. Subclasses implement other conversion
189
* routines, but should always follow these notes:
191
* 1) Ensure that ALL from_xxx methods call is_valid()
192
* 2) Ensure that ALL to_xxx methods are void returns and
193
* do not call is_valid()
195
* This minimizes the repeated bounds-checking to
196
* just the conversion from_xxx routines.
198
friend class TemporalFormat;
201
/* Forward declaration needed */
207
* Class representing temporal components in a valid
208
* SQL date range, with no time component
210
class Date: public Temporal
213
Date() :Temporal() {}
215
* Comparison operator overloads to compare a Date against
216
* another Date value.
218
* @param Date to compare against.
220
virtual bool operator==(const Date &rhs);
221
virtual bool operator!=(const Date &rhs);
222
virtual bool operator>(const Date &rhs);
223
virtual bool operator>=(const Date &rhs);
224
virtual bool operator<(const Date &rhs);
225
virtual bool operator<=(const Date &rhs);
228
* Comparison operator overloads to compare a Date against
231
* @param DateTime to compare against.
233
virtual bool operator==(const DateTime &rhs);
234
virtual bool operator!=(const DateTime &rhs);
235
virtual bool operator>(const DateTime &rhs);
236
virtual bool operator>=(const DateTime &rhs);
237
virtual bool operator<(const DateTime &rhs);
238
virtual bool operator<=(const DateTime &rhs);
241
* Comparison operator overloads to compare this against
244
* @param Timestamp to compare against.
246
virtual bool operator==(const Timestamp &rhs);
247
virtual bool operator!=(const Timestamp &rhs);
248
virtual bool operator>(const Timestamp &rhs);
249
virtual bool operator>=(const Timestamp &rhs);
250
virtual bool operator<(const Timestamp &rhs);
251
virtual bool operator<=(const Timestamp &rhs);
254
* Operator overload for adding/subtracting another Date
255
* (or subclass) to/from this temporal. When subtracting
256
* or adding two Dates, we return a new Date instance.
258
* @param Date instance to add/subtract to/from
260
const Date operator-(const Date &rhs);
261
const Date operator+(const Date &rhs);
262
Date& operator+=(const Date &rhs);
263
Date& operator-=(const Date &rhs);
266
* Operator to add/subtract a Time from a Time.
267
* We can return a Time new temporal instance.
269
* @param Temporal instance to add/subtract to/from
271
const Date operator-(const Time &rhs);
272
const Date operator+(const Time &rhs);
273
Date& operator-=(const Time &rhs);
274
Date& operator+=(const Time &rhs);
278
* Operator overload for adding/subtracting a DateTime
279
* (or subclass) to/from this temporal. When subtracting
280
* or adding two Dates, we return a new Date instance.
282
* @param DateTime instance to add/subtract to/from
284
const Date operator-(const DateTime &rhs);
285
const Date operator+(const DateTime &rhs);
286
Date& operator+=(const DateTime &rhs);
287
Date& operator-=(const DateTime &rhs);
291
* Operator overload for when a DateTime instance is
292
* assigned to a Date. We do a copy of the DateTime's
293
* date-related components.
295
* @param The DateTime to copy from
297
Date& operator=(const DateTime &rhs);
299
virtual bool is_valid_date() const {return is_valid();}
300
virtual bool is_valid_datetime() const {return is_valid();}
301
virtual bool is_valid_time() const {return false;}
302
virtual bool is_valid_timestamp() const
304
return is_valid() && in_unix_epoch();
307
/** Returns whether the temporal value is valid date. */
308
virtual bool is_valid() const;
309
/* Returns whether the Date (or subclass) instance is in the Unix Epoch. */
310
virtual bool in_unix_epoch() const;
313
* Fills a supplied char string with a
314
* string representation of the Date
317
* @param C-String to fill.
318
* @param Length of to C-String
319
* @returns length of string written (including trailing '\0').
320
* If output was truncated, returns length that would have
323
virtual int to_string(char *to, size_t to_len) const;
326
* Maximum length of C-String needed to represent type
329
static const int MAX_STRING_LENGTH= 11;
332
* Attempts to populate the Date instance based
333
* on the contents of a supplied string.
335
* Returns whether the conversion was
338
* @param String to convert from
339
* @param Length of supplied string (not including trailing '\0').
341
virtual bool from_string(const char *from, size_t from_len);
344
* Fills a supplied 8-byte integer pointer with an
345
* integer representation of the Date
348
* @param Integer to fill.
350
virtual void to_int64_t(int64_t *to) const;
353
* Fills a supplied 4-byte integer pointer with an
354
* integer representation of the Date
357
* @param Integer to fill.
359
virtual void to_int32_t(int32_t *to) const;
362
* Attempts to populate the Date instance based
363
* on the contents of a supplied 4-byte integer.
365
* Returns whether the conversion was
368
* @param Integer to convert from
370
virtual bool from_int32_t(const int32_t from);
373
* Fills a supplied int64_t with the Julian Day Number
374
* representation of this Date.
376
* @note Julian Day Number != julian day!
378
* Julian Day Number is the monotonically increasing number
379
* of days from the start of the Julian calendar (~4713 B.C.)
381
* julian day is the ordinal day number of a day in a year.
383
* @param int64_t to fill
385
void to_julian_day_number(int64_t *to) const;
388
* Attempts to populate the Date instance based
389
* on the contents of a supplied Julian Day Number
391
* Returns whether the conversion was
394
* @param Integer to convert from
396
bool from_julian_day_number(const int64_t from);
399
* Fills a supplied tm pointer with an
400
* representation of the Date
405
virtual void to_tm(struct tm *to) const;
408
* Attempts to populate the Date instance based
409
* on the contents of a supplied pointer to struct tm
412
* Returns whether the conversion was
415
* @param Pointe rto the struct tm to convert from
417
virtual bool from_tm(const struct tm *from);
420
* Attempts to convert the Date value into
423
* @param Pointer to a time_t to convert to
425
virtual void to_time_t(time_t &to) const;
428
* Attempts to populate the Date instance based
429
* on the contents of a supplied time_t
431
* Returns whether the conversion was
434
* @param time_t to convert from
436
virtual bool from_time_t(const time_t from);
439
* Fills a supplied type::Decimal with a representation of
442
* @param Pointer to the type::Decimal to fill
444
virtual void to_decimal(type::Decimal *to) const;
446
friend class TemporalInterval;
447
friend class Timestamp;
450
/* Forward declare needed for friendship */
454
* Class representing temporal components having only
455
* a time component, with no date structure
457
class Time: public Temporal
460
Time() :Temporal() {}
461
/* Maximum number of seconds in 23:59:59 (24 * 60 * 60) */
462
static const uint32_t MAX_CUMULATIVE_SECONDS= 86400L;
465
* Comparison operator overloads to compare a Time against
466
* another Time value.
468
* @param Time to compare against.
470
bool operator==(const Time &rhs);
471
bool operator!=(const Time &rhs);
472
bool operator>(const Time &rhs);
473
bool operator>=(const Time &rhs);
474
bool operator<(const Time &rhs);
475
bool operator<=(const Time &rhs);
477
* Operator to add/subtract a Time from a Time.
478
* We can return a Time new temporal instance.
480
* @param Temporal instance to add/subtract to/from
482
const Time operator-(const Time &rhs);
483
const Time operator+(const Time &rhs);
484
Time& operator-=(const Time &rhs);
485
Time& operator+=(const Time &rhs);
487
bool is_valid_date() const {return false;}
488
bool is_valid_datetime() const {return false;}
489
bool is_valid_time() const {return is_valid();}
490
bool is_valid_timestamp() const {return false;}
492
/** Returns whether the temporal value is valid date. */
493
bool is_valid() const;
494
bool is_fuzzy_valid() const;
497
* Fills a supplied char string with a
498
* string representation of the Time
501
* @param C-String to fill
502
* @param Length of to C-String
503
* @returns length of string written (not including trailing '\0').
504
* If output was truncated, returns length that would have
507
int to_string(char *to, size_t to_len) const;
510
* Maximum length of C-String needed to represent type
513
static const int MAX_STRING_LENGTH= 9;
517
* Attempts to populate the Time instance based
518
* on the contents of a supplied string.
520
* Returns whether the conversion was
523
* @param String to convert from
524
* @param Length of supplied string
526
bool from_string(const char *from, size_t from_len);
529
* Fills a supplied 4-byte integer pointer with an
530
* integer representation of the Time
533
* @param Integer to fill.
535
void to_int32_t(int32_t *to) const;
538
* Fills a supplied 8-byte integer pointer with an
539
* integer representation of the Time
540
* value. It is assume seconds past unix epoch
542
* @param Integer to fill.
544
void to_uint64_t(uint64_t &to) const;
547
* Attempts to populate the Time instance based
548
* on the contents of a supplied 4-byte integer.
550
* Returns whether the conversion was
553
* @param Integer to convert from
555
bool from_int32_t(const int32_t from);
558
* Attempts to populate the Time instance based
559
* on the contents of a supplied time_t
561
* Returns whether the conversion was
566
* We can only convert *from* a time_t, not back
567
* to a time_t since it would be a lossy conversion.
569
* @param time_t to convert from
571
bool from_time_t(const time_t from);
574
* Fills a supplied type::Decimal with a representation of
577
* @param Pointer to the type::Decimal to fill
579
void to_decimal(type::Decimal *to) const;
582
friend class DateTime;
586
* Class representing temporal components in a valid
587
* SQL datetime range, including a time component
589
class DateTime: public Date
592
DateTime() :Date() {}
594
friend class TemporalInterval;
596
/** Returns whether the DateTime (or subclass) instance
597
* is in the Unix Epoch.
599
bool in_unix_epoch() const;
600
/** Returns whether the temporal value is valid datetime. */
601
virtual bool is_valid() const;
604
* It's not possible to convert to and from a DateTime and
605
* a 4-byte integer, so let us know if we try and do it!
607
void to_int32_t(int32_t *) const {assert(0);}
608
bool from_int32_t(int32_t) {assert(0); return false;}
611
* Fills a supplied char string with a
612
* string representation of the DateTime
615
* @param C-String to fill
616
* @param Length of to C-String
617
* @returns length of string written (not including trailing '\0').
618
* If output was truncated, returns length that would have
621
virtual int to_string(char *to, size_t to_len) const;
624
* Maximum length of C-String needed to represent type
627
static const int MAX_STRING_LENGTH= 27;
630
* Attempts to populate the DateTime instance based
631
* on the contents of a supplied string.
633
* Returns whether the conversion was
636
* @param String to convert from
637
* @param Length of supplied string
639
bool from_string(const char *from, size_t from_len);
642
* Fills a supplied 8-byte integer pointer with an
643
* integer representation of the DateTime
646
* @param Integer to fill.
648
void to_int64_t(int64_t *to) const;
651
* Attempts to populate the DateTime instance based
652
* on the contents of a supplied time_t
654
* Returns whether the conversion was
657
* @param time_t to convert from
659
bool from_time_t(const time_t from);
660
bool from_timeval(struct timeval &_timeval);
663
* Attempts to populate the DateTime instance based
664
* on the contents of a supplied 8-byte integer.
666
* Returns whether the conversion was
669
* @param Integer to convert from
670
* @param convert if conversion to canonical representation
671
* should be attempted
673
bool from_int64_t(const int64_t from, bool convert);
675
bool from_int64_t(const int64_t from) {
676
return from_int64_t(from, true);
680
* Fills a supplied tm pointer with an
681
* representation of the DateTime
686
void to_tm(struct tm *to) const;
689
* Fills a supplied type::Decimal with a representation of
690
* the DateTime value.
692
* @param Pointer to the type::Decimal to fill
694
void to_decimal(type::Decimal *to) const;
696
friend class Timestamp;
700
* Class representing temporal components in the UNIX epoch
702
class Timestamp: public DateTime
705
Timestamp() :DateTime() {}
708
* Comparison operator overloads to compare this against
711
* @param Timestamp to compare against.
713
bool operator==(const Date &rhs);
714
bool operator!=(const Date &rhs);
715
bool operator>(const Date &rhs);
716
bool operator>=(const Date &rhs);
717
bool operator<(const Date &rhs);
718
bool operator<=(const Date &rhs);
721
* Comparison operator overloads to compare this against
724
* @param DateTime to compare against.
726
bool operator==(const DateTime &rhs);
727
bool operator!=(const DateTime &rhs);
728
bool operator>(const DateTime &rhs);
729
bool operator>=(const DateTime &rhs);
730
bool operator<(const DateTime &rhs);
731
bool operator<=(const DateTime &rhs);
734
* Comparison operator overloads to compare this against
735
* another Timestamp value.
737
* @param Timestamp to compare against.
739
bool operator==(const Timestamp &rhs);
740
bool operator!=(const Timestamp &rhs);
741
bool operator>(const Timestamp &rhs);
742
bool operator>=(const Timestamp &rhs);
743
bool operator<(const Timestamp &rhs);
744
bool operator<=(const Timestamp &rhs);
746
bool is_valid_timestamp() const {return is_valid();}
747
/** Returns whether the temporal value is valid timestamp. */
748
virtual bool is_valid() const;
751
* Attempts to convert the Timestamp value into
754
* @param Pointer to a time_t to convert to
756
void to_time_t(time_t &to) const;
760
* Operator overload to an output stream for a Timestamp.
762
std::ostream& operator<<(std::ostream& os, const Timestamp& subject);
765
* Class representing temporal components in the UNIX epoch
766
* with an additional microsecond component.
768
class MicroTimestamp: public Timestamp
771
MicroTimestamp() :Timestamp() {}
772
/** Returns whether the temporal value is valid micro-timestamp. */
773
bool is_valid() const;
776
* Fills a supplied char string with a
777
* string representation of the MicroTimestamp
780
* @param C-String to fill
781
* @param Length of to C-String
782
* @returns length of string written (not including trailing '\0').
783
* If output was truncated, returns length that would have
786
int to_string(char *to, size_t to_len) const;
789
* Maximum length of C-String needed to represent type
792
static const int MAX_STRING_LENGTH= 27;
795
* Fills a supplied timeval pointer with an
796
* representation of the MicroTimestamp
799
* Returns whether the conversion was
802
* @param timeval to fill.
804
void to_timeval(struct timeval &to) const;
808
* Class representing temporal components in the UNIX epoch
809
* with an additional nanosecond component.
811
class NanoTimestamp: public Timestamp
814
NanoTimestamp() :Timestamp() {}
815
/** Returns whether the temporal value is valid nano-timestamp. */
816
bool is_valid() const;
819
* Fills a supplied timespec pointer with an
820
* representation of the NanoTimestamp
823
* Returns whether the conversion was
826
* @param timespec to fill.
828
void to_timespec(struct timespec *to) const;
831
} /* end namespace drizzled */
833
#endif /* DRIZZLED_TEMPORAL_H */