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, Inc.
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
22
#include "drizzled/function/time/timestamp_diff.h"
23
#include "drizzled/time_functions.h"
28
int64_t Item_func_timestamp_diff::val_int()
30
type::Time ltime1, ltime2;
37
if (args[0]->get_date(ltime1, TIME_NO_ZERO_DATE) ||
38
args[1]->get_date(ltime2, TIME_NO_ZERO_DATE))
41
if (calc_time_diff(<ime2,<ime1, 1,
42
&seconds, µseconds))
45
if (int_type == INTERVAL_YEAR ||
46
int_type == INTERVAL_QUARTER ||
47
int_type == INTERVAL_MONTH)
49
uint32_t year_beg, year_end, month_beg, month_end, day_beg, day_end;
51
uint32_t second_beg, second_end, microsecond_beg, microsecond_end;
55
year_beg= ltime2.year;
56
year_end= ltime1.year;
57
month_beg= ltime2.month;
58
month_end= ltime1.month;
61
second_beg= ltime2.hour * 3600 + ltime2.minute * 60 + ltime2.second;
62
second_end= ltime1.hour * 3600 + ltime1.minute * 60 + ltime1.second;
63
microsecond_beg= ltime2.second_part;
64
microsecond_end= ltime1.second_part;
68
year_beg= ltime1.year;
69
year_end= ltime2.year;
70
month_beg= ltime1.month;
71
month_end= ltime2.month;
74
second_beg= ltime1.hour * 3600 + ltime1.minute * 60 + ltime1.second;
75
second_end= ltime2.hour * 3600 + ltime2.minute * 60 + ltime2.second;
76
microsecond_beg= ltime1.second_part;
77
microsecond_end= ltime2.second_part;
81
years= year_end - year_beg;
82
if (month_end < month_beg || (month_end == month_beg && day_end < day_beg))
87
if (month_end < month_beg || (month_end == month_beg && day_end < day_beg))
88
months+= 12 - (month_beg - month_end);
90
months+= (month_end - month_beg);
92
if (day_end < day_beg)
94
else if ((day_end == day_beg) &&
95
((second_end < second_beg) ||
96
(second_end == second_beg && microsecond_end < microsecond_beg)))
102
return months/12*neg;
103
case INTERVAL_QUARTER:
108
return seconds/86400L/7L*neg;
110
return seconds/86400L*neg;
112
return seconds/3600L*neg;
113
case INTERVAL_MINUTE:
114
return seconds/60L*neg;
115
case INTERVAL_SECOND:
117
case INTERVAL_MICROSECOND:
119
In MySQL difference between any two valid datetime values
120
in microseconds fits into int64_t.
122
return (seconds*1000000L+microseconds)*neg;
133
void Item_func_timestamp_diff::print(String *str, enum_query_type query_type)
135
str->append(func_name());
140
str->append(STRING_WITH_LEN("YEAR"));
142
case INTERVAL_QUARTER:
143
str->append(STRING_WITH_LEN("QUARTER"));
146
str->append(STRING_WITH_LEN("MONTH"));
149
str->append(STRING_WITH_LEN("WEEK"));
152
str->append(STRING_WITH_LEN("DAY"));
155
str->append(STRING_WITH_LEN("HOUR"));
157
case INTERVAL_MINUTE:
158
str->append(STRING_WITH_LEN("MINUTE"));
160
case INTERVAL_SECOND:
161
str->append(STRING_WITH_LEN("SECOND"));
163
case INTERVAL_MICROSECOND:
164
str->append(STRING_WITH_LEN("SECOND_FRAC"));
170
for (uint32_t i=0 ; i < 2 ; i++)
173
args[i]->print(str, query_type);
178
} /* namespace drizzled */