MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
my_time.h
1 /* Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 /*
17  This is a private header of sql-common library, containing
18  declarations for my_time.c
19 */
20 
21 #ifndef _my_time_h_
22 #define _my_time_h_
23 #include "my_global.h"
24 #include "mysql_time.h"
25 
26 C_MODE_START
27 
28 extern ulonglong log_10_int[20];
29 extern uchar days_in_month[];
30 extern const char my_zero_datetime6[]; /* "0000-00-00 00:00:00.000000" */
31 
32 /*
33  Portable time_t replacement.
34  Should be signed and hold seconds for 1902 -- 2038-01-19 range
35  i.e at least a 32bit variable
36 
37  Using the system built in time_t is not an option as
38  we rely on the above requirements in the time functions
39 */
40 typedef long my_time_t;
41 
42 #define MY_TIME_T_MAX LONG_MAX
43 #define MY_TIME_T_MIN LONG_MIN
44 
45 #define DATETIME_MAX_DECIMALS 6
46 
47 /* Time handling defaults */
48 #define TIMESTAMP_MAX_YEAR 2038
49 #define TIMESTAMP_MIN_YEAR (1900 + YY_PART_YEAR - 1)
50 #define TIMESTAMP_MAX_VALUE INT_MAX32
51 #define TIMESTAMP_MIN_VALUE 1
52 
53 /* two-digit years < this are 20..; >= this are 19.. */
54 #define YY_PART_YEAR 70
55 
56 /*
57  check for valid times only if the range of time_t is greater than
58  the range of my_time_t
59 */
60 #if SIZEOF_TIME_T > 4 || defined(TIME_T_UNSIGNED)
61 # define IS_TIME_T_VALID_FOR_TIMESTAMP(x) \
62  ((x) <= TIMESTAMP_MAX_VALUE && \
63  (x) >= TIMESTAMP_MIN_VALUE)
64 #else
65 # define IS_TIME_T_VALID_FOR_TIMESTAMP(x) \
66  ((x) >= TIMESTAMP_MIN_VALUE)
67 #endif
68 
69 /* Flags to str_to_datetime and number_to_datetime */
70 #define TIME_FUZZY_DATE 1
71 #define TIME_DATETIME_ONLY 2
72 #define TIME_NO_NSEC_ROUNDING 4
73 #define TIME_NO_DATE_FRAC_WARN 8
74 
75 /* Must be same as MODE_NO_ZERO_IN_DATE */
76 #define TIME_NO_ZERO_IN_DATE (65536L*2*2*2*2*2*2*2)
77 /* Must be same as MODE_NO_ZERO_DATE */
78 #define TIME_NO_ZERO_DATE (TIME_NO_ZERO_IN_DATE*2)
79 #define TIME_INVALID_DATES (TIME_NO_ZERO_DATE*2)
80 
81 /* Conversion warnings */
82 #define MYSQL_TIME_WARN_TRUNCATED 1
83 #define MYSQL_TIME_WARN_OUT_OF_RANGE 2
84 #define MYSQL_TIME_WARN_INVALID_TIMESTAMP 4
85 #define MYSQL_TIME_WARN_ZERO_DATE 8
86 #define MYSQL_TIME_NOTE_TRUNCATED 16
87 #define MYSQL_TIME_WARN_ZERO_IN_DATE 32
88 
89 /* Usefull constants */
90 #define SECONDS_IN_24H 86400L
91 
92 /* Limits for the TIME data type */
93 #define TIME_MAX_HOUR 838
94 #define TIME_MAX_MINUTE 59
95 #define TIME_MAX_SECOND 59
96 #define TIME_MAX_VALUE (TIME_MAX_HOUR*10000 + TIME_MAX_MINUTE*100 + \
97  TIME_MAX_SECOND)
98 #define TIME_MAX_VALUE_SECONDS (TIME_MAX_HOUR * 3600L + \
99  TIME_MAX_MINUTE * 60L + TIME_MAX_SECOND)
100 
101 /*
102  Structure to return status from
103  str_to_datetime(), str_to_time(), number_to_datetime(), number_to_time()
104 */
105 typedef struct st_mysql_time_status
106 {
107  int warnings;
108  uint fractional_digits;
109  uint nanoseconds;
111 
112 static inline void my_time_status_init(MYSQL_TIME_STATUS *status)
113 {
114  status->warnings= status->fractional_digits= status->nanoseconds= 0;
115 }
116 
117 
118 my_bool check_date(const MYSQL_TIME *ltime, my_bool not_zero_date,
119  ulonglong flags, int *was_cut);
120 my_bool str_to_datetime(const char *str, uint length, MYSQL_TIME *l_time,
121  ulonglong flags, MYSQL_TIME_STATUS *status);
122 longlong number_to_datetime(longlong nr, MYSQL_TIME *time_res,
123  ulonglong flags, int *was_cut);
124 my_bool number_to_time(longlong nr, MYSQL_TIME *ltime, int *warnings);
125 ulonglong TIME_to_ulonglong_datetime(const MYSQL_TIME *);
126 ulonglong TIME_to_ulonglong_date(const MYSQL_TIME *);
127 ulonglong TIME_to_ulonglong_time(const MYSQL_TIME *);
128 ulonglong TIME_to_ulonglong(const MYSQL_TIME *);
129 
130 #define MY_PACKED_TIME_GET_INT_PART(x) ((x) >> 24)
131 #define MY_PACKED_TIME_GET_FRAC_PART(x) ((x) % (1LL << 24))
132 #define MY_PACKED_TIME_MAKE(i, f) ((((longlong) (i)) << 24) + (f))
133 #define MY_PACKED_TIME_MAKE_INT(i) ((((longlong) (i)) << 24))
134 
135 longlong year_to_longlong_datetime_packed(long year);
136 longlong TIME_to_longlong_datetime_packed(const MYSQL_TIME *);
137 longlong TIME_to_longlong_date_packed(const MYSQL_TIME *);
138 longlong TIME_to_longlong_time_packed(const MYSQL_TIME *);
139 longlong TIME_to_longlong_packed(const MYSQL_TIME *);
140 
141 void TIME_from_longlong_datetime_packed(MYSQL_TIME *ltime, longlong nr);
142 void TIME_from_longlong_time_packed(MYSQL_TIME *ltime, longlong nr);
143 void TIME_from_longlong_date_packed(MYSQL_TIME *ltime, longlong nr);
144 void TIME_set_yymmdd(MYSQL_TIME *ltime, uint yymmdd);
145 void TIME_set_hhmmss(MYSQL_TIME *ltime, uint hhmmss);
146 
147 void my_datetime_packed_to_binary(longlong nr, uchar *ptr, uint dec);
148 longlong my_datetime_packed_from_binary(const uchar *ptr, uint dec);
149 uint my_datetime_binary_length(uint dec);
150 
151 void my_time_packed_to_binary(longlong nr, uchar *ptr, uint dec);
152 longlong my_time_packed_from_binary(const uchar *ptr, uint dec);
153 uint my_time_binary_length(uint dec);
154 
155 void my_timestamp_to_binary(const struct timeval *tm, uchar *ptr, uint dec);
156 void my_timestamp_from_binary(struct timeval *tm, const uchar *ptr, uint dec);
157 uint my_timestamp_binary_length(uint dec);
158 
159 my_bool str_to_time(const char *str,uint length, MYSQL_TIME *l_time,
160  MYSQL_TIME_STATUS *status);
161 
162 my_bool check_time_mmssff_range(const MYSQL_TIME *ltime);
163 my_bool check_time_range_quick(const MYSQL_TIME *ltime);
164 my_bool check_datetime_range(const MYSQL_TIME *ltime);
165 void adjust_time_range(struct st_mysql_time *, int *warning);
166 
167 long calc_daynr(uint year,uint month,uint day);
168 uint calc_days_in_year(uint year);
169 uint year_2000_handling(uint year);
170 
171 void my_init_time(void);
172 
173 
174 /*
175  Function to check sanity of a TIMESTAMP value
176 
177  DESCRIPTION
178  Check if a given MYSQL_TIME value fits in TIMESTAMP range.
179  This function doesn't make precise check, but rather a rough
180  estimate.
181 
182  RETURN VALUES
183  TRUE The value seems sane
184  FALSE The MYSQL_TIME value is definitely out of range
185 */
186 
187 static inline my_bool validate_timestamp_range(const MYSQL_TIME *t)
188 {
189  if ((t->year > TIMESTAMP_MAX_YEAR || t->year < TIMESTAMP_MIN_YEAR) ||
190  (t->year == TIMESTAMP_MAX_YEAR && (t->month > 1 || t->day > 19)) ||
191  (t->year == TIMESTAMP_MIN_YEAR && (t->month < 12 || t->day < 31)))
192  return FALSE;
193 
194  return TRUE;
195 }
196 
197 my_time_t
198 my_system_gmt_sec(const MYSQL_TIME *t, long *my_timezone,
199  my_bool *in_dst_time_gap);
200 
201 void set_zero_time(MYSQL_TIME *tm, enum enum_mysql_timestamp_type time_type);
202 void set_max_time(MYSQL_TIME *tm, my_bool neg);
203 void set_max_hhmmss(MYSQL_TIME *tm);
204 
205 /*
206  Required buffer length for my_time_to_str, my_date_to_str,
207  my_datetime_to_str and TIME_to_string functions. Note, that the
208  caller is still responsible to check that given TIME structure
209  has values in valid ranges, otherwise size of the buffer could
210  be not enough. We also rely on the fact that even wrong values
211  sent using binary protocol fit in this buffer.
212 */
213 #define MAX_DATE_STRING_REP_LENGTH 30
214 
215 int my_time_to_str(const MYSQL_TIME *l_time, char *to, uint dec);
216 int my_date_to_str(const MYSQL_TIME *l_time, char *to);
217 int my_datetime_to_str(const MYSQL_TIME *l_time, char *to, uint dec);
218 int my_TIME_to_str(const MYSQL_TIME *l_time, char *to, uint dec);
219 
220 int my_timeval_to_str(const struct timeval *tm, char *to, uint dec);
221 
222 /*
223  Available interval types used in any statement.
224 
225  'interval_type' must be sorted so that simple intervals comes first,
226  ie year, quarter, month, week, day, hour, etc. The order based on
227  interval size is also important and the intervals should be kept in a
228  large to smaller order. (get_interval_value() depends on this)
229 
230  Note: If you change the order of elements in this enum you should fix
231  order of elements in 'interval_type_to_name' and 'interval_names'
232  arrays
233 
234  See also interval_type_to_name, get_interval_value, interval_names
235 */
236 
237 enum interval_type
238 {
239  INTERVAL_YEAR, INTERVAL_QUARTER, INTERVAL_MONTH, INTERVAL_WEEK, INTERVAL_DAY,
240  INTERVAL_HOUR, INTERVAL_MINUTE, INTERVAL_SECOND, INTERVAL_MICROSECOND,
241  INTERVAL_YEAR_MONTH, INTERVAL_DAY_HOUR, INTERVAL_DAY_MINUTE,
242  INTERVAL_DAY_SECOND, INTERVAL_HOUR_MINUTE, INTERVAL_HOUR_SECOND,
243  INTERVAL_MINUTE_SECOND, INTERVAL_DAY_MICROSECOND, INTERVAL_HOUR_MICROSECOND,
244  INTERVAL_MINUTE_MICROSECOND, INTERVAL_SECOND_MICROSECOND, INTERVAL_LAST
245 };
246 
247 C_MODE_END
248 
249 #endif /* _my_time_h_ */