~drizzle-trunk/drizzle/development

574.3.19 by Lee
moving functions from item_timefunc to functions/time directory
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
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 <drizzled/server_includes.h>
21
#include CSTDINT_H
670.1.20 by Monty Taylor
Renamed functions to function... everything else is singular.
22
#include <drizzled/function/time/timestamp_diff.h>
574.3.19 by Lee
moving functions from item_timefunc to functions/time directory
23
24
int64_t Item_func_timestamp_diff::val_int()
25
{
26
  DRIZZLE_TIME ltime1, ltime2;
27
  int64_t seconds;
28
  long microseconds;
29
  long months= 0;
30
  int neg= 1;
31
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
32
  null_value= 0;
574.3.19 by Lee
moving functions from item_timefunc to functions/time directory
33
  if (args[0]->get_date(&ltime1, TIME_NO_ZERO_DATE) ||
34
      args[1]->get_date(&ltime2, TIME_NO_ZERO_DATE))
35
    goto null_date;
36
37
  if (calc_time_diff(&ltime2,&ltime1, 1,
38
		     &seconds, &microseconds))
39
    neg= -1;
40
41
  if (int_type == INTERVAL_YEAR ||
42
      int_type == INTERVAL_QUARTER ||
43
      int_type == INTERVAL_MONTH)
44
  {
45
    uint32_t year_beg, year_end, month_beg, month_end, day_beg, day_end;
46
    uint32_t years= 0;
47
    uint32_t second_beg, second_end, microsecond_beg, microsecond_end;
48
49
    if (neg == -1)
50
    {
51
      year_beg= ltime2.year;
52
      year_end= ltime1.year;
53
      month_beg= ltime2.month;
54
      month_end= ltime1.month;
55
      day_beg= ltime2.day;
56
      day_end= ltime1.day;
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;
61
    }
62
    else
63
    {
64
      year_beg= ltime1.year;
65
      year_end= ltime2.year;
66
      month_beg= ltime1.month;
67
      month_end= ltime2.month;
68
      day_beg= ltime1.day;
69
      day_end= ltime2.day;
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;
74
    }
75
76
    /* calc years */
77
    years= year_end - year_beg;
78
    if (month_end < month_beg || (month_end == month_beg && day_end < day_beg))
79
      years-= 1;
80
81
    /* calc months */
82
    months= 12*years;
83
    if (month_end < month_beg || (month_end == month_beg && day_end < day_beg))
84
      months+= 12 - (month_beg - month_end);
85
    else
86
      months+= (month_end - month_beg);
87
88
    if (day_end < day_beg)
89
      months-= 1;
90
    else if ((day_end == day_beg) &&
91
	     ((second_end < second_beg) ||
92
	      (second_end == second_beg && microsecond_end < microsecond_beg)))
93
      months-= 1;
94
  }
95
96
  switch (int_type) {
97
  case INTERVAL_YEAR:
98
    return months/12*neg;
99
  case INTERVAL_QUARTER:
100
    return months/3*neg;
101
  case INTERVAL_MONTH:
102
    return months*neg;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
103
  case INTERVAL_WEEK:
574.3.19 by Lee
moving functions from item_timefunc to functions/time directory
104
    return seconds/86400L/7L*neg;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
105
  case INTERVAL_DAY:
574.3.19 by Lee
moving functions from item_timefunc to functions/time directory
106
    return seconds/86400L*neg;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
107
  case INTERVAL_HOUR:
574.3.19 by Lee
moving functions from item_timefunc to functions/time directory
108
    return seconds/3600L*neg;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
109
  case INTERVAL_MINUTE:
574.3.19 by Lee
moving functions from item_timefunc to functions/time directory
110
    return seconds/60L*neg;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
111
  case INTERVAL_SECOND:
574.3.19 by Lee
moving functions from item_timefunc to functions/time directory
112
    return seconds*neg;
113
  case INTERVAL_MICROSECOND:
114
    /*
115
      In MySQL difference between any two valid datetime values
116
      in microseconds fits into int64_t.
117
    */
118
    return (seconds*1000000L+microseconds)*neg;
119
  default:
120
    break;
121
  }
122
123
null_date:
124
  null_value=1;
125
  return 0;
126
}
127
128
129
void Item_func_timestamp_diff::print(String *str, enum_query_type query_type)
130
{
131
  str->append(func_name());
132
  str->append('(');
133
134
  switch (int_type) {
135
  case INTERVAL_YEAR:
136
    str->append(STRING_WITH_LEN("YEAR"));
137
    break;
138
  case INTERVAL_QUARTER:
139
    str->append(STRING_WITH_LEN("QUARTER"));
140
    break;
141
  case INTERVAL_MONTH:
142
    str->append(STRING_WITH_LEN("MONTH"));
143
    break;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
144
  case INTERVAL_WEEK:
574.3.19 by Lee
moving functions from item_timefunc to functions/time directory
145
    str->append(STRING_WITH_LEN("WEEK"));
146
    break;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
147
  case INTERVAL_DAY:
574.3.19 by Lee
moving functions from item_timefunc to functions/time directory
148
    str->append(STRING_WITH_LEN("DAY"));
149
    break;
150
  case INTERVAL_HOUR:
151
    str->append(STRING_WITH_LEN("HOUR"));
152
    break;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
153
  case INTERVAL_MINUTE:
574.3.19 by Lee
moving functions from item_timefunc to functions/time directory
154
    str->append(STRING_WITH_LEN("MINUTE"));
155
    break;
156
  case INTERVAL_SECOND:
157
    str->append(STRING_WITH_LEN("SECOND"));
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
158
    break;
574.3.19 by Lee
moving functions from item_timefunc to functions/time directory
159
  case INTERVAL_MICROSECOND:
160
    str->append(STRING_WITH_LEN("SECOND_FRAC"));
161
    break;
162
  default:
163
    break;
164
  }
165
166
  for (uint32_t i=0 ; i < 2 ; i++)
167
  {
168
    str->append(',');
169
    args[i]->print(str, query_type);
170
  }
171
  str->append(')');
172
}