drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
timestamp_diff.cc
1/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2008 Sun Microsystems, Inc.
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 */
19
20#include <config.h>
21
22#include <drizzled/function/time/timestamp_diff.h>
23#include <drizzled/time_functions.h>
24
25namespace drizzled
26{
27
29{
30 type::Time ltime1, ltime2;
31 int64_t seconds;
32 long microseconds;
33 long months= 0;
34 int neg= 1;
35
36 null_value= 0;
37 if (args[0]->get_date(ltime1, TIME_NO_ZERO_DATE) ||
38 args[1]->get_date(ltime2, TIME_NO_ZERO_DATE))
39 goto null_date;
40
41 if (calc_time_diff(&ltime2,&ltime1, 1,
42 &seconds, &microseconds))
43 neg= -1;
44
45 if (int_type == INTERVAL_YEAR ||
46 int_type == INTERVAL_QUARTER ||
47 int_type == INTERVAL_MONTH)
48 {
49 uint32_t year_beg, year_end, month_beg, month_end, day_beg, day_end;
50 uint32_t years= 0;
51 uint32_t second_beg, second_end, microsecond_beg, microsecond_end;
52
53 if (neg == -1)
54 {
55 year_beg= ltime2.year;
56 year_end= ltime1.year;
57 month_beg= ltime2.month;
58 month_end= ltime1.month;
59 day_beg= ltime2.day;
60 day_end= ltime1.day;
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;
65 }
66 else
67 {
68 year_beg= ltime1.year;
69 year_end= ltime2.year;
70 month_beg= ltime1.month;
71 month_end= ltime2.month;
72 day_beg= ltime1.day;
73 day_end= ltime2.day;
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;
78 }
79
80 /* calc years */
81 years= year_end - year_beg;
82 if (month_end < month_beg || (month_end == month_beg && day_end < day_beg))
83 years-= 1;
84
85 /* calc months */
86 months= 12*years;
87 if (month_end < month_beg || (month_end == month_beg && day_end < day_beg))
88 months+= 12 - (month_beg - month_end);
89 else
90 months+= (month_end - month_beg);
91
92 if (day_end < day_beg)
93 months-= 1;
94 else if ((day_end == day_beg) &&
95 ((second_end < second_beg) ||
96 (second_end == second_beg && microsecond_end < microsecond_beg)))
97 months-= 1;
98 }
99
100 switch (int_type) {
101 case INTERVAL_YEAR:
102 return months/12*neg;
103 case INTERVAL_QUARTER:
104 return months/3*neg;
105 case INTERVAL_MONTH:
106 return months*neg;
107 case INTERVAL_WEEK:
108 return seconds/86400L/7L*neg;
109 case INTERVAL_DAY:
110 return seconds/86400L*neg;
111 case INTERVAL_HOUR:
112 return seconds/3600L*neg;
113 case INTERVAL_MINUTE:
114 return seconds/60L*neg;
115 case INTERVAL_SECOND:
116 return seconds*neg;
117 case INTERVAL_MICROSECOND:
118 /*
119 In MySQL difference between any two valid datetime values
120 in microseconds fits into int64_t.
121 */
122 return (seconds*1000000L+microseconds)*neg;
123 default:
124 break;
125 }
126
127null_date:
128 null_value=1;
129 return 0;
130}
131
132
134{
135 str->append(func_name(), strlen(func_name()));
136 str->append('(');
137
138 switch (int_type) {
139 case INTERVAL_YEAR:
140 str->append(STRING_WITH_LEN("YEAR"));
141 break;
142 case INTERVAL_QUARTER:
143 str->append(STRING_WITH_LEN("QUARTER"));
144 break;
145 case INTERVAL_MONTH:
146 str->append(STRING_WITH_LEN("MONTH"));
147 break;
148 case INTERVAL_WEEK:
149 str->append(STRING_WITH_LEN("WEEK"));
150 break;
151 case INTERVAL_DAY:
152 str->append(STRING_WITH_LEN("DAY"));
153 break;
154 case INTERVAL_HOUR:
155 str->append(STRING_WITH_LEN("HOUR"));
156 break;
157 case INTERVAL_MINUTE:
158 str->append(STRING_WITH_LEN("MINUTE"));
159 break;
160 case INTERVAL_SECOND:
161 str->append(STRING_WITH_LEN("SECOND"));
162 break;
163 case INTERVAL_MICROSECOND:
164 str->append(STRING_WITH_LEN("SECOND_FRAC"));
165 break;
166 default:
167 break;
168 }
169
170 for (uint32_t i=0 ; i < 2 ; i++)
171 {
172 str->append(',');
173 args[i]->print(str);
174 }
175 str->append(')');
176}
177
178} /* namespace drizzled */
virtual void print(String *str)
virtual bool get_date(type::Time &ltime, uint32_t fuzzydate)
Definition item.cc:452
bool null_value
Definition item.h:124
TODO: Rename this file - func.h is stupid.