~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
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
574.3.19 by Lee
moving functions from item_timefunc to functions/time directory
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
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
20
#include <config.h>
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
21
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
22
#include <drizzled/function/time/timestamp_diff.h>
23
#include <drizzled/time_functions.h>
574.3.19 by Lee
moving functions from item_timefunc to functions/time directory
24
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
25
namespace drizzled
26
{
27
574.3.19 by Lee
moving functions from item_timefunc to functions/time directory
28
int64_t Item_func_timestamp_diff::val_int()
29
{
2030.1.5 by Brian Aker
Update for moving DRIZZLE_TIME to type::Time
30
  type::Time ltime1, ltime2;
574.3.19 by Lee
moving functions from item_timefunc to functions/time directory
31
  int64_t seconds;
32
  long microseconds;
33
  long months= 0;
34
  int neg= 1;
35
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
36
  null_value= 0;
2104.2.8 by Brian Aker
Merge in reference from pointer.
37
  if (args[0]->get_date(ltime1, TIME_NO_ZERO_DATE) ||
38
      args[1]->get_date(ltime2, TIME_NO_ZERO_DATE))
574.3.19 by Lee
moving functions from item_timefunc to functions/time directory
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;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
107
  case INTERVAL_WEEK:
574.3.19 by Lee
moving functions from item_timefunc to functions/time directory
108
    return seconds/86400L/7L*neg;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
109
  case INTERVAL_DAY:
574.3.19 by Lee
moving functions from item_timefunc to functions/time directory
110
    return seconds/86400L*neg;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
111
  case INTERVAL_HOUR:
574.3.19 by Lee
moving functions from item_timefunc to functions/time directory
112
    return seconds/3600L*neg;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
113
  case INTERVAL_MINUTE:
574.3.19 by Lee
moving functions from item_timefunc to functions/time directory
114
    return seconds/60L*neg;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
115
  case INTERVAL_SECOND:
574.3.19 by Lee
moving functions from item_timefunc to functions/time directory
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
127
null_date:
128
  null_value=1;
129
  return 0;
130
}
131
132
2215.2.1 by Stewart Smith
remove enum_query_type which was effectively unused. It was set to one value once, compared to it once (i.e. always true) and passed around everywhere doing nothing.
133
void Item_func_timestamp_diff::print(String *str)
574.3.19 by Lee
moving functions from item_timefunc to functions/time directory
134
{
135
  str->append(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;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
148
  case INTERVAL_WEEK:
574.3.19 by Lee
moving functions from item_timefunc to functions/time directory
149
    str->append(STRING_WITH_LEN("WEEK"));
150
    break;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
151
  case INTERVAL_DAY:
574.3.19 by Lee
moving functions from item_timefunc to functions/time directory
152
    str->append(STRING_WITH_LEN("DAY"));
153
    break;
154
  case INTERVAL_HOUR:
155
    str->append(STRING_WITH_LEN("HOUR"));
156
    break;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
157
  case INTERVAL_MINUTE:
574.3.19 by Lee
moving functions from item_timefunc to functions/time directory
158
    str->append(STRING_WITH_LEN("MINUTE"));
159
    break;
160
  case INTERVAL_SECOND:
161
    str->append(STRING_WITH_LEN("SECOND"));
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
162
    break;
574.3.19 by Lee
moving functions from item_timefunc to functions/time directory
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(',');
2215.2.1 by Stewart Smith
remove enum_query_type which was effectively unused. It was set to one value once, compared to it once (i.e. always true) and passed around everywhere doing nothing.
173
    args[i]->print(str);
574.3.19 by Lee
moving functions from item_timefunc to functions/time directory
174
  }
175
  str->append(')');
176
}
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
177
178
} /* namespace drizzled */