Drizzled Public API Documentation

time.h
00001 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008 MySQL
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 
00021 #pragma once
00022 
00023 #if TIME_WITH_SYS_TIME
00024 # include <sys/time.h>
00025 # include <time.h>
00026 #else
00027 # if HAVE_SYS_TIME_H
00028 #  include <sys/time.h>
00029 # else
00030 #  include <time.h>
00031 # endif
00032 #endif
00033 
00034 #include <drizzled/common_fwd.h>
00035 
00036 namespace drizzled {
00037 
00038 extern uint64_t log_10_int[20];
00039 extern unsigned char days_in_month[];
00040 
00041 /* Time handling defaults */
00042 #define TIMESTAMP_MIN_YEAR (1900 + YY_PART_YEAR - 1)
00043 #define TIMESTAMP_MAX_VALUE INT32_MAX
00044 #define TIMESTAMP_MIN_VALUE 1
00045 
00046 /* two-digit years < this are 20..; >= this are 19.. */
00047 #define YY_PART_YEAR     70
00048 
00049 /* Flags to str_to_datetime */
00050 #define TIME_FUZZY_DATE   1
00051 #define TIME_DATETIME_ONLY  2
00052 
00053 /* Must be same as MODE_NO_ZERO_IN_DATE */
00054 #define TIME_NO_ZERO_IN_DATE    (65536L*2*2*2*2*2*2*2)
00055 
00056 /* Must be same as MODE_NO_ZERO_DATE */
00057 #define TIME_NO_ZERO_DATE (TIME_NO_ZERO_IN_DATE*2)
00058 #define TIME_INVALID_DATES  (TIME_NO_ZERO_DATE*2)
00059 
00060 #define DRIZZLE_TIME_WARN_TRUNCATED    1
00061 #define DRIZZLE_TIME_WARN_OUT_OF_RANGE 2
00062 
00063 /* Limits for the TIME data type */
00064 #define TIME_MAX_HOUR 838
00065 #define TIME_MAX_MINUTE 59
00066 #define TIME_MAX_SECOND 59
00067 #define TIME_MAX_VALUE (TIME_MAX_HOUR*10000 + TIME_MAX_MINUTE*100 + \
00068                         TIME_MAX_SECOND)
00069 
00070 /*
00071   Structure which is used to represent datetime values inside Drizzle.
00072 
00073   We assume that values in this structure are normalized, i.e. year <= 9999,
00074   month <= 12, day <= 31, hour <= 23, hour <= 59, hour <= 59. Many functions
00075   in server such as my_system_gmt_sec() or make_time() family of functions
00076   rely on this (actually now usage of make_*() family relies on a bit weaker
00077   restriction). Also functions that produce type::Time as result ensure this.
00078   There is one exception to this rule though if this structure holds time
00079   value (time_type == DRIZZLE_TIMESTAMP_TIME) days and hour member can hold
00080   bigger values.
00081 */
00082 namespace type {
00083 
00084 enum timestamp_t
00085 {
00086   DRIZZLE_TIMESTAMP_NONE= -2, DRIZZLE_TIMESTAMP_ERROR= -1,
00087   DRIZZLE_TIMESTAMP_DATE= 0, DRIZZLE_TIMESTAMP_DATETIME= 1, DRIZZLE_TIMESTAMP_TIME= 2
00088 };
00089 
00090 enum cut_t
00091 {
00092   VALID= 0,
00093   CUT= 1,
00094   INVALID= 2
00095 };
00096 
00097 /*
00098   datatime_t while being stored in an integer is actually a formatted value.
00099 */
00100 
00101 inline bool is_valid(datetime_t value)
00102 {
00103   return value != -1;
00104 }
00105 
00106 class Time
00107 {
00108 public:
00109   Time()
00110   {
00111     reset();
00112   }
00113 
00114   Time(uint32_t year_arg,
00115        uint32_t month_arg,
00116        uint32_t day_arg,
00117        uint32_t hour_arg,
00118        uint32_t minute_arg,
00119        uint32_t second_arg,
00120        usec_t second_part_arg,
00121        timestamp_t type_arg) :
00122     year(year_arg),
00123     month(month_arg),
00124     day(day_arg),
00125     hour(hour_arg),
00126     minute(minute_arg),
00127     second(second_arg),
00128     second_part(second_part_arg),
00129     neg(false),
00130     time_type(type_arg)
00131   {
00132   }
00133 
00134   Time(uint32_t hour_arg,
00135        uint32_t minute_arg,
00136        uint32_t second_arg,
00137        usec_t second_part_arg,
00138        bool neg_arg) :
00139     year(0),
00140     month(0),
00141     day(0),
00142     hour(hour_arg),
00143     minute(minute_arg),
00144     second(second_arg),
00145     second_part(second_part_arg),
00146     neg(neg_arg),
00147     time_type(DRIZZLE_TIMESTAMP_TIME)
00148   {
00149   }
00150 
00151   uint32_t year, month, day, hour, minute, second;
00152   usec_t second_part;
00153   bool neg;
00154   timestamp_t time_type;
00155 
00156   void reset()
00157   {
00158     year= month= day= hour= minute= second= second_part= 0;
00159     neg= false;
00160     time_type= DRIZZLE_TIMESTAMP_DATE;
00161   }
00162 
00163   timestamp_t type() const
00164   {
00165     return time_type;
00166   }
00167 
00168   void convert(drizzled::String &str, timestamp_t arg= type::DRIZZLE_TIMESTAMP_DATETIME);
00169   void convert(char *str, size_t &to_length, timestamp_t arg= type::DRIZZLE_TIMESTAMP_DATETIME);
00170   void convert(datetime_t &datetime, timestamp_t arg= type::DRIZZLE_TIMESTAMP_DATETIME);
00171   void convert(datetime_t &ret, int64_t nr, uint32_t flags);
00172   void convert(datetime_t &ret, int64_t nr, uint32_t flags, type::cut_t &was_cut);
00173   void convert(type::epoch_t &epoch, long *my_timezone) const;
00174 
00175   void truncate(const timestamp_t arg);
00176 
00177   bool store(const char *str,uint32_t length, int &warning, type::timestamp_t arg= DRIZZLE_TIMESTAMP_TIME);
00178   type::timestamp_t store(const char *str, uint32_t length, uint32_t flags, type::cut_t &was_cut);
00179   type::timestamp_t store(const char *str, uint32_t length, uint32_t flags);
00180   void store(type::epoch_t from);
00181   void store(type::epoch_t from, usec_t from_fractional_seconds);
00182   void store(const tm&);
00183   void store(const timeval&);
00184 
00185 
00186   static const uint32_t FRACTIONAL_DIGITS= 1000000;
00187   static const size_t MAX_STRING_LENGTH= 32;   // +32 to make my_snprintf_{8bit|ucs2} happy
00188 
00189   bool check(bool not_zero_date, uint32_t flags, type::cut_t &was_cut) const;
00190 
00191   inline bool isValidEpoch() const
00192   {
00193     if ((year < TIMESTAMP_MIN_YEAR) or (year == TIMESTAMP_MIN_YEAR && (month < 12 || day < 31)))
00194     {
00195       return false;
00196     }
00197 
00198     return true;
00199   }
00200 };
00201 
00202 }
00203 
00204 long calc_daynr(uint32_t year,uint32_t month,uint32_t day);
00205 uint32_t calc_days_in_year(uint32_t year);
00206 uint32_t year_2000_handling(uint32_t year);
00207 
00208 void init_time();
00209 
00210 /*
00211   Available interval types used in any statement.
00212 
00213   'interval_type' must be sorted so that simple intervals comes first,
00214   ie year, quarter, month, week, day, hour, etc. The order based on
00215   interval size is also important and the intervals should be kept in a
00216   large to smaller order. (get_interval_value() depends on this)
00217 
00218   Note: If you change the order of elements in this enum you should fix
00219   order of elements in 'interval_type_to_name' and 'interval_names'
00220   arrays
00221 
00222   See also interval_type_to_name, get_interval_value, interval_names
00223 */
00224 
00225 enum interval_type
00226 {
00227   INTERVAL_YEAR, INTERVAL_QUARTER, INTERVAL_MONTH, INTERVAL_WEEK, INTERVAL_DAY,
00228   INTERVAL_HOUR, INTERVAL_MINUTE, INTERVAL_SECOND, INTERVAL_MICROSECOND,
00229   INTERVAL_YEAR_MONTH, INTERVAL_DAY_HOUR, INTERVAL_DAY_MINUTE,
00230   INTERVAL_DAY_SECOND, INTERVAL_HOUR_MINUTE, INTERVAL_HOUR_SECOND,
00231   INTERVAL_MINUTE_SECOND, INTERVAL_DAY_MICROSECOND, INTERVAL_HOUR_MICROSECOND,
00232   INTERVAL_MINUTE_MICROSECOND, INTERVAL_SECOND_MICROSECOND, INTERVAL_LAST
00233 };
00234 
00235 } /* namespace drizzled */