Drizzled Public API Documentation

temporal.h

Go to the documentation of this file.
00001 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008-2009 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00042 #pragma once
00043 
00044 #define DRIZZLE_MAX_SECONDS 59
00045 #define DRIZZLE_MAX_SECONDS_WITH_LEAP 61
00046 #define DRIZZLE_MAX_MINUTES 59
00047 #define DRIZZLE_MAX_HOURS 23
00048 #define DRIZZLE_MAX_DAYS 31
00049 #define DRIZZLE_MAX_MONTHS 12
00050 #define DRIZZLE_MAX_YEARS_SQL 9999
00051 #define DRIZZLE_MAX_YEARS_EPOCH 2038
00052 #define DRIZZLE_MIN_SECONDS 0
00053 #define DRIZZLE_MIN_MINUTES 0
00054 #define DRIZZLE_MIN_HOURS 0
00055 #define DRIZZLE_MIN_DAYS 1
00056 #define DRIZZLE_MIN_MONTHS 1
00057 #define DRIZZLE_MIN_YEARS_SQL 1
00058 #define DRIZZLE_MIN_YEARS_EPOCH 1970
00059 
00060 #define DRIZZLE_SECONDS_IN_MINUTE 60
00061 #define DRIZZLE_SECONDS_IN_HOUR (60*60)
00062 #define DRIZZLE_SECONDS_IN_DAY (60*60*24)
00063 #define DRIZZLE_NANOSECONDS_IN_MICROSECOND 1000
00064 
00065 #define DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING 40
00066 
00067 #define DRIZZLE_YY_PART_YEAR  70
00068 
00069 #include <drizzled/calendar.h>
00070 
00071 #include <cassert>
00072 #include <ostream>
00073 
00074 /* Outside forward declarations */
00075 namespace type {
00076 class Decimal;
00077 }
00078 
00079 namespace drizzled
00080 {
00081 
00082 /* Forward declaration needed */
00083 class TemporalInterval;
00084 class TemporalIntervalYear;
00085 class TemporalIntervalDayOrLess;
00086 class TemporalIntervalDayOrWeek;
00087 class TemporalIntervalYearMonth;
00088 
00092 class Temporal
00093 {
00094 protected:
00095   enum calendar _calendar;
00096   uint32_t _years;
00097   uint32_t _months;
00098   uint32_t _days;
00099   uint32_t _hours;
00100   uint32_t _minutes;
00101   uint32_t _seconds;
00102   time_t _epoch_seconds;
00103   uint32_t _useconds;
00104   uint32_t _nseconds;
00106   bool _overflow;
00108   uint64_t _cumulative_seconds_in_time() const;
00110   inline void _reset()
00111   {
00112     _years= _months= _days= _hours= _minutes=
00113       _seconds= _epoch_seconds= _useconds= _nseconds= 0;
00114   }
00115 
00116 public:
00117   Temporal();
00118   virtual ~Temporal() {}
00119 
00121   inline enum calendar calendar() const {return _calendar;}
00123   inline void set_nseconds(const uint32_t nsecond) {_nseconds= nsecond;}
00125   inline uint32_t nseconds() const {return _nseconds;}
00127   inline void set_useconds(const uint32_t usecond) {_useconds= usecond;}
00129   inline uint32_t useconds() const {return _useconds;}
00134   void set_epoch_seconds();
00136   inline void set_epoch_seconds(const uint32_t epoch_second)
00137   {_epoch_seconds= epoch_second;}
00139   inline time_t epoch_seconds() const {return _epoch_seconds;}
00141   inline void set_seconds(const uint32_t second) {_seconds= second;}
00143   inline uint32_t seconds() const {return _seconds;}
00145   inline void set_minutes(const uint32_t minute) {_minutes= minute;}
00147   inline uint32_t minutes() const {return _minutes;}
00149   inline void set_hours(const uint32_t hour) {_hours= hour;}
00151   inline uint32_t hours() const {return _hours;}
00153   inline void set_days(const uint32_t day) {_days= day;}
00155   inline uint32_t days() const {return _days;}
00157   inline void set_months(const uint32_t month) {_months= month;}
00159   inline uint32_t months() const {return _months;}
00161   inline void set_years(const uint32_t year) {_years= year;}
00163   inline uint32_t years() const {return _years;}
00166   inline bool overflow() const {return _overflow;}
00167 
00169   virtual bool is_valid_date() const= 0;
00171   virtual bool is_valid_datetime() const= 0;
00173   virtual bool is_valid_time() const= 0;
00175   virtual bool is_valid_timestamp() const= 0;
00176 
00182   virtual bool is_valid() const= 0;
00183 
00197   friend class TemporalFormat;
00198 };
00199 
00200 /* Forward declaration needed */
00201 class DateTime;
00202 class Timestamp;
00203 class Time;
00204 
00209 class Date: public Temporal
00210 {
00211 public:
00212   Date() :Temporal() {}
00219   virtual bool operator==(const Date &rhs);
00220   virtual bool operator!=(const Date &rhs);
00221   virtual bool operator>(const Date &rhs);
00222   virtual bool operator>=(const Date &rhs);
00223   virtual bool operator<(const Date &rhs);
00224   virtual bool operator<=(const Date &rhs);
00225 
00232   virtual bool operator==(const DateTime &rhs);
00233   virtual bool operator!=(const DateTime &rhs);
00234   virtual bool operator>(const DateTime &rhs);
00235   virtual bool operator>=(const DateTime &rhs);
00236   virtual bool operator<(const DateTime &rhs);
00237   virtual bool operator<=(const DateTime &rhs);
00238 
00245   virtual bool operator==(const Timestamp &rhs);
00246   virtual bool operator!=(const Timestamp &rhs);
00247   virtual bool operator>(const Timestamp &rhs);
00248   virtual bool operator>=(const Timestamp &rhs);
00249   virtual bool operator<(const Timestamp &rhs);
00250   virtual bool operator<=(const Timestamp &rhs);
00251 
00259   const Date operator-(const Date &rhs);
00260   const Date operator+(const Date &rhs);
00261   Date& operator+=(const Date &rhs);
00262   Date& operator-=(const Date &rhs);
00263 
00270   const Date operator-(const Time &rhs);
00271   const Date operator+(const Time &rhs);
00272   Date& operator-=(const Time &rhs);
00273   Date& operator+=(const Time &rhs);
00274 
00275 
00283   const Date operator-(const DateTime &rhs);
00284   const Date operator+(const DateTime &rhs);
00285   Date& operator+=(const DateTime &rhs);
00286   Date& operator-=(const DateTime &rhs);
00287 
00288 
00296   Date& operator=(const DateTime &rhs);
00297 
00298   virtual bool is_valid_date() const {return is_valid();}
00299   virtual bool is_valid_datetime() const {return is_valid();}
00300   virtual bool is_valid_time() const {return false;}
00301   virtual bool is_valid_timestamp() const
00302   {
00303     return is_valid() && in_unix_epoch();
00304   }
00305 
00307   virtual bool is_valid() const;
00308   /* Returns whether the Date (or subclass) instance is in the Unix Epoch. */
00309   virtual bool in_unix_epoch() const;
00310 
00322   virtual int to_string(char *to, size_t to_len) const;
00323 
00328   static const int MAX_STRING_LENGTH= 11;
00329 
00340   virtual bool from_string(const char *from, size_t from_len);
00341 
00349   virtual void to_int64_t(int64_t *to) const;
00350 
00358   virtual void to_int32_t(int32_t *to) const;
00359 
00369   virtual bool from_int32_t(const int32_t from);
00370 
00384   void to_julian_day_number(int64_t *to) const;
00385 
00395   bool from_julian_day_number(const int64_t from);
00396 
00404   virtual void to_tm(struct tm *to) const;
00405 
00416   virtual bool from_tm(const struct tm *from);
00417 
00424   virtual void to_time_t(time_t &to) const;
00425 
00435   virtual bool from_time_t(const time_t from);
00436 
00443   virtual void to_decimal(type::Decimal *to) const;
00444 
00445   friend class TemporalInterval;
00446   friend class Timestamp;
00447 };
00448 
00449 /* Forward declare needed for friendship */
00450 class DateTime;
00451 
00456 class Time: public Temporal
00457 {
00458 public:
00459   Time() :Temporal() {}
00460   /* Maximum number of seconds in 23:59:59 (24 * 60 * 60) */
00461   static const uint32_t MAX_CUMULATIVE_SECONDS= 86400L;
00462 
00469   bool operator==(const Time &rhs);
00470   bool operator!=(const Time &rhs);
00471   bool operator>(const Time &rhs);
00472   bool operator>=(const Time &rhs);
00473   bool operator<(const Time &rhs);
00474   bool operator<=(const Time &rhs);
00481   const Time operator-(const Time &rhs);
00482   const Time operator+(const Time &rhs);
00483   Time& operator-=(const Time &rhs);
00484   Time& operator+=(const Time &rhs);
00485 
00486   bool is_valid_date() const {return false;}
00487   bool is_valid_datetime() const {return false;}
00488   bool is_valid_time() const {return is_valid();}
00489   bool is_valid_timestamp() const {return false;}
00490 
00492   bool is_valid() const;
00493   bool is_fuzzy_valid() const;
00494 
00506   int to_string(char *to, size_t to_len) const;
00507 
00512   static const int MAX_STRING_LENGTH= 9;
00513 
00514 
00525   bool from_string(const char *from, size_t from_len);
00526 
00534   void to_int32_t(int32_t *to) const;
00535 
00543   void to_uint64_t(uint64_t &to) const;
00544 
00554   bool from_int32_t(const int32_t from);
00555 
00570   bool from_time_t(const time_t from);
00571 
00578   void to_decimal(type::Decimal *to) const;
00579 
00580   friend class Date;
00581   friend class DateTime;
00582 };
00583 
00588 class DateTime: public Date
00589 {
00590 public:
00591   DateTime() :Date() {}
00592 
00593   friend class TemporalInterval;
00594 
00598   bool in_unix_epoch() const;
00600   virtual bool is_valid() const;
00601 
00606   void to_int32_t(int32_t *) const {assert(0);}
00607   bool from_int32_t(int32_t) {assert(0); return false;}
00608 
00620   virtual int to_string(char *to, size_t to_len) const;
00621 
00626   static const int MAX_STRING_LENGTH= 27;
00627 
00638   bool from_string(const char *from, size_t from_len);
00639 
00647   void to_int64_t(int64_t *to) const;
00648 
00658   bool from_time_t(const time_t from);
00659   bool from_timeval(struct timeval &_timeval);
00660 
00672   bool from_int64_t(const int64_t from, bool convert);
00673 
00674   bool from_int64_t(const int64_t from) {
00675     return from_int64_t(from, true);
00676   }
00677 
00685   void to_tm(struct tm *to) const;
00686 
00693   void to_decimal(type::Decimal *to) const;
00694 
00695   friend class Timestamp;
00696 };
00697 
00701 class Timestamp: public DateTime
00702 {
00703 public:
00704   Timestamp() :DateTime() {}
00705 
00712   bool operator==(const Date &rhs);
00713   bool operator!=(const Date &rhs);
00714   bool operator>(const Date &rhs);
00715   bool operator>=(const Date &rhs);
00716   bool operator<(const Date &rhs);
00717   bool operator<=(const Date &rhs);
00718 
00725   bool operator==(const DateTime &rhs);
00726   bool operator!=(const DateTime &rhs);
00727   bool operator>(const DateTime &rhs);
00728   bool operator>=(const DateTime &rhs);
00729   bool operator<(const DateTime &rhs);
00730   bool operator<=(const DateTime &rhs);
00731 
00738   bool operator==(const Timestamp &rhs);
00739   bool operator!=(const Timestamp &rhs);
00740   bool operator>(const Timestamp &rhs);
00741   bool operator>=(const Timestamp &rhs);
00742   bool operator<(const Timestamp &rhs);
00743   bool operator<=(const Timestamp &rhs);
00744 
00745   bool is_valid_timestamp() const {return is_valid();}
00747   virtual bool is_valid() const;
00748 
00755   void to_time_t(time_t &to) const;
00756 };
00757 
00761 std::ostream& operator<<(std::ostream& os, const Timestamp& subject);
00762 
00767 class MicroTimestamp: public Timestamp
00768 {
00769 public:
00770   MicroTimestamp() :Timestamp() {}
00772   bool is_valid() const;
00773 
00785   int to_string(char *to, size_t to_len) const;
00786 
00791   static const int MAX_STRING_LENGTH= 27;
00792 
00803   void to_timeval(struct timeval &to) const;
00804 };
00805 
00810 class NanoTimestamp: public Timestamp
00811 {
00812 public:
00813   NanoTimestamp() :Timestamp() {}
00815   bool is_valid() const;
00816 
00827   void to_timespec(struct timespec *to) const;
00828 };
00829 
00830 } /* end namespace drizzled */
00831