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 #include <drizzled/common_fwd.h>
00071 #include <cassert>
00072 #include <ostream>
00073 
00074 namespace drizzled {
00075 
00079 class DRIZZLED_API Temporal
00080 {
00081 protected:
00082   enum calendar _calendar;
00083   uint32_t _years;
00084   uint32_t _months;
00085   uint32_t _days;
00086   uint32_t _hours;
00087   uint32_t _minutes;
00088   uint32_t _seconds;
00089   time_t _epoch_seconds;
00090   uint32_t _useconds;
00091   uint32_t _nseconds;
00093   bool _overflow;
00095   uint64_t _cumulative_seconds_in_time() const;
00097   inline void _reset()
00098   {
00099     _years= _months= _days= _hours= _minutes=
00100       _seconds= _epoch_seconds= _useconds= _nseconds= 0;
00101   }
00102 
00103 public:
00104   Temporal();
00105   virtual ~Temporal() {}
00106 
00108   inline enum calendar calendar() const {return _calendar;}
00110   inline void set_nseconds(const uint32_t nsecond) {_nseconds= nsecond;}
00112   inline uint32_t nseconds() const {return _nseconds;}
00114   inline void set_useconds(const uint32_t usecond) {_useconds= usecond;}
00116   inline uint32_t useconds() const {return _useconds;}
00121   void set_epoch_seconds();
00123   inline void set_epoch_seconds(const uint32_t epoch_second)
00124   {_epoch_seconds= epoch_second;}
00126   inline time_t epoch_seconds() const {return _epoch_seconds;}
00128   inline void set_seconds(const uint32_t second) {_seconds= second;}
00130   inline uint32_t seconds() const {return _seconds;}
00132   inline void set_minutes(const uint32_t minute) {_minutes= minute;}
00134   inline uint32_t minutes() const {return _minutes;}
00136   inline void set_hours(const uint32_t hour) {_hours= hour;}
00138   inline uint32_t hours() const {return _hours;}
00140   inline void set_days(const uint32_t day) {_days= day;}
00142   inline uint32_t days() const {return _days;}
00144   inline void set_months(const uint32_t month) {_months= month;}
00146   inline uint32_t months() const {return _months;}
00148   inline void set_years(const uint32_t year) {_years= year;}
00150   inline uint32_t years() const {return _years;}
00153   inline bool overflow() const {return _overflow;}
00154 
00156   virtual bool is_valid_date() const= 0;
00158   virtual bool is_valid_datetime() const= 0;
00160   virtual bool is_valid_time() const= 0;
00162   virtual bool is_valid_timestamp() const= 0;
00163 
00169   virtual bool is_valid() const= 0;
00170 
00184   friend class TemporalFormat;
00185 };
00186 
00191 class DRIZZLED_API Date: public Temporal
00192 {
00193 public:
00194   Date() :Temporal() {}
00201   virtual bool operator==(const Date &rhs);
00202   virtual bool operator!=(const Date &rhs);
00203   virtual bool operator>(const Date &rhs);
00204   virtual bool operator>=(const Date &rhs);
00205   virtual bool operator<(const Date &rhs);
00206   virtual bool operator<=(const Date &rhs);
00207 
00214   virtual bool operator==(const DateTime &rhs);
00215   virtual bool operator!=(const DateTime &rhs);
00216   virtual bool operator>(const DateTime &rhs);
00217   virtual bool operator>=(const DateTime &rhs);
00218   virtual bool operator<(const DateTime &rhs);
00219   virtual bool operator<=(const DateTime &rhs);
00220 
00227   virtual bool operator==(const Timestamp &rhs);
00228   virtual bool operator!=(const Timestamp &rhs);
00229   virtual bool operator>(const Timestamp &rhs);
00230   virtual bool operator>=(const Timestamp &rhs);
00231   virtual bool operator<(const Timestamp &rhs);
00232   virtual bool operator<=(const Timestamp &rhs);
00233 
00241   const Date operator-(const Date &rhs);
00242   const Date operator+(const Date &rhs);
00243   Date& operator+=(const Date &rhs);
00244   Date& operator-=(const Date &rhs);
00245 
00252   const Date operator-(const Time &rhs);
00253   const Date operator+(const Time &rhs);
00254   Date& operator-=(const Time &rhs);
00255   Date& operator+=(const Time &rhs);
00256 
00257 
00265   const Date operator-(const DateTime &rhs);
00266   const Date operator+(const DateTime &rhs);
00267   Date& operator+=(const DateTime &rhs);
00268   Date& operator-=(const DateTime &rhs);
00269 
00270 
00278   Date& operator=(const DateTime &rhs);
00279 
00280   virtual bool is_valid_date() const {return is_valid();}
00281   virtual bool is_valid_datetime() const {return is_valid();}
00282   virtual bool is_valid_time() const {return false;}
00283   virtual bool is_valid_timestamp() const
00284   {
00285     return is_valid() && in_unix_epoch();
00286   }
00287 
00289   virtual bool is_valid() const;
00290   /* Returns whether the Date (or subclass) instance is in the Unix Epoch. */
00291   virtual bool in_unix_epoch() const;
00292 
00304   virtual int to_string(char *to, size_t to_len) const;
00305 
00310   static const int MAX_STRING_LENGTH= 11;
00311 
00322   virtual bool from_string(const char *from, size_t from_len);
00323 
00331   virtual void to_int64_t(int64_t *to) const;
00332 
00340   virtual void to_int32_t(int32_t *to) const;
00341 
00351   virtual bool from_int32_t(const int32_t from);
00352 
00366   void to_julian_day_number(int64_t *to) const;
00367 
00377   bool from_julian_day_number(const int64_t from);
00378 
00386   virtual void to_tm(struct tm *to) const;
00387 
00398   virtual bool from_tm(const struct tm *from);
00399 
00406   virtual void to_time_t(time_t &to) const;
00407 
00417   virtual bool from_time_t(const time_t from);
00418 
00425   virtual void to_decimal(type::Decimal *to) const;
00426 
00427   friend class TemporalInterval;
00428   friend class Timestamp;
00429 };
00430 
00431 /* Forward declare needed for friendship */
00432 class DateTime;
00433 
00438 class DRIZZLED_API Time: public Temporal
00439 {
00440 public:
00441   Time() :Temporal() {}
00442   /* Maximum number of seconds in 23:59:59 (24 * 60 * 60) */
00443   static const uint32_t MAX_CUMULATIVE_SECONDS= 86400L;
00444 
00451   bool operator==(const Time &rhs);
00452   bool operator!=(const Time &rhs);
00453   bool operator>(const Time &rhs);
00454   bool operator>=(const Time &rhs);
00455   bool operator<(const Time &rhs);
00456   bool operator<=(const Time &rhs);
00463   const Time operator-(const Time &rhs);
00464   const Time operator+(const Time &rhs);
00465   Time& operator-=(const Time &rhs);
00466   Time& operator+=(const Time &rhs);
00467 
00468   bool is_valid_date() const {return false;}
00469   bool is_valid_datetime() const {return false;}
00470   bool is_valid_time() const {return is_valid();}
00471   bool is_valid_timestamp() const {return false;}
00472 
00474   bool is_valid() const;
00475   bool is_fuzzy_valid() const;
00476 
00488   int to_string(char *to, size_t to_len) const;
00489 
00494   static const int MAX_STRING_LENGTH= 9;
00495 
00496 
00507   bool from_string(const char *from, size_t from_len);
00508 
00516   void to_int32_t(int32_t *to) const;
00517 
00525   void to_uint64_t(uint64_t &to) const;
00526 
00536   bool from_int32_t(const int32_t from);
00537 
00552   bool from_time_t(const time_t from);
00553 
00560   void to_decimal(type::Decimal *to) const;
00561 
00562   friend class Date;
00563   friend class DateTime;
00564 };
00565 
00570 class DRIZZLED_API DateTime: public Date
00571 {
00572 public:
00573   DateTime() :Date() {}
00574 
00575   friend class TemporalInterval;
00576 
00580   bool in_unix_epoch() const;
00582   virtual bool is_valid() const;
00583 
00588   void to_int32_t(int32_t *) const {assert(0);}
00589   bool from_int32_t(int32_t) {assert(0); return false;}
00590 
00602   virtual int to_string(char *to, size_t to_len) const;
00603 
00608   static const int MAX_STRING_LENGTH= 27;
00609 
00620   bool from_string(const char *from, size_t from_len);
00621 
00629   void to_int64_t(int64_t *to) const;
00630 
00640   bool from_time_t(const time_t from);
00641   bool from_timeval(struct timeval &_timeval);
00642 
00654   bool from_int64_t(const int64_t from, bool convert);
00655 
00656   bool from_int64_t(const int64_t from) {
00657     return from_int64_t(from, true);
00658   }
00659 
00667   void to_tm(struct tm *to) const;
00668 
00675   void to_decimal(type::Decimal *to) const;
00676 
00677   friend class Timestamp;
00678 };
00679 
00683 class DRIZZLED_API Timestamp: public DateTime
00684 {
00685 public:
00686   Timestamp() :DateTime() {}
00687 
00694   bool operator==(const Date &rhs);
00695   bool operator!=(const Date &rhs);
00696   bool operator>(const Date &rhs);
00697   bool operator>=(const Date &rhs);
00698   bool operator<(const Date &rhs);
00699   bool operator<=(const Date &rhs);
00700 
00707   bool operator==(const DateTime &rhs);
00708   bool operator!=(const DateTime &rhs);
00709   bool operator>(const DateTime &rhs);
00710   bool operator>=(const DateTime &rhs);
00711   bool operator<(const DateTime &rhs);
00712   bool operator<=(const DateTime &rhs);
00713 
00720   bool operator==(const Timestamp &rhs);
00721   bool operator!=(const Timestamp &rhs);
00722   bool operator>(const Timestamp &rhs);
00723   bool operator>=(const Timestamp &rhs);
00724   bool operator<(const Timestamp &rhs);
00725   bool operator<=(const Timestamp &rhs);
00726 
00727   bool is_valid_timestamp() const {return is_valid();}
00729   virtual bool is_valid() const;
00730 
00737   void to_time_t(time_t &to) const;
00738 };
00739 
00743 std::ostream& operator<<(std::ostream& os, const Timestamp& subject);
00744 
00749 class DRIZZLED_API MicroTimestamp: public Timestamp
00750 {
00751 public:
00752   MicroTimestamp() :Timestamp() {}
00754   bool is_valid() const;
00755 
00767   int to_string(char *to, size_t to_len) const;
00768 
00773   static const int MAX_STRING_LENGTH= 27;
00774 
00785   void to_timeval(struct timeval &to) const;
00786 };
00787 
00792 class DRIZZLED_API NanoTimestamp: public Timestamp
00793 {
00794 public:
00795   NanoTimestamp() :Timestamp() {}
00797   bool is_valid() const;
00798 
00809   void to_timespec(struct timespec *to) const;
00810 };
00811 
00812 } /* end namespace drizzled */
00813