1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
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.
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.
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
20
#include <drizzled/server_includes.h>
22
#include <drizzled/function/time/add_time.h>
23
#include <drizzled/function/time/make_datetime.h>
24
#include <drizzled/function/time/make_datetime_with_warn.h>
26
void Item_func_add_time::fix_length_and_dec()
28
enum_field_types arg0_field_type;
30
max_length=MAX_DATETIME_FULL_WIDTH*MY_CHARSET_BIN_MB_MAXLEN;
34
The field type for the result of an Item_func_add_time function is defined
37
- If first arg is a DRIZZLE_TYPE_DATETIME or DRIZZLE_TYPE_TIMESTAMP
38
result is DRIZZLE_TYPE_DATETIME
39
- If first arg is a DRIZZLE_TYPE_TIME result is DRIZZLE_TYPE_TIME
40
- Otherwise the result is DRIZZLE_TYPE_VARCHAR
43
cached_field_type= DRIZZLE_TYPE_VARCHAR;
44
arg0_field_type= args[0]->field_type();
45
if (arg0_field_type == DRIZZLE_TYPE_DATE ||
46
arg0_field_type == DRIZZLE_TYPE_DATETIME ||
47
arg0_field_type == DRIZZLE_TYPE_TIMESTAMP)
48
cached_field_type= DRIZZLE_TYPE_DATETIME;
49
else if (arg0_field_type == DRIZZLE_TYPE_TIME)
50
cached_field_type= DRIZZLE_TYPE_TIME;
54
ADDTIME(t,a) and SUBTIME(t,a) are time functions that calculate a
57
t: time_or_datetime_expression
60
Result: Time value or datetime value
63
String *Item_func_add_time::val_str(String *str)
66
DRIZZLE_TIME l_time1, l_time2, l_time3;
68
long days, microseconds;
73
if (is_date) // TIMESTAMP function
75
if (get_arg0_date(&l_time1, TIME_FUZZY_DATE) ||
76
args[1]->get_time(&l_time2) ||
77
l_time1.time_type == DRIZZLE_TIMESTAMP_TIME ||
78
l_time2.time_type != DRIZZLE_TIMESTAMP_TIME)
81
else // ADDTIME function
83
if (args[0]->get_time(&l_time1) ||
84
args[1]->get_time(&l_time2) ||
85
l_time2.time_type == DRIZZLE_TIMESTAMP_DATETIME)
87
is_time= (l_time1.time_type == DRIZZLE_TIMESTAMP_TIME);
89
if (l_time1.neg != l_time2.neg)
92
memset(&l_time3, 0, sizeof(l_time3));
94
l_time3.neg= calc_time_diff(&l_time1, &l_time2, -l_sign,
95
&seconds, µseconds);
98
If first argument was negative and diff between arguments
99
is non-zero we need to swap sign to get proper result.
101
if (l_time1.neg && (seconds || microseconds))
102
l_time3.neg= 1-l_time3.neg; // Swap sign of result
104
if (!is_time && l_time3.neg)
107
days= (long)(seconds/86400L);
109
calc_time_from_sec(&l_time3, (long)(seconds%86400L), microseconds);
113
get_date_from_daynr(days,&l_time3.year,&l_time3.month,&l_time3.day);
115
!make_datetime(l_time1.second_part || l_time2.second_part ?
116
DATE_TIME_MICROSECOND : DATE_TIME,
122
l_time3.hour+= days*24;
123
if (!make_datetime_with_warn(l_time1.second_part || l_time2.second_part ?
124
TIME_MICROSECOND : TIME_ONLY,
134
void Item_func_add_time::print(String *str, enum_query_type query_type)
139
str->append(STRING_WITH_LEN("timestamp("));
144
str->append(STRING_WITH_LEN("addtime("));
146
str->append(STRING_WITH_LEN("subtime("));
148
args[0]->print(str, query_type);
150
args[1]->print(str, query_type);