Drizzled Public API Documentation

temporal.h
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-2009 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 
42 #pragma once
43 
44 #define DRIZZLE_MAX_SECONDS 59
45 #define DRIZZLE_MAX_SECONDS_WITH_LEAP 61
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
59 
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
64 
65 #define DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING 40
66 
67 #define DRIZZLE_YY_PART_YEAR 70
68 
69 #include <drizzled/calendar.h>
70 #include <drizzled/common_fwd.h>
71 #include <cassert>
72 #include <ostream>
73 
74 namespace drizzled {
75 
80 {
81 protected:
82  enum calendar _calendar;
83  uint32_t _years;
84  uint32_t _months;
85  uint32_t _days;
86  uint32_t _hours;
87  uint32_t _minutes;
88  uint32_t _seconds;
89  time_t _epoch_seconds;
90  uint32_t _useconds;
91  uint32_t _nseconds;
93  bool _overflow;
95  uint64_t _cumulative_seconds_in_time() const;
97  inline void _reset()
98  {
99  _years= _months= _days= _hours= _minutes=
100  _seconds= _epoch_seconds= _useconds= _nseconds= 0;
101  }
102 
103 public:
104  Temporal();
105  virtual ~Temporal() {}
106 
108  inline enum calendar calendar() const {return _calendar;}
110  inline void set_nseconds(const uint32_t nsecond) {_nseconds= nsecond;}
112  inline uint32_t nseconds() const {return _nseconds;}
114  inline void set_useconds(const uint32_t usecond) {_useconds= usecond;}
116  inline uint32_t useconds() const {return _useconds;}
121  void set_epoch_seconds();
123  inline void set_epoch_seconds(const uint32_t epoch_second)
124  {_epoch_seconds= epoch_second;}
126  inline time_t epoch_seconds() const {return _epoch_seconds;}
128  inline void set_seconds(const uint32_t second) {_seconds= second;}
130  inline uint32_t seconds() const {return _seconds;}
132  inline void set_minutes(const uint32_t minute) {_minutes= minute;}
134  inline uint32_t minutes() const {return _minutes;}
136  inline void set_hours(const uint32_t hour) {_hours= hour;}
138  inline uint32_t hours() const {return _hours;}
140  inline void set_days(const uint32_t day) {_days= day;}
142  inline uint32_t days() const {return _days;}
144  inline void set_months(const uint32_t month) {_months= month;}
146  inline uint32_t months() const {return _months;}
148  inline void set_years(const uint32_t year) {_years= year;}
150  inline uint32_t years() const {return _years;}
153  inline bool overflow() const {return _overflow;}
154 
156  virtual bool is_valid_date() const= 0;
158  virtual bool is_valid_datetime() const= 0;
160  virtual bool is_valid_time() const= 0;
162  virtual bool is_valid_timestamp() const= 0;
163 
169  virtual bool is_valid() const= 0;
170 
184  friend class TemporalFormat;
185 };
186 
192 {
193 public:
194  Date() :Temporal() {}
201  virtual bool operator==(const Date &rhs);
202  virtual bool operator!=(const Date &rhs);
203  virtual bool operator>(const Date &rhs);
204  virtual bool operator>=(const Date &rhs);
205  virtual bool operator<(const Date &rhs);
206  virtual bool operator<=(const Date &rhs);
207 
214  virtual bool operator==(const DateTime &rhs);
215  virtual bool operator!=(const DateTime &rhs);
216  virtual bool operator>(const DateTime &rhs);
217  virtual bool operator>=(const DateTime &rhs);
218  virtual bool operator<(const DateTime &rhs);
219  virtual bool operator<=(const DateTime &rhs);
220 
227  virtual bool operator==(const Timestamp &rhs);
228  virtual bool operator!=(const Timestamp &rhs);
229  virtual bool operator>(const Timestamp &rhs);
230  virtual bool operator>=(const Timestamp &rhs);
231  virtual bool operator<(const Timestamp &rhs);
232  virtual bool operator<=(const Timestamp &rhs);
233 
241  const Date operator-(const Date &rhs);
242  const Date operator+(const Date &rhs);
243  Date& operator+=(const Date &rhs);
244  Date& operator-=(const Date &rhs);
245 
252  const Date operator-(const Time &rhs);
253  const Date operator+(const Time &rhs);
254  Date& operator-=(const Time &rhs);
255  Date& operator+=(const Time &rhs);
256 
257 
265  const Date operator-(const DateTime &rhs);
266  const Date operator+(const DateTime &rhs);
267  Date& operator+=(const DateTime &rhs);
268  Date& operator-=(const DateTime &rhs);
269 
270 
278  Date& operator=(const DateTime &rhs);
279 
280  virtual bool is_valid_date() const {return is_valid();}
281  virtual bool is_valid_datetime() const {return is_valid();}
282  virtual bool is_valid_time() const {return false;}
283  virtual bool is_valid_timestamp() const
284  {
285  return is_valid() && in_unix_epoch();
286  }
287 
289  virtual bool is_valid() const;
290  /* Returns whether the Date (or subclass) instance is in the Unix Epoch. */
291  virtual bool in_unix_epoch() const;
292 
304  virtual int to_string(char *to, size_t to_len) const;
305 
310  static const int MAX_STRING_LENGTH= 11;
311 
322  virtual bool from_string(const char *from, size_t from_len);
323 
331  virtual void to_int64_t(int64_t *to) const;
332 
340  virtual void to_int32_t(int32_t *to) const;
341 
351  virtual bool from_int32_t(const int32_t from);
352 
366  void to_julian_day_number(int64_t *to) const;
367 
377  bool from_julian_day_number(const int64_t from);
378 
386  virtual void to_tm(struct tm *to) const;
387 
398  virtual bool from_tm(const struct tm *from);
399 
406  virtual void to_time_t(time_t &to) const;
407 
417  virtual bool from_time_t(const time_t from);
418 
425  virtual void to_decimal(type::Decimal *to) const;
426 
427  friend class TemporalInterval;
428  friend class Timestamp;
429 };
430 
431 /* Forward declare needed for friendship */
432 class DateTime;
433 
439 {
440 public:
441  Time() :Temporal() {}
442  /* Maximum number of seconds in 23:59:59 (24 * 60 * 60) */
443  static const uint32_t MAX_CUMULATIVE_SECONDS= 86400L;
444 
451  bool operator==(const Time &rhs);
452  bool operator!=(const Time &rhs);
453  bool operator>(const Time &rhs);
454  bool operator>=(const Time &rhs);
455  bool operator<(const Time &rhs);
456  bool operator<=(const Time &rhs);
463  const Time operator-(const Time &rhs);
464  const Time operator+(const Time &rhs);
465  Time& operator-=(const Time &rhs);
466  Time& operator+=(const Time &rhs);
467 
468  bool is_valid_date() const {return false;}
469  bool is_valid_datetime() const {return false;}
470  bool is_valid_time() const {return is_valid();}
471  bool is_valid_timestamp() const {return false;}
472 
474  bool is_valid() const;
475  bool is_fuzzy_valid() const;
476 
488  int to_string(char *to, size_t to_len) const;
489 
494  static const int MAX_STRING_LENGTH= 9;
495 
496 
507  bool from_string(const char *from, size_t from_len);
508 
516  void to_int32_t(int32_t *to) const;
517 
525  void to_uint64_t(uint64_t &to) const;
526 
536  bool from_int32_t(const int32_t from);
537 
552  bool from_time_t(const time_t from);
553 
560  void to_decimal(type::Decimal *to) const;
561 
562  friend class Date;
563  friend class DateTime;
564 };
565 
571 {
572 public:
573  DateTime() :Date() {}
574 
575  friend class TemporalInterval;
576 
580  bool in_unix_epoch() const;
582  virtual bool is_valid() const;
583 
588  void to_int32_t(int32_t *) const {assert(0);}
589  bool from_int32_t(int32_t) {assert(0); return false;}
590 
602  virtual int to_string(char *to, size_t to_len) const;
603 
608  static const int MAX_STRING_LENGTH= 27;
609 
620  bool from_string(const char *from, size_t from_len);
621 
629  void to_int64_t(int64_t *to) const;
630 
640  bool from_time_t(const time_t from);
641  bool from_timeval(struct timeval &_timeval);
642 
654  bool from_int64_t(const int64_t from, bool convert);
655 
656  bool from_int64_t(const int64_t from) {
657  return from_int64_t(from, true);
658  }
659 
667  void to_tm(struct tm *to) const;
668 
675  void to_decimal(type::Decimal *to) const;
676 
677  friend class Timestamp;
678 };
679 
684 {
685 public:
686  Timestamp() :DateTime() {}
687 
694  bool operator==(const Date &rhs);
695  bool operator!=(const Date &rhs);
696  bool operator>(const Date &rhs);
697  bool operator>=(const Date &rhs);
698  bool operator<(const Date &rhs);
699  bool operator<=(const Date &rhs);
700 
707  bool operator==(const DateTime &rhs);
708  bool operator!=(const DateTime &rhs);
709  bool operator>(const DateTime &rhs);
710  bool operator>=(const DateTime &rhs);
711  bool operator<(const DateTime &rhs);
712  bool operator<=(const DateTime &rhs);
713 
720  bool operator==(const Timestamp &rhs);
721  bool operator!=(const Timestamp &rhs);
722  bool operator>(const Timestamp &rhs);
723  bool operator>=(const Timestamp &rhs);
724  bool operator<(const Timestamp &rhs);
725  bool operator<=(const Timestamp &rhs);
726 
727  bool is_valid_timestamp() const {return is_valid();}
729  virtual bool is_valid() const;
730 
737  void to_time_t(time_t &to) const;
738 };
739 
743 std::ostream& operator<<(std::ostream& os, const Timestamp& subject);
744 
750 {
751 public:
752  MicroTimestamp() :Timestamp() {}
754  bool is_valid() const;
755 
767  int to_string(char *to, size_t to_len) const;
768 
773  static const int MAX_STRING_LENGTH= 27;
774 
785  void to_timeval(struct timeval &to) const;
786 };
787 
793 {
794 public:
795  NanoTimestamp() :Timestamp() {}
797  bool is_valid() const;
798 
809  void to_timespec(struct timespec *to) const;
810 };
811 
812 } /* end namespace drizzled */
813 
bool is_valid_time() const
Definition: temporal.h:470
bool is_valid_timestamp() const
Definition: temporal.h:727
void set_hours(const uint32_t hour)
Definition: temporal.h:136
Stores time interval for date/time manipulation.
uint32_t nseconds() const
Definition: temporal.h:112
uint32_t useconds() const
Definition: temporal.h:116
uint32_t hours() const
Definition: temporal.h:138
virtual bool is_valid_time() const
Definition: temporal.h:282
void set_useconds(const uint32_t usecond)
Definition: temporal.h:114
TODO: Rename this file - func.h is stupid.
time_t epoch_seconds() const
Definition: temporal.h:126
bool is_valid_timestamp() const
Definition: temporal.h:471
bool is_valid_date() const
Definition: temporal.h:468
void set_years(const uint32_t year)
Definition: temporal.h:148
uint32_t months() const
Definition: temporal.h:146
void set_minutes(const uint32_t minute)
Definition: temporal.h:132
void set_nseconds(const uint32_t nsecond)
Definition: temporal.h:110
bool from_int32_t(int32_t)
Definition: temporal.h:589
#define DRIZZLED_API
Definition: visibility.h:62
void set_days(const uint32_t day)
Definition: temporal.h:140
virtual bool is_valid_datetime() const
Definition: temporal.h:281
uint32_t minutes() const
Definition: temporal.h:134
void set_seconds(const uint32_t second)
Definition: temporal.h:128
bool is_valid_datetime() const
Definition: temporal.h:469
uint32_t days() const
Definition: temporal.h:142
bool overflow() const
Definition: temporal.h:153
void set_months(const uint32_t month)
Definition: temporal.h:144
uint32_t years() const
Definition: temporal.h:150
virtual bool is_valid_date() const
Definition: temporal.h:280
void set_epoch_seconds(const uint32_t epoch_second)
Definition: temporal.h:123
void to_int32_t(int32_t *) const
Definition: temporal.h:588
uint32_t seconds() const
Definition: temporal.h:130
virtual bool is_valid_timestamp() const
Definition: temporal.h:283