520.4.14
by Monty Taylor
Removed korr.h and tztime.h from common_includes. Also removed the HAVE_DTRACE block and stuck it in autoconf. |
1 |
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
|
2 |
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
|
|
3 |
*
|
|
1237.9.4
by Padraig O'Sullivan
Removed the inclusion of drizzled/field.h in the server_includes header file. |
4 |
* Copyright (C) 2008-2009 Sun Microsystems
|
520.4.14
by Monty Taylor
Removed korr.h and tztime.h from common_includes. Also removed the HAVE_DTRACE block and stuck it in autoconf. |
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; version 2 of the License.
|
|
9 |
*
|
|
10 |
* This program is distributed in the hope that it will be useful,
|
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 |
* GNU General Public License for more details.
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License
|
|
16 |
* along with this program; if not, write to the Free Software
|
|
17 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
18 |
*/
|
|
1
by brian
clean slate |
19 |
|
20 |
||
21 |
/* Functions to handle date and time */
|
|
22 |
||
1241.9.36
by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h. |
23 |
#include "config.h" |
1237.9.4
by Padraig O'Sullivan
Removed the inclusion of drizzled/field.h in the server_includes header file. |
24 |
#include "drizzled/error.h" |
25 |
#include "drizzled/util/test.h" |
|
26 |
#include "drizzled/tztime.h" |
|
27 |
#include "drizzled/session.h" |
|
1237.9.8
by Monty Taylor
Fixed solaris build. |
28 |
#include "drizzled/time_functions.h" |
520.4.14
by Monty Taylor
Removed korr.h and tztime.h from common_includes. Also removed the HAVE_DTRACE block and stuck it in autoconf. |
29 |
|
1280.1.10
by Monty Taylor
Put everything in drizzled into drizzled namespace. |
30 |
namespace drizzled |
31 |
{
|
|
32 |
||
520.4.14
by Monty Taylor
Removed korr.h and tztime.h from common_includes. Also removed the HAVE_DTRACE block and stuck it in autoconf. |
33 |
/* Some functions to calculate dates */
|
1
by brian
clean slate |
34 |
|
35 |
||
36 |
int calc_weekday(long daynr,bool sunday_first_day_of_week) |
|
37 |
{
|
|
51.1.71
by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols |
38 |
return ((int) ((daynr + 5L + (sunday_first_day_of_week ? 1L : 0L)) % 7)); |
1
by brian
clean slate |
39 |
}
|
40 |
||
41 |
||
482
by Brian Aker
Remove uint. |
42 |
uint32_t calc_week(DRIZZLE_TIME *l_time, uint32_t week_behaviour, uint32_t *year) |
1
by brian
clean slate |
43 |
{
|
482
by Brian Aker
Remove uint. |
44 |
uint32_t days; |
520.1.7
by Brian Aker
More ulong fixes (including a bug on row return on error) |
45 |
uint32_t daynr= calc_daynr(l_time->year,l_time->month,l_time->day); |
46 |
uint32_t first_daynr= calc_daynr(l_time->year,1,1); |
|
1
by brian
clean slate |
47 |
bool monday_first= test(week_behaviour & WEEK_MONDAY_FIRST); |
48 |
bool week_year= test(week_behaviour & WEEK_YEAR); |
|
49 |
bool first_weekday= test(week_behaviour & WEEK_FIRST_WEEKDAY); |
|
50 |
||
1237.9.4
by Padraig O'Sullivan
Removed the inclusion of drizzled/field.h in the server_includes header file. |
51 |
uint32_t weekday= calc_weekday(first_daynr, !monday_first); |
1
by brian
clean slate |
52 |
*year=l_time->year; |
53 |
||
54 |
if (l_time->month == 1 && l_time->day <= 7-weekday) |
|
55 |
{
|
|
56 |
if ((!week_year) && ((first_weekday && weekday != 0) || (!first_weekday && weekday >= 4))) |
|
57 |
return 0; |
|
58 |
week_year= 1; |
|
59 |
(*year)--; |
|
60 |
first_daynr-= (days=calc_days_in_year(*year)); |
|
61 |
weekday= (weekday + 53*7- days) % 7; |
|
62 |
}
|
|
63 |
||
64 |
if ((first_weekday && weekday != 0) || |
|
65 |
(!first_weekday && weekday >= 4)) |
|
66 |
days= daynr - (first_daynr+ (7-weekday)); |
|
67 |
else
|
|
68 |
days= daynr - (first_daynr - weekday); |
|
69 |
||
70 |
if (week_year && days >= 52*7) |
|
71 |
{
|
|
72 |
weekday= (weekday + calc_days_in_year(*year)) % 7; |
|
73 |
if ((!first_weekday && weekday < 4) || (first_weekday && weekday == 0)) |
|
74 |
{
|
|
75 |
(*year)++; |
|
76 |
return 1; |
|
77 |
}
|
|
78 |
}
|
|
79 |
return days/7+1; |
|
80 |
}
|
|
81 |
||
82 |
||
1237.9.4
by Padraig O'Sullivan
Removed the inclusion of drizzled/field.h in the server_includes header file. |
83 |
void get_date_from_daynr(long daynr, |
84 |
uint32_t *ret_year, |
|
85 |
uint32_t *ret_month, |
|
86 |
uint32_t *ret_day) |
|
1
by brian
clean slate |
87 |
{
|
482
by Brian Aker
Remove uint. |
88 |
uint32_t year,temp,leap_day,day_of_year,days_in_year; |
481
by Brian Aker
Remove all of uchar. |
89 |
unsigned char *month_pos; |
1
by brian
clean slate |
90 |
|
91 |
if (daynr <= 365L || daynr >= 3652500) |
|
92 |
{ /* Fix if wrong daynr */ |
|
93 |
*ret_year= *ret_month = *ret_day =0; |
|
94 |
}
|
|
95 |
else
|
|
96 |
{
|
|
895
by Brian Aker
Completion (?) of uint conversion. |
97 |
year= (uint32_t) (daynr*100 / 36525L); |
1
by brian
clean slate |
98 |
temp=(((year-1)/100+1)*3)/4; |
895
by Brian Aker
Completion (?) of uint conversion. |
99 |
day_of_year=(uint32_t) (daynr - (long) year * 365L) - (year-1)/4 +temp; |
1
by brian
clean slate |
100 |
while (day_of_year > (days_in_year= calc_days_in_year(year))) |
101 |
{
|
|
102 |
day_of_year-=days_in_year; |
|
103 |
(year)++; |
|
104 |
}
|
|
105 |
leap_day=0; |
|
106 |
if (days_in_year == 366) |
|
107 |
{
|
|
108 |
if (day_of_year > 31+28) |
|
109 |
{
|
|
110 |
day_of_year--; |
|
111 |
if (day_of_year == 31+28) |
|
112 |
leap_day=1; /* Handle leapyears leapday */ |
|
113 |
}
|
|
114 |
}
|
|
115 |
*ret_month=1; |
|
116 |
for (month_pos= days_in_month ; |
|
895
by Brian Aker
Completion (?) of uint conversion. |
117 |
day_of_year > (uint32_t) *month_pos ; |
1
by brian
clean slate |
118 |
day_of_year-= *(month_pos++), (*ret_month)++) |
119 |
;
|
|
120 |
*ret_year=year; |
|
121 |
*ret_day=day_of_year+leap_day; |
|
122 |
}
|
|
51.1.71
by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols |
123 |
return; |
1
by brian
clean slate |
124 |
}
|
125 |
||
126 |
||
398.1.1
by Monty Taylor
Remove typedef enum enum_drizzle_timestamp_type timestamp_type; |
127 |
enum enum_drizzle_timestamp_type |
1237.9.4
by Padraig O'Sullivan
Removed the inclusion of drizzled/field.h in the server_includes header file. |
128 |
str_to_datetime_with_warn(const char *str, |
129 |
uint32_t length, |
|
130 |
DRIZZLE_TIME *l_time, |
|
482
by Brian Aker
Remove uint. |
131 |
uint32_t flags) |
1
by brian
clean slate |
132 |
{
|
133 |
int was_cut; |
|
1633.4.7
by Brian Aker
Put a copy of the Session in the root of function to make use of. |
134 |
enum enum_drizzle_timestamp_type ts_type; |
520.1.22
by Brian Aker
Second pass of thd cleanup |
135 |
Session *session= current_session; |
660.1.3
by Eric Herman
removed trailing whitespace with simple script: |
136 |
|
1
by brian
clean slate |
137 |
ts_type= str_to_datetime(str, length, l_time, |
520.1.22
by Brian Aker
Second pass of thd cleanup |
138 |
(flags | (session->variables.sql_mode & |
1
by brian
clean slate |
139 |
(MODE_INVALID_DATES | |
140 |
MODE_NO_ZERO_DATE))), |
|
141 |
&was_cut); |
|
236.1.24
by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME. |
142 |
if (was_cut || ts_type <= DRIZZLE_TIMESTAMP_ERROR) |
1633.4.7
by Brian Aker
Put a copy of the Session in the root of function to make use of. |
143 |
make_truncated_value_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, |
461
by Monty Taylor
Removed NullS. bu-bye. |
144 |
str, length, ts_type, NULL); |
1
by brian
clean slate |
145 |
return ts_type; |
146 |
}
|
|
147 |
||
148 |
||
149 |
bool
|
|
482
by Brian Aker
Remove uint. |
150 |
str_to_time_with_warn(const char *str, uint32_t length, DRIZZLE_TIME *l_time) |
1
by brian
clean slate |
151 |
{
|
152 |
int warning; |
|
153 |
bool ret_val= str_to_time(str, length, l_time, &warning); |
|
154 |
if (ret_val || warning) |
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
155 |
make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN, |
461
by Monty Taylor
Removed NullS. bu-bye. |
156 |
str, length, DRIZZLE_TIMESTAMP_TIME, NULL); |
1
by brian
clean slate |
157 |
return ret_val; |
158 |
}
|
|
159 |
||
160 |
||
236.1.24
by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME. |
161 |
void localtime_to_TIME(DRIZZLE_TIME *to, struct tm *from) |
1
by brian
clean slate |
162 |
{
|
163 |
to->neg=0; |
|
164 |
to->second_part=0; |
|
165 |
to->year= (int) ((from->tm_year+1900) % 10000); |
|
166 |
to->month= (int) from->tm_mon+1; |
|
167 |
to->day= (int) from->tm_mday; |
|
168 |
to->hour= (int) from->tm_hour; |
|
169 |
to->minute= (int) from->tm_min; |
|
170 |
to->second= (int) from->tm_sec; |
|
171 |
}
|
|
172 |
||
907.1.2
by Jay Pipes
Merging in old r902 temporal changes |
173 |
void make_date(const DRIZZLE_TIME *l_time, String *str) |
174 |
{
|
|
910.4.7
by Stewart Smith
ensure there is enough space allocated to String for string representation of date/time types. |
175 |
str->alloc(MAX_DATE_STRING_REP_LENGTH); |
176 |
uint32_t length= (uint32_t) my_date_to_str(l_time, str->c_ptr()); |
|
907.1.2
by Jay Pipes
Merging in old r902 temporal changes |
177 |
str->length(length); |
178 |
str->set_charset(&my_charset_bin); |
|
179 |
}
|
|
180 |
||
181 |
||
182 |
void make_datetime(const DRIZZLE_TIME *l_time, String *str) |
|
183 |
{
|
|
910.4.7
by Stewart Smith
ensure there is enough space allocated to String for string representation of date/time types. |
184 |
str->alloc(MAX_DATE_STRING_REP_LENGTH); |
185 |
uint32_t length= (uint32_t) my_datetime_to_str(l_time, str->c_ptr()); |
|
1
by brian
clean slate |
186 |
str->length(length); |
187 |
str->set_charset(&my_charset_bin); |
|
188 |
}
|
|
189 |
||
190 |
||
1237.9.4
by Padraig O'Sullivan
Removed the inclusion of drizzled/field.h in the server_includes header file. |
191 |
void make_truncated_value_warning(Session *session, |
192 |
DRIZZLE_ERROR::enum_warning_level level, |
|
1
by brian
clean slate |
193 |
const char *str_val, |
1237.9.4
by Padraig O'Sullivan
Removed the inclusion of drizzled/field.h in the server_includes header file. |
194 |
uint32_t str_length, |
398.1.1
by Monty Taylor
Remove typedef enum enum_drizzle_timestamp_type timestamp_type; |
195 |
enum enum_drizzle_timestamp_type time_type, |
1
by brian
clean slate |
196 |
const char *field_name) |
197 |
{
|
|
261.4.1
by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR. |
198 |
char warn_buff[DRIZZLE_ERRMSG_SIZE]; |
1
by brian
clean slate |
199 |
const char *type_str; |
383.1.12
by Brian Aker
Much closer toward UTF8 being around all the time... |
200 |
CHARSET_INFO *cs= &my_charset_utf8_general_ci; |
1
by brian
clean slate |
201 |
char buff[128]; |
205
by Brian Aker
uint32 -> uin32_t |
202 |
String str(buff,(uint32_t) sizeof(buff), system_charset_info); |
1
by brian
clean slate |
203 |
str.copy(str_val, str_length, system_charset_info); |
204 |
str[str_length]= 0; // Ensure we have end 0 for snprintf |
|
205 |
||
206 |
switch (time_type) { |
|
660.1.3
by Eric Herman
removed trailing whitespace with simple script: |
207 |
case DRIZZLE_TIMESTAMP_DATE: |
1
by brian
clean slate |
208 |
type_str= "date"; |
209 |
break; |
|
236.1.24
by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME. |
210 |
case DRIZZLE_TIMESTAMP_TIME: |
1
by brian
clean slate |
211 |
type_str= "time"; |
212 |
break; |
|
236.1.24
by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME. |
213 |
case DRIZZLE_TIMESTAMP_DATETIME: // FALLTHROUGH |
1
by brian
clean slate |
214 |
default: |
215 |
type_str= "datetime"; |
|
216 |
break; |
|
217 |
}
|
|
218 |
if (field_name) |
|
1578.4.12
by Brian Aker
This removes a couple of additional uses of current_session. |
219 |
{
|
1
by brian
clean slate |
220 |
cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff), |
221 |
ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD), |
|
222 |
type_str, str.c_ptr(), field_name, |
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
223 |
(uint32_t) session->row_count); |
1578.4.12
by Brian Aker
This removes a couple of additional uses of current_session. |
224 |
}
|
1
by brian
clean slate |
225 |
else
|
226 |
{
|
|
236.1.24
by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME. |
227 |
if (time_type > DRIZZLE_TIMESTAMP_ERROR) |
1
by brian
clean slate |
228 |
cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff), |
229 |
ER(ER_TRUNCATED_WRONG_VALUE), |
|
230 |
type_str, str.c_ptr()); |
|
231 |
else
|
|
232 |
cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff), |
|
233 |
ER(ER_WRONG_VALUE), type_str, str.c_ptr()); |
|
234 |
}
|
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
235 |
push_warning(session, level, |
1
by brian
clean slate |
236 |
ER_TRUNCATED_WRONG_VALUE, warn_buff); |
237 |
}
|
|
238 |
||
239 |
||
240 |
bool
|
|
236.1.24
by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME. |
241 |
calc_time_diff(DRIZZLE_TIME *l_time1, DRIZZLE_TIME *l_time2, int l_sign, int64_t *seconds_out, |
1
by brian
clean slate |
242 |
long *microseconds_out) |
243 |
{
|
|
244 |
long days; |
|
245 |
bool neg; |
|
152
by Brian Aker
longlong replacement |
246 |
int64_t microseconds; |
1
by brian
clean slate |
247 |
|
248 |
/*
|
|
236.1.24
by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME. |
249 |
We suppose that if first argument is DRIZZLE_TIMESTAMP_TIME
|
1
by brian
clean slate |
250 |
the second argument should be TIMESTAMP_TIME also.
|
251 |
We should check it before calc_time_diff call.
|
|
252 |
*/
|
|
236.1.24
by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME. |
253 |
if (l_time1->time_type == DRIZZLE_TIMESTAMP_TIME) // Time value |
1
by brian
clean slate |
254 |
days= (long)l_time1->day - l_sign * (long)l_time2->day; |
255 |
else
|
|
256 |
{
|
|
895
by Brian Aker
Completion (?) of uint conversion. |
257 |
days= calc_daynr((uint32_t) l_time1->year, |
258 |
(uint32_t) l_time1->month, |
|
259 |
(uint32_t) l_time1->day); |
|
236.1.24
by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME. |
260 |
if (l_time2->time_type == DRIZZLE_TIMESTAMP_TIME) |
1
by brian
clean slate |
261 |
days-= l_sign * (long)l_time2->day; |
262 |
else
|
|
895
by Brian Aker
Completion (?) of uint conversion. |
263 |
days-= l_sign*calc_daynr((uint32_t) l_time2->year, |
264 |
(uint32_t) l_time2->month, |
|
265 |
(uint32_t) l_time2->day); |
|
1
by brian
clean slate |
266 |
}
|
267 |
||
398.1.8
by Monty Taylor
Enabled -Wlong-long. |
268 |
microseconds= ((int64_t)days*86400L + |
152
by Brian Aker
longlong replacement |
269 |
(int64_t)(l_time1->hour*3600L + |
1
by brian
clean slate |
270 |
l_time1->minute*60L + |
271 |
l_time1->second) - |
|
152
by Brian Aker
longlong replacement |
272 |
l_sign*(int64_t)(l_time2->hour*3600L + |
1
by brian
clean slate |
273 |
l_time2->minute*60L + |
398.1.8
by Monty Taylor
Enabled -Wlong-long. |
274 |
l_time2->second)) * 1000000L + |
152
by Brian Aker
longlong replacement |
275 |
(int64_t)l_time1->second_part - |
276 |
l_sign*(int64_t)l_time2->second_part; |
|
1
by brian
clean slate |
277 |
|
278 |
neg= 0; |
|
279 |
if (microseconds < 0) |
|
280 |
{
|
|
281 |
microseconds= -microseconds; |
|
282 |
neg= 1; |
|
283 |
}
|
|
284 |
*seconds_out= microseconds/1000000L; |
|
285 |
*microseconds_out= (long) (microseconds%1000000L); |
|
286 |
return neg; |
|
287 |
}
|
|
288 |
||
1280.1.10
by Monty Taylor
Put everything in drizzled into drizzled namespace. |
289 |
} /* namespace drizzled */ |