Drizzled Public API Documentation

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