00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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
00047 #define YY_PART_YEAR 70
00048
00049
00050 #define TIME_FUZZY_DATE 1
00051 #define TIME_DATETIME_ONLY 2
00052
00053
00054 #define TIME_NO_ZERO_IN_DATE (65536L*2*2*2*2*2*2*2)
00055
00056
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
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
00072
00073
00074
00075
00076
00077
00078
00079
00080
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
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;
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
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
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 }