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/functions/time/timestamp_diff.h>
24
int64_t Item_func_timestamp_diff::val_int()
26
DRIZZLE_TIME ltime1, ltime2;
33
if (args[0]->get_date(<ime1, TIME_NO_ZERO_DATE) ||
34
args[1]->get_date(<ime2, TIME_NO_ZERO_DATE))
37
if (calc_time_diff(<ime2,<ime1, 1,
38
&seconds, µseconds))
41
if (int_type == INTERVAL_YEAR ||
42
int_type == INTERVAL_QUARTER ||
43
int_type == INTERVAL_MONTH)
45
uint32_t year_beg, year_end, month_beg, month_end, day_beg, day_end;
47
uint32_t second_beg, second_end, microsecond_beg, microsecond_end;
51
year_beg= ltime2.year;
52
year_end= ltime1.year;
53
month_beg= ltime2.month;
54
month_end= ltime1.month;
57
second_beg= ltime2.hour * 3600 + ltime2.minute * 60 + ltime2.second;
58
second_end= ltime1.hour * 3600 + ltime1.minute * 60 + ltime1.second;
59
microsecond_beg= ltime2.second_part;
60
microsecond_end= ltime1.second_part;
64
year_beg= ltime1.year;
65
year_end= ltime2.year;
66
month_beg= ltime1.month;
67
month_end= ltime2.month;
70
second_beg= ltime1.hour * 3600 + ltime1.minute * 60 + ltime1.second;
71
second_end= ltime2.hour * 3600 + ltime2.minute * 60 + ltime2.second;
72
microsecond_beg= ltime1.second_part;
73
microsecond_end= ltime2.second_part;
77
years= year_end - year_beg;
78
if (month_end < month_beg || (month_end == month_beg && day_end < day_beg))
83
if (month_end < month_beg || (month_end == month_beg && day_end < day_beg))
84
months+= 12 - (month_beg - month_end);
86
months+= (month_end - month_beg);
88
if (day_end < day_beg)
90
else if ((day_end == day_beg) &&
91
((second_end < second_beg) ||
92
(second_end == second_beg && microsecond_end < microsecond_beg)))
99
case INTERVAL_QUARTER:
104
return seconds/86400L/7L*neg;
106
return seconds/86400L*neg;
108
return seconds/3600L*neg;
109
case INTERVAL_MINUTE:
110
return seconds/60L*neg;
111
case INTERVAL_SECOND:
113
case INTERVAL_MICROSECOND:
115
In MySQL difference between any two valid datetime values
116
in microseconds fits into int64_t.
118
return (seconds*1000000L+microseconds)*neg;
129
void Item_func_timestamp_diff::print(String *str, enum_query_type query_type)
131
str->append(func_name());
136
str->append(STRING_WITH_LEN("YEAR"));
138
case INTERVAL_QUARTER:
139
str->append(STRING_WITH_LEN("QUARTER"));
142
str->append(STRING_WITH_LEN("MONTH"));
145
str->append(STRING_WITH_LEN("WEEK"));
148
str->append(STRING_WITH_LEN("DAY"));
151
str->append(STRING_WITH_LEN("HOUR"));
153
case INTERVAL_MINUTE:
154
str->append(STRING_WITH_LEN("MINUTE"));
156
case INTERVAL_SECOND:
157
str->append(STRING_WITH_LEN("SECOND"));
159
case INTERVAL_MICROSECOND:
160
str->append(STRING_WITH_LEN("SECOND_FRAC"));
166
for (uint32_t i=0 ; i < 2 ; i++)
169
args[i]->print(str, query_type);