~drizzle-trunk/drizzle/development

520.4.14 by Monty Taylor
Removed korr.h and tztime.h from common_includes. Also removed the HAVE_DTRACE block and stuck it in autoconf.
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-2009 Sun Microsystems, Inc.
520.4.14 by Monty Taylor
Removed korr.h and tztime.h from common_includes. Also removed the HAVE_DTRACE block and stuck it in autoconf.
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
 */
1 by brian
clean slate
19
20
21
/* Functions to handle date and time */
22
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
23
#include <config.h>
24
#include <drizzled/error.h>
25
#include <drizzled/util/test.h>
26
#include <drizzled/session.h>
27
#include <drizzled/time_functions.h>
2281.5.1 by Muhammad Umair
Merged charset declarations of global_charset_info.h and charset_info.h into charset.h header file.
28
#include <drizzled/charset.h>
2241.3.2 by Olaf van der Spek
Refactor Session::variables
29
#include <drizzled/system_variables.h>
2318.6.113 by Olaf van der Spek
Add include
30
#include <drizzled/sql_string.h>
520.4.14 by Monty Taylor
Removed korr.h and tztime.h from common_includes. Also removed the HAVE_DTRACE block and stuck it in autoconf.
31
2241.2.14 by Olaf van der Spek
Refactor
32
namespace drizzled {
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
33
520.4.14 by Monty Taylor
Removed korr.h and tztime.h from common_includes. Also removed the HAVE_DTRACE block and stuck it in autoconf.
34
/* Some functions to calculate dates */
1 by brian
clean slate
35
36
37
int calc_weekday(long daynr,bool sunday_first_day_of_week)
38
{
51.1.71 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
39
  return ((int) ((daynr + 5L + (sunday_first_day_of_week ? 1L : 0L)) % 7));
1 by brian
clean slate
40
}
41
42
2030.1.5 by Brian Aker
Update for moving DRIZZLE_TIME to type::Time
43
uint32_t calc_week(type::Time *l_time, uint32_t week_behaviour, uint32_t *year)
1 by brian
clean slate
44
{
482 by Brian Aker
Remove uint.
45
  uint32_t days;
520.1.7 by Brian Aker
More ulong fixes (including a bug on row return on error)
46
  uint32_t daynr= calc_daynr(l_time->year,l_time->month,l_time->day);
47
  uint32_t first_daynr= calc_daynr(l_time->year,1,1);
1 by brian
clean slate
48
  bool monday_first= test(week_behaviour & WEEK_MONDAY_FIRST);
49
  bool week_year= test(week_behaviour & WEEK_YEAR);
50
  bool first_weekday= test(week_behaviour & WEEK_FIRST_WEEKDAY);
51
1237.9.4 by Padraig O'Sullivan
Removed the inclusion of drizzled/field.h in the server_includes header file.
52
  uint32_t weekday= calc_weekday(first_daynr, !monday_first);
1 by brian
clean slate
53
  *year=l_time->year;
54
55
  if (l_time->month == 1 && l_time->day <= 7-weekday)
56
  {
57
    if ((!week_year) && ((first_weekday && weekday != 0) || (!first_weekday && weekday >= 4)))
58
      return 0;
59
    week_year= 1;
60
    (*year)--;
61
    first_daynr-= (days=calc_days_in_year(*year));
62
    weekday= (weekday + 53*7- days) % 7;
63
  }
64
65
  if ((first_weekday && weekday != 0) ||
66
      (!first_weekday && weekday >= 4))
67
    days= daynr - (first_daynr+ (7-weekday));
68
  else
69
    days= daynr - (first_daynr - weekday);
70
71
  if (week_year && days >= 52*7)
72
  {
73
    weekday= (weekday + calc_days_in_year(*year)) % 7;
74
    if ((!first_weekday && weekday < 4) || (first_weekday && weekday == 0))
75
    {
76
      (*year)++;
77
      return 1;
78
    }
79
  }
80
  return days/7+1;
81
}
82
83
1237.9.4 by Padraig O'Sullivan
Removed the inclusion of drizzled/field.h in the server_includes header file.
84
void get_date_from_daynr(long daynr,
85
                         uint32_t *ret_year,
86
                         uint32_t *ret_month,
87
			                   uint32_t *ret_day)
1 by brian
clean slate
88
{
482 by Brian Aker
Remove uint.
89
  uint32_t year,temp,leap_day,day_of_year,days_in_year;
481 by Brian Aker
Remove all of uchar.
90
  unsigned char *month_pos;
1 by brian
clean slate
91
92
  if (daynr <= 365L || daynr >= 3652500)
93
  {						/* Fix if wrong daynr */
94
    *ret_year= *ret_month = *ret_day =0;
95
  }
96
  else
97
  {
895 by Brian Aker
Completion (?) of uint conversion.
98
    year= (uint32_t) (daynr*100 / 36525L);
1 by brian
clean slate
99
    temp=(((year-1)/100+1)*3)/4;
895 by Brian Aker
Completion (?) of uint conversion.
100
    day_of_year=(uint32_t) (daynr - (long) year * 365L) - (year-1)/4 +temp;
1 by brian
clean slate
101
    while (day_of_year > (days_in_year= calc_days_in_year(year)))
102
    {
103
      day_of_year-=days_in_year;
104
      (year)++;
105
    }
106
    leap_day=0;
107
    if (days_in_year == 366)
108
    {
109
      if (day_of_year > 31+28)
110
      {
2088.8.6 by Brian Aker
Format
111
        day_of_year--;
112
        if (day_of_year == 31+28)
113
          leap_day=1;		/* Handle leapyears leapday */
1 by brian
clean slate
114
      }
115
    }
116
    *ret_month=1;
117
    for (month_pos= days_in_month ;
2088.8.6 by Brian Aker
Format
118
         day_of_year > (uint32_t) *month_pos ;
119
         day_of_year-= *(month_pos++), (*ret_month)++)
1 by brian
clean slate
120
      ;
121
    *ret_year=year;
122
    *ret_day=day_of_year+leap_day;
123
  }
51.1.71 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
124
  return;
1 by brian
clean slate
125
}
126
127
2114.5.1 by Brian Aker
Additional abstract around time (this also makes the abort_on_warnings in
128
type::timestamp_t str_to_datetime_with_warn(Session *session,
129
                                            const char *str, 
2088.8.9 by Brian Aker
Merge in namespace of enum.
130
                                            uint32_t length, 
131
                                            type::Time *l_time,
132
                                            uint32_t flags)
1 by brian
clean slate
133
{
2104.2.6 by Brian Aker
Enum was_cut
134
  type::cut_t was_cut= type::VALID;
2088.8.9 by Brian Aker
Merge in namespace of enum.
135
  type::timestamp_t ts_type;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
136
2104.2.1 by Brian Aker
Encapsulate str_to_date
137
  ts_type= l_time->store(str, length,
138
                         (flags | (session->variables.sql_mode &
139
                                   (MODE_INVALID_DATES |
140
                                    MODE_NO_ZERO_DATE))),
141
                         was_cut);
2088.8.9 by Brian Aker
Merge in namespace of enum.
142
  if (was_cut || ts_type <= type::DRIZZLE_TIMESTAMP_ERROR)
1633.4.7 by Brian Aker
Put a copy of the Session in the root of function to make use of.
143
    make_truncated_value_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
461 by Monty Taylor
Removed NullS. bu-bye.
144
                                 str, length, ts_type,  NULL);
2104.2.1 by Brian Aker
Encapsulate str_to_date
145
1 by brian
clean slate
146
  return ts_type;
147
}
148
149
150
bool
2137.1.9 by Brian Aker
Remove current_session usage in one location (we have the table's session to
151
str_to_time_with_warn(Session *session, const char *str, uint32_t length, type::Time *l_time)
1 by brian
clean slate
152
{
153
  int warning;
2104.2.3 by Brian Aker
Merge in more encapsulation of time work.
154
  bool ret_val= l_time->store(str, length, warning, type::DRIZZLE_TIMESTAMP_TIME);
1 by brian
clean slate
155
  if (ret_val || warning)
2137.1.9 by Brian Aker
Remove current_session usage in one location (we have the table's session to
156
    make_truncated_value_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
2088.8.9 by Brian Aker
Merge in namespace of enum.
157
                                 str, length, type::DRIZZLE_TIMESTAMP_TIME, NULL);
1 by brian
clean slate
158
  return ret_val;
159
}
160
161
1237.9.4 by Padraig O'Sullivan
Removed the inclusion of drizzled/field.h in the server_includes header file.
162
void make_truncated_value_warning(Session *session, 
163
                                  DRIZZLE_ERROR::enum_warning_level level,
1 by brian
clean slate
164
                                  const char *str_val,
1237.9.4 by Padraig O'Sullivan
Removed the inclusion of drizzled/field.h in the server_includes header file.
165
				                          uint32_t str_length,
2088.8.9 by Brian Aker
Merge in namespace of enum.
166
                                  type::timestamp_t time_type,
1 by brian
clean slate
167
                                  const char *field_name)
168
{
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
169
  char warn_buff[DRIZZLE_ERRMSG_SIZE];
1 by brian
clean slate
170
  const char *type_str;
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
171
  charset_info_st *cs= &my_charset_utf8_general_ci;
1 by brian
clean slate
172
  char buff[128];
205 by Brian Aker
uint32 -> uin32_t
173
  String str(buff,(uint32_t) sizeof(buff), system_charset_info);
1 by brian
clean slate
174
  str.copy(str_val, str_length, system_charset_info);
175
  str[str_length]= 0;               // Ensure we have end 0 for snprintf
176
177
  switch (time_type) {
2088.8.9 by Brian Aker
Merge in namespace of enum.
178
  case type::DRIZZLE_TIMESTAMP_DATE:
2088.8.6 by Brian Aker
Format
179
    type_str= "date";
180
    break;
2088.8.9 by Brian Aker
Merge in namespace of enum.
181
182
  case type::DRIZZLE_TIMESTAMP_TIME:
2088.8.6 by Brian Aker
Format
183
    type_str= "time";
184
    break;
2088.8.9 by Brian Aker
Merge in namespace of enum.
185
186
  case type::DRIZZLE_TIMESTAMP_DATETIME:  // FALLTHROUGH
2088.8.6 by Brian Aker
Format
187
  default:
188
    type_str= "datetime";
189
    break;
1 by brian
clean slate
190
  }
2069.2.2 by Brian Aker
Small refactoring around type::Time
191
1 by brian
clean slate
192
  if (field_name)
1578.4.12 by Brian Aker
This removes a couple of additional uses of current_session.
193
  {
1 by brian
clean slate
194
    cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff),
195
                       ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
196
                       type_str, str.c_ptr(), field_name,
520.1.22 by Brian Aker
Second pass of thd cleanup
197
                       (uint32_t) session->row_count);
1578.4.12 by Brian Aker
This removes a couple of additional uses of current_session.
198
  }
1 by brian
clean slate
199
  else
200
  {
2088.8.9 by Brian Aker
Merge in namespace of enum.
201
    if (time_type > type::DRIZZLE_TIMESTAMP_ERROR)
2069.2.2 by Brian Aker
Small refactoring around type::Time
202
    {
1 by brian
clean slate
203
      cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff),
204
                         ER(ER_TRUNCATED_WRONG_VALUE),
205
                         type_str, str.c_ptr());
2069.2.2 by Brian Aker
Small refactoring around type::Time
206
    }
1 by brian
clean slate
207
    else
2069.2.2 by Brian Aker
Small refactoring around type::Time
208
    {
1 by brian
clean slate
209
      cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff),
210
                         ER(ER_WRONG_VALUE), type_str, str.c_ptr());
2069.2.2 by Brian Aker
Small refactoring around type::Time
211
    }
1 by brian
clean slate
212
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
213
  push_warning(session, level,
1 by brian
clean slate
214
               ER_TRUNCATED_WRONG_VALUE, warn_buff);
215
}
216
217
218
bool
2030.1.5 by Brian Aker
Update for moving DRIZZLE_TIME to type::Time
219
calc_time_diff(type::Time *l_time1, type::Time *l_time2, int l_sign, int64_t *seconds_out,
1 by brian
clean slate
220
               long *microseconds_out)
221
{
222
  long days;
223
  bool neg;
152 by Brian Aker
longlong replacement
224
  int64_t microseconds;
1 by brian
clean slate
225
226
  /*
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
227
    We suppose that if first argument is DRIZZLE_TIMESTAMP_TIME
1 by brian
clean slate
228
    the second argument should be TIMESTAMP_TIME also.
229
    We should check it before calc_time_diff call.
230
  */
2088.8.9 by Brian Aker
Merge in namespace of enum.
231
  if (l_time1->time_type == type::DRIZZLE_TIMESTAMP_TIME)  // Time value
1 by brian
clean slate
232
    days= (long)l_time1->day - l_sign * (long)l_time2->day;
233
  else
234
  {
895 by Brian Aker
Completion (?) of uint conversion.
235
    days= calc_daynr((uint32_t) l_time1->year,
2088.8.6 by Brian Aker
Format
236
                     (uint32_t) l_time1->month,
237
                     (uint32_t) l_time1->day);
2088.8.9 by Brian Aker
Merge in namespace of enum.
238
    if (l_time2->time_type == type::DRIZZLE_TIMESTAMP_TIME)
1 by brian
clean slate
239
      days-= l_sign * (long)l_time2->day;
240
    else
895 by Brian Aker
Completion (?) of uint conversion.
241
      days-= l_sign*calc_daynr((uint32_t) l_time2->year,
2088.8.6 by Brian Aker
Format
242
                               (uint32_t) l_time2->month,
243
                               (uint32_t) l_time2->day);
1 by brian
clean slate
244
  }
245
398.1.8 by Monty Taylor
Enabled -Wlong-long.
246
  microseconds= ((int64_t)days*86400L +
152 by Brian Aker
longlong replacement
247
                 (int64_t)(l_time1->hour*3600L +
2088.8.6 by Brian Aker
Format
248
                           l_time1->minute*60L +
249
                           l_time1->second) -
152 by Brian Aker
longlong replacement
250
                 l_sign*(int64_t)(l_time2->hour*3600L +
2088.8.6 by Brian Aker
Format
251
                                  l_time2->minute*60L +
252
                                  l_time2->second)) * 1000000L +
253
    (int64_t)l_time1->second_part -
254
    l_sign*(int64_t)l_time2->second_part;
1 by brian
clean slate
255
256
  neg= 0;
257
  if (microseconds < 0)
258
  {
259
    microseconds= -microseconds;
260
    neg= 1;
261
  }
262
  *seconds_out= microseconds/1000000L;
263
  *microseconds_out= (long) (microseconds%1000000L);
264
  return neg;
265
}
266
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
267
} /* namespace drizzled */