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 |
*
|
|
1999.6.1
by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file |
4 |
* Copyright (C) 2008-2009 Sun Microsystems, Inc.
|
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 |
||
2173.2.1
by Monty Taylor
Fixes incorrect usage of include |
23 |
#include <config.h> |
24 |
#include <drizzled/error.h> |
|
25 |
#include <drizzled/util/test.h> |
|
26 |
#include <drizzled/session.h> |
|
27 |
#include <drizzled/time_functions.h> |
|
2281.5.1
by Muhammad Umair
Merged charset declarations of global_charset_info.h and charset_info.h into charset.h header file. |
28 |
#include <drizzled/charset.h> |
2241.3.2
by Olaf van der Spek
Refactor Session::variables |
29 |
#include <drizzled/system_variables.h> |
2318.6.113
by Olaf van der Spek
Add include |
30 |
#include <drizzled/sql_string.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. |
31 |
|
2241.2.14
by Olaf van der Spek
Refactor |
32 |
namespace drizzled { |
1280.1.10
by Monty Taylor
Put everything in drizzled into drizzled namespace. |
33 |
|
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. |
34 |
/* Some functions to calculate dates */
|
1
by brian
clean slate |
35 |
|
36 |
||
37 |
int calc_weekday(long daynr,bool sunday_first_day_of_week) |
|
38 |
{
|
|
51.1.71
by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols |
39 |
return ((int) ((daynr + 5L + (sunday_first_day_of_week ? 1L : 0L)) % 7)); |
1
by brian
clean slate |
40 |
}
|
41 |
||
42 |
||
2030.1.5
by Brian Aker
Update for moving DRIZZLE_TIME to type::Time |
43 |
uint32_t calc_week(type::Time *l_time, uint32_t week_behaviour, uint32_t *year) |
1
by brian
clean slate |
44 |
{
|
482
by Brian Aker
Remove uint. |
45 |
uint32_t days; |
520.1.7
by Brian Aker
More ulong fixes (including a bug on row return on error) |
46 |
uint32_t daynr= calc_daynr(l_time->year,l_time->month,l_time->day); |
47 |
uint32_t first_daynr= calc_daynr(l_time->year,1,1); |
|
1
by brian
clean slate |
48 |
bool monday_first= test(week_behaviour & WEEK_MONDAY_FIRST); |
49 |
bool week_year= test(week_behaviour & WEEK_YEAR); |
|
50 |
bool first_weekday= test(week_behaviour & WEEK_FIRST_WEEKDAY); |
|
51 |
||
1237.9.4
by Padraig O'Sullivan
Removed the inclusion of drizzled/field.h in the server_includes header file. |
52 |
uint32_t weekday= calc_weekday(first_daynr, !monday_first); |
1
by brian
clean slate |
53 |
*year=l_time->year; |
54 |
||
55 |
if (l_time->month == 1 && l_time->day <= 7-weekday) |
|
56 |
{
|
|
57 |
if ((!week_year) && ((first_weekday && weekday != 0) || (!first_weekday && weekday >= 4))) |
|
58 |
return 0; |
|
59 |
week_year= 1; |
|
60 |
(*year)--; |
|
61 |
first_daynr-= (days=calc_days_in_year(*year)); |
|
62 |
weekday= (weekday + 53*7- days) % 7; |
|
63 |
}
|
|
64 |
||
65 |
if ((first_weekday && weekday != 0) || |
|
66 |
(!first_weekday && weekday >= 4)) |
|
67 |
days= daynr - (first_daynr+ (7-weekday)); |
|
68 |
else
|
|
69 |
days= daynr - (first_daynr - weekday); |
|
70 |
||
71 |
if (week_year && days >= 52*7) |
|
72 |
{
|
|
73 |
weekday= (weekday + calc_days_in_year(*year)) % 7; |
|
74 |
if ((!first_weekday && weekday < 4) || (first_weekday && weekday == 0)) |
|
75 |
{
|
|
76 |
(*year)++; |
|
77 |
return 1; |
|
78 |
}
|
|
79 |
}
|
|
80 |
return days/7+1; |
|
81 |
}
|
|
82 |
||
83 |
||
1237.9.4
by Padraig O'Sullivan
Removed the inclusion of drizzled/field.h in the server_includes header file. |
84 |
void get_date_from_daynr(long daynr, |
85 |
uint32_t *ret_year, |
|
86 |
uint32_t *ret_month, |
|
87 |
uint32_t *ret_day) |
|
1
by brian
clean slate |
88 |
{
|
482
by Brian Aker
Remove uint. |
89 |
uint32_t year,temp,leap_day,day_of_year,days_in_year; |
481
by Brian Aker
Remove all of uchar. |
90 |
unsigned char *month_pos; |
1
by brian
clean slate |
91 |
|
92 |
if (daynr <= 365L || daynr >= 3652500) |
|
93 |
{ /* Fix if wrong daynr */ |
|
94 |
*ret_year= *ret_month = *ret_day =0; |
|
95 |
}
|
|
96 |
else
|
|
97 |
{
|
|
895
by Brian Aker
Completion (?) of uint conversion. |
98 |
year= (uint32_t) (daynr*100 / 36525L); |
1
by brian
clean slate |
99 |
temp=(((year-1)/100+1)*3)/4; |
895
by Brian Aker
Completion (?) of uint conversion. |
100 |
day_of_year=(uint32_t) (daynr - (long) year * 365L) - (year-1)/4 +temp; |
1
by brian
clean slate |
101 |
while (day_of_year > (days_in_year= calc_days_in_year(year))) |
102 |
{
|
|
103 |
day_of_year-=days_in_year; |
|
104 |
(year)++; |
|
105 |
}
|
|
106 |
leap_day=0; |
|
107 |
if (days_in_year == 366) |
|
108 |
{
|
|
109 |
if (day_of_year > 31+28) |
|
110 |
{
|
|
2088.8.6
by Brian Aker
Format |
111 |
day_of_year--; |
112 |
if (day_of_year == 31+28) |
|
113 |
leap_day=1; /* Handle leapyears leapday */ |
|
1
by brian
clean slate |
114 |
}
|
115 |
}
|
|
116 |
*ret_month=1; |
|
117 |
for (month_pos= days_in_month ; |
|
2088.8.6
by Brian Aker
Format |
118 |
day_of_year > (uint32_t) *month_pos ; |
119 |
day_of_year-= *(month_pos++), (*ret_month)++) |
|
1
by brian
clean slate |
120 |
;
|
121 |
*ret_year=year; |
|
122 |
*ret_day=day_of_year+leap_day; |
|
123 |
}
|
|
51.1.71
by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols |
124 |
return; |
1
by brian
clean slate |
125 |
}
|
126 |
||
127 |
||
2440.2.23
by Olaf van der Spek
Use str_ref |
128 |
type::timestamp_t str_to_datetime_with_warn(Session& session, str_ref str, type::Time& l_time, uint32_t flags) |
1
by brian
clean slate |
129 |
{
|
2104.2.6
by Brian Aker
Enum was_cut |
130 |
type::cut_t was_cut= type::VALID; |
2440.2.23
by Olaf van der Spek
Use str_ref |
131 |
type::timestamp_t ts_type= l_time.store(str.data(), str.size(), (flags | (session.variables.sql_mode & (MODE_INVALID_DATES | MODE_NO_ZERO_DATE))), was_cut); |
2088.8.9
by Brian Aker
Merge in namespace of enum. |
132 |
if (was_cut || ts_type <= type::DRIZZLE_TIMESTAMP_ERROR) |
2440.2.23
by Olaf van der Spek
Use str_ref |
133 |
make_truncated_value_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, str, ts_type, NULL); |
1
by brian
clean slate |
134 |
return ts_type; |
135 |
}
|
|
136 |
||
137 |
||
2440.2.23
by Olaf van der Spek
Use str_ref |
138 |
bool str_to_time_with_warn(Session& session, str_ref str, type::Time& l_time) |
1
by brian
clean slate |
139 |
{
|
140 |
int warning; |
|
2440.2.23
by Olaf van der Spek
Use str_ref |
141 |
bool ret_val= l_time.store(str.data(), str.size(), warning, type::DRIZZLE_TIMESTAMP_TIME); |
1
by brian
clean slate |
142 |
if (ret_val || warning) |
2440.2.23
by Olaf van der Spek
Use str_ref |
143 |
make_truncated_value_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, str, type::DRIZZLE_TIMESTAMP_TIME, NULL); |
1
by brian
clean slate |
144 |
return ret_val; |
145 |
}
|
|
146 |
||
147 |
||
2440.2.23
by Olaf van der Spek
Use str_ref |
148 |
void make_truncated_value_warning(Session& session, DRIZZLE_ERROR::enum_warning_level level, str_ref str_arg, type::timestamp_t time_type, const char *field_name) |
1
by brian
clean slate |
149 |
{
|
261.4.1
by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR. |
150 |
char warn_buff[DRIZZLE_ERRMSG_SIZE]; |
2254
by Brian Aker
Shift CHARSET_INFO to charset_info_st |
151 |
charset_info_st *cs= &my_charset_utf8_general_ci; |
1
by brian
clean slate |
152 |
char buff[128]; |
205
by Brian Aker
uint32 -> uin32_t |
153 |
String str(buff,(uint32_t) sizeof(buff), system_charset_info); |
2440.2.23
by Olaf van der Spek
Use str_ref |
154 |
str.copy(str_arg.data(), str_arg.size(), system_charset_info); |
155 |
assert(not str[str_arg.size()]); // Ensure we have end 0 for snprintf |
|
1
by brian
clean slate |
156 |
|
2440.2.23
by Olaf van der Spek
Use str_ref |
157 |
const char *type_str; |
158 |
switch (time_type) |
|
159 |
{
|
|
2088.8.9
by Brian Aker
Merge in namespace of enum. |
160 |
case type::DRIZZLE_TIMESTAMP_DATE: |
2088.8.6
by Brian Aker
Format |
161 |
type_str= "date"; |
162 |
break; |
|
2088.8.9
by Brian Aker
Merge in namespace of enum. |
163 |
|
164 |
case type::DRIZZLE_TIMESTAMP_TIME: |
|
2088.8.6
by Brian Aker
Format |
165 |
type_str= "time"; |
166 |
break; |
|
2088.8.9
by Brian Aker
Merge in namespace of enum. |
167 |
|
168 |
case type::DRIZZLE_TIMESTAMP_DATETIME: // FALLTHROUGH |
|
2088.8.6
by Brian Aker
Format |
169 |
default: |
170 |
type_str= "datetime"; |
|
1
by brian
clean slate |
171 |
}
|
2069.2.2
by Brian Aker
Small refactoring around type::Time |
172 |
|
1
by brian
clean slate |
173 |
if (field_name) |
1578.4.12
by Brian Aker
This removes a couple of additional uses of current_session. |
174 |
{
|
1
by brian
clean slate |
175 |
cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff), |
176 |
ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD), |
|
177 |
type_str, str.c_ptr(), field_name, |
|
2440.2.23
by Olaf van der Spek
Use str_ref |
178 |
(uint32_t) session.row_count); |
179 |
}
|
|
180 |
else if (time_type > type::DRIZZLE_TIMESTAMP_ERROR) |
|
181 |
{
|
|
182 |
cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff), ER(ER_TRUNCATED_WRONG_VALUE), type_str, str.c_ptr()); |
|
1578.4.12
by Brian Aker
This removes a couple of additional uses of current_session. |
183 |
}
|
1
by brian
clean slate |
184 |
else
|
185 |
{
|
|
2440.2.23
by Olaf van der Spek
Use str_ref |
186 |
cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff), ER(ER_WRONG_VALUE), type_str, str.c_ptr()); |
1
by brian
clean slate |
187 |
}
|
2440.2.23
by Olaf van der Spek
Use str_ref |
188 |
push_warning(&session, level, ER_TRUNCATED_WRONG_VALUE, warn_buff); |
1
by brian
clean slate |
189 |
}
|
190 |
||
191 |
||
192 |
bool
|
|
2030.1.5
by Brian Aker
Update for moving DRIZZLE_TIME to type::Time |
193 |
calc_time_diff(type::Time *l_time1, type::Time *l_time2, int l_sign, int64_t *seconds_out, |
1
by brian
clean slate |
194 |
long *microseconds_out) |
195 |
{
|
|
196 |
long days; |
|
197 |
bool neg; |
|
152
by Brian Aker
longlong replacement |
198 |
int64_t microseconds; |
1
by brian
clean slate |
199 |
|
200 |
/*
|
|
236.1.24
by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME. |
201 |
We suppose that if first argument is DRIZZLE_TIMESTAMP_TIME
|
1
by brian
clean slate |
202 |
the second argument should be TIMESTAMP_TIME also.
|
203 |
We should check it before calc_time_diff call.
|
|
204 |
*/
|
|
2088.8.9
by Brian Aker
Merge in namespace of enum. |
205 |
if (l_time1->time_type == type::DRIZZLE_TIMESTAMP_TIME) // Time value |
1
by brian
clean slate |
206 |
days= (long)l_time1->day - l_sign * (long)l_time2->day; |
207 |
else
|
|
208 |
{
|
|
895
by Brian Aker
Completion (?) of uint conversion. |
209 |
days= calc_daynr((uint32_t) l_time1->year, |
2088.8.6
by Brian Aker
Format |
210 |
(uint32_t) l_time1->month, |
211 |
(uint32_t) l_time1->day); |
|
2088.8.9
by Brian Aker
Merge in namespace of enum. |
212 |
if (l_time2->time_type == type::DRIZZLE_TIMESTAMP_TIME) |
1
by brian
clean slate |
213 |
days-= l_sign * (long)l_time2->day; |
214 |
else
|
|
895
by Brian Aker
Completion (?) of uint conversion. |
215 |
days-= l_sign*calc_daynr((uint32_t) l_time2->year, |
2088.8.6
by Brian Aker
Format |
216 |
(uint32_t) l_time2->month, |
217 |
(uint32_t) l_time2->day); |
|
1
by brian
clean slate |
218 |
}
|
219 |
||
398.1.8
by Monty Taylor
Enabled -Wlong-long. |
220 |
microseconds= ((int64_t)days*86400L + |
152
by Brian Aker
longlong replacement |
221 |
(int64_t)(l_time1->hour*3600L + |
2088.8.6
by Brian Aker
Format |
222 |
l_time1->minute*60L + |
223 |
l_time1->second) - |
|
152
by Brian Aker
longlong replacement |
224 |
l_sign*(int64_t)(l_time2->hour*3600L + |
2088.8.6
by Brian Aker
Format |
225 |
l_time2->minute*60L + |
226 |
l_time2->second)) * 1000000L + |
|
227 |
(int64_t)l_time1->second_part - |
|
228 |
l_sign*(int64_t)l_time2->second_part; |
|
1
by brian
clean slate |
229 |
|
230 |
neg= 0; |
|
231 |
if (microseconds < 0) |
|
232 |
{
|
|
233 |
microseconds= -microseconds; |
|
234 |
neg= 1; |
|
235 |
}
|
|
236 |
*seconds_out= microseconds/1000000L; |
|
237 |
*microseconds_out= (long) (microseconds%1000000L); |
|
238 |
return neg; |
|
239 |
}
|
|
240 |
||
1280.1.10
by Monty Taylor
Put everything in drizzled into drizzled namespace. |
241 |
} /* namespace drizzled */ |