drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
type/time.h
1/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2008 MySQL
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
21#pragma once
22
23#include <sys/time.h>
24#include <time.h>
25
26#include <drizzled/common_fwd.h>
27
28namespace drizzled {
29
30extern uint64_t log_10_int[20];
31extern unsigned char days_in_month[];
32
33/* Time handling defaults */
34#define TIMESTAMP_MIN_YEAR (1900 + YY_PART_YEAR - 1)
35#define TIMESTAMP_MAX_VALUE INT32_MAX
36#define TIMESTAMP_MIN_VALUE 1
37
38/* two-digit years < this are 20..; >= this are 19.. */
39#define YY_PART_YEAR 70
40
41/* Flags to str_to_datetime */
42#define TIME_FUZZY_DATE 1
43#define TIME_DATETIME_ONLY 2
44
45/* Must be same as MODE_NO_ZERO_IN_DATE */
46#define TIME_NO_ZERO_IN_DATE (65536L*2*2*2*2*2*2*2)
47
48/* Must be same as MODE_NO_ZERO_DATE */
49#define TIME_NO_ZERO_DATE (TIME_NO_ZERO_IN_DATE*2)
50#define TIME_INVALID_DATES (TIME_NO_ZERO_DATE*2)
51
52#define DRIZZLE_TIME_WARN_TRUNCATED 1
53#define DRIZZLE_TIME_WARN_OUT_OF_RANGE 2
54
55/* Limits for the TIME data type */
56#define TIME_MAX_HOUR 838
57#define TIME_MAX_MINUTE 59
58#define TIME_MAX_SECOND 59
59#define TIME_MAX_VALUE (TIME_MAX_HOUR*10000 + TIME_MAX_MINUTE*100 + \
60 TIME_MAX_SECOND)
61
62/*
63 Structure which is used to represent datetime values inside Drizzle.
64
65 We assume that values in this structure are normalized, i.e. year <= 9999,
66 month <= 12, day <= 31, hour <= 23, hour <= 59, hour <= 59. Many functions
67 in server such as my_system_gmt_sec() or make_time() family of functions
68 rely on this (actually now usage of make_*() family relies on a bit weaker
69 restriction). Also functions that produce type::Time as result ensure this.
70 There is one exception to this rule though if this structure holds time
71 value (time_type == DRIZZLE_TIMESTAMP_TIME) days and hour member can hold
72 bigger values.
73*/
74namespace type {
75
76enum timestamp_t
77{
78 DRIZZLE_TIMESTAMP_NONE= -2, DRIZZLE_TIMESTAMP_ERROR= -1,
79 DRIZZLE_TIMESTAMP_DATE= 0, DRIZZLE_TIMESTAMP_DATETIME= 1, DRIZZLE_TIMESTAMP_TIME= 2
80};
81
82enum cut_t
83{
84 VALID= 0,
85 CUT= 1,
86 INVALID= 2
87};
88
89/*
90 datatime_t while being stored in an integer is actually a formatted value.
91*/
92
93inline bool is_valid(datetime_t value)
94{
95 return value != -1;
96}
97
98class Time
99{
100public:
101 Time()
102 {
103 reset();
104 }
105
106 Time(uint32_t year_arg,
107 uint32_t month_arg,
108 uint32_t day_arg,
109 uint32_t hour_arg,
110 uint32_t minute_arg,
111 uint32_t second_arg,
112 usec_t second_part_arg,
113 timestamp_t type_arg) :
114 year(year_arg),
115 month(month_arg),
116 day(day_arg),
117 hour(hour_arg),
118 minute(minute_arg),
119 second(second_arg),
120 second_part(second_part_arg),
121 neg(false),
122 time_type(type_arg)
123 {
124 }
125
126 Time(uint32_t hour_arg,
127 uint32_t minute_arg,
128 uint32_t second_arg,
129 usec_t second_part_arg,
130 bool neg_arg) :
131 year(0),
132 month(0),
133 day(0),
134 hour(hour_arg),
135 minute(minute_arg),
136 second(second_arg),
137 second_part(second_part_arg),
138 neg(neg_arg),
139 time_type(DRIZZLE_TIMESTAMP_TIME)
140 {
141 }
142
143 uint32_t year, month, day, hour, minute, second;
144 usec_t second_part;
145 bool neg;
146 timestamp_t time_type;
147
148 void reset()
149 {
150 year= month= day= hour= minute= second= second_part= 0;
151 neg= false;
152 time_type= DRIZZLE_TIMESTAMP_DATE;
153 }
154
155 timestamp_t type() const
156 {
157 return time_type;
158 }
159
160 void convert(drizzled::String &str, timestamp_t arg= type::DRIZZLE_TIMESTAMP_DATETIME);
161 void convert(char *str, size_t &to_length, timestamp_t arg= type::DRIZZLE_TIMESTAMP_DATETIME);
162 void convert(datetime_t &datetime, timestamp_t arg= type::DRIZZLE_TIMESTAMP_DATETIME);
163 void convert(datetime_t &ret, int64_t nr, uint32_t flags);
164 void convert(datetime_t &ret, int64_t nr, uint32_t flags, type::cut_t &was_cut);
165 void convert(type::epoch_t &epoch, long *my_timezone) const;
166
167 void truncate(const timestamp_t arg);
168
169 bool store(const char *str,uint32_t length, int &warning, type::timestamp_t arg= DRIZZLE_TIMESTAMP_TIME);
170 type::timestamp_t store(const char *str, uint32_t length, uint32_t flags, type::cut_t &was_cut);
171 type::timestamp_t store(const char *str, uint32_t length, uint32_t flags);
172 void store(type::epoch_t from);
173 void store(type::epoch_t from, usec_t from_fractional_seconds);
174 void store(const tm&);
175 void store(const timeval&);
176
177
178 static const uint32_t FRACTIONAL_DIGITS= 1000000;
179 static const size_t MAX_STRING_LENGTH= 32; // +32 to make my_snprintf_{8bit|ucs2} happy
180
181 bool check(bool not_zero_date, uint32_t flags, type::cut_t &was_cut) const;
182
183 inline bool isValidEpoch() const
184 {
185 if ((year < TIMESTAMP_MIN_YEAR) or (year == TIMESTAMP_MIN_YEAR && (month < 12 || day < 31)))
186 {
187 return false;
188 }
189
190 return true;
191 }
192};
193
194}
195
196long calc_daynr(uint32_t year,uint32_t month,uint32_t day);
197uint32_t calc_days_in_year(uint32_t year);
198uint32_t year_2000_handling(uint32_t year);
199
200void init_time();
201
202/*
203 Available interval types used in any statement.
204
205 'interval_type' must be sorted so that simple intervals comes first,
206 ie year, quarter, month, week, day, hour, etc. The order based on
207 interval size is also important and the intervals should be kept in a
208 large to smaller order. (get_interval_value() depends on this)
209
210 Note: If you change the order of elements in this enum you should fix
211 order of elements in 'interval_type_to_name' and 'interval_names'
212 arrays
213
214 See also interval_type_to_name, get_interval_value, interval_names
215*/
216
217enum interval_type
218{
219 INTERVAL_YEAR, INTERVAL_QUARTER, INTERVAL_MONTH, INTERVAL_WEEK, INTERVAL_DAY,
220 INTERVAL_HOUR, INTERVAL_MINUTE, INTERVAL_SECOND, INTERVAL_MICROSECOND,
221 INTERVAL_YEAR_MONTH, INTERVAL_DAY_HOUR, INTERVAL_DAY_MINUTE,
222 INTERVAL_DAY_SECOND, INTERVAL_HOUR_MINUTE, INTERVAL_HOUR_SECOND,
223 INTERVAL_MINUTE_SECOND, INTERVAL_DAY_MICROSECOND, INTERVAL_HOUR_MICROSECOND,
224 INTERVAL_MINUTE_MICROSECOND, INTERVAL_SECOND_MICROSECOND, INTERVAL_LAST
225};
226
227} /* namespace drizzled */
bool check(bool not_zero_date, uint32_t flags, type::cut_t &was_cut) const
Check datetime value for validity according to flags.
Definition type/time.cc:97
Convert from decimal to binary representation.
Definition common_fwd.h:215
TODO: Rename this file - func.h is stupid.