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