~ubuntu-branches/ubuntu/saucy/drizzle/saucy-proposed

1 by Monty Taylor
Import upstream version 2010.03.1347
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2008-2009 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
21
/* Functions to handle date and time */
22
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"
29
30
namespace drizzled
31
{
32
33
/* Some functions to calculate dates */
34
35
36
int calc_weekday(long daynr,bool sunday_first_day_of_week)
37
{
38
  return ((int) ((daynr + 5L + (sunday_first_day_of_week ? 1L : 0L)) % 7));
39
}
40
41
42
uint32_t calc_week(DRIZZLE_TIME *l_time, uint32_t week_behaviour, uint32_t *year)
43
{
44
  uint32_t days;
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);
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
51
  uint32_t weekday= calc_weekday(first_daynr, !monday_first);
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
83
void get_date_from_daynr(long daynr,
84
                         uint32_t *ret_year,
85
                         uint32_t *ret_month,
86
			                   uint32_t *ret_day)
87
{
88
  uint32_t year,temp,leap_day,day_of_year,days_in_year;
89
  unsigned char *month_pos;
90
91
  if (daynr <= 365L || daynr >= 3652500)
92
  {						/* Fix if wrong daynr */
93
    *ret_year= *ret_month = *ret_day =0;
94
  }
95
  else
96
  {
97
    year= (uint32_t) (daynr*100 / 36525L);
98
    temp=(((year-1)/100+1)*3)/4;
99
    day_of_year=(uint32_t) (daynr - (long) year * 365L) - (year-1)/4 +temp;
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
      {
110
	day_of_year--;
111
	if (day_of_year == 31+28)
112
	  leap_day=1;		/* Handle leapyears leapday */
113
      }
114
    }
115
    *ret_month=1;
116
    for (month_pos= days_in_month ;
117
	 day_of_year > (uint32_t) *month_pos ;
118
	 day_of_year-= *(month_pos++), (*ret_month)++)
119
      ;
120
    *ret_year=year;
121
    *ret_day=day_of_year+leap_day;
122
  }
123
  return;
124
}
125
126
127
enum enum_drizzle_timestamp_type
128
str_to_datetime_with_warn(const char *str, 
129
                          uint32_t length, 
130
                          DRIZZLE_TIME *l_time,
131
                          uint32_t flags)
132
{
133
  int was_cut;
134
  Session *session= current_session;
135
  enum enum_drizzle_timestamp_type ts_type;
136
137
  ts_type= str_to_datetime(str, length, l_time,
138
                           (flags | (session->variables.sql_mode &
139
                                     (MODE_INVALID_DATES |
140
                                      MODE_NO_ZERO_DATE))),
141
                           &was_cut);
142
  if (was_cut || ts_type <= DRIZZLE_TIMESTAMP_ERROR)
143
    make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
144
                                 str, length, ts_type,  NULL);
145
  return ts_type;
146
}
147
148
149
bool
150
str_to_time_with_warn(const char *str, uint32_t length, DRIZZLE_TIME *l_time)
151
{
152
  int warning;
153
  bool ret_val= str_to_time(str, length, l_time, &warning);
154
  if (ret_val || warning)
155
    make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
156
                                 str, length, DRIZZLE_TIMESTAMP_TIME, NULL);
157
  return ret_val;
158
}
159
160
161
void localtime_to_TIME(DRIZZLE_TIME *to, struct tm *from)
162
{
163
  to->neg=0;
164
  to->second_part=0;
165
  to->year=	(int) ((from->tm_year+1900) % 10000);
166
  to->month=	(int) from->tm_mon+1;
167
  to->day=	(int) from->tm_mday;
168
  to->hour=	(int) from->tm_hour;
169
  to->minute=	(int) from->tm_min;
170
  to->second=   (int) from->tm_sec;
171
}
172
173
void make_date(const DRIZZLE_TIME *l_time, String *str)
174
{
175
  str->alloc(MAX_DATE_STRING_REP_LENGTH);
176
  uint32_t length= (uint32_t) my_date_to_str(l_time, str->c_ptr());
177
  str->length(length);
178
  str->set_charset(&my_charset_bin);
179
}
180
181
182
void make_datetime(const DRIZZLE_TIME *l_time, String *str)
183
{
184
  str->alloc(MAX_DATE_STRING_REP_LENGTH);
185
  uint32_t length= (uint32_t) my_datetime_to_str(l_time, str->c_ptr());
186
  str->length(length);
187
  str->set_charset(&my_charset_bin);
188
}
189
190
191
void make_truncated_value_warning(Session *session, 
192
                                  DRIZZLE_ERROR::enum_warning_level level,
193
                                  const char *str_val,
194
				                          uint32_t str_length,
195
                                  enum enum_drizzle_timestamp_type time_type,
196
                                  const char *field_name)
197
{
198
  char warn_buff[DRIZZLE_ERRMSG_SIZE];
199
  const char *type_str;
200
  CHARSET_INFO *cs= &my_charset_utf8_general_ci;
201
  char buff[128];
202
  String str(buff,(uint32_t) sizeof(buff), system_charset_info);
203
  str.copy(str_val, str_length, system_charset_info);
204
  str[str_length]= 0;               // Ensure we have end 0 for snprintf
205
206
  switch (time_type) {
207
    case DRIZZLE_TIMESTAMP_DATE:
208
      type_str= "date";
209
      break;
210
    case DRIZZLE_TIMESTAMP_TIME:
211
      type_str= "time";
212
      break;
213
    case DRIZZLE_TIMESTAMP_DATETIME:  // FALLTHROUGH
214
    default:
215
      type_str= "datetime";
216
      break;
217
  }
218
  if (field_name)
219
    cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff),
220
                       ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
221
                       type_str, str.c_ptr(), field_name,
222
                       (uint32_t) session->row_count);
223
  else
224
  {
225
    if (time_type > DRIZZLE_TIMESTAMP_ERROR)
226
      cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff),
227
                         ER(ER_TRUNCATED_WRONG_VALUE),
228
                         type_str, str.c_ptr());
229
    else
230
      cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff),
231
                         ER(ER_WRONG_VALUE), type_str, str.c_ptr());
232
  }
233
  push_warning(session, level,
234
               ER_TRUNCATED_WRONG_VALUE, warn_buff);
235
}
236
237
238
bool
239
calc_time_diff(DRIZZLE_TIME *l_time1, DRIZZLE_TIME *l_time2, int l_sign, int64_t *seconds_out,
240
               long *microseconds_out)
241
{
242
  long days;
243
  bool neg;
244
  int64_t microseconds;
245
246
  /*
247
    We suppose that if first argument is DRIZZLE_TIMESTAMP_TIME
248
    the second argument should be TIMESTAMP_TIME also.
249
    We should check it before calc_time_diff call.
250
  */
251
  if (l_time1->time_type == DRIZZLE_TIMESTAMP_TIME)  // Time value
252
    days= (long)l_time1->day - l_sign * (long)l_time2->day;
253
  else
254
  {
255
    days= calc_daynr((uint32_t) l_time1->year,
256
		     (uint32_t) l_time1->month,
257
		     (uint32_t) l_time1->day);
258
    if (l_time2->time_type == DRIZZLE_TIMESTAMP_TIME)
259
      days-= l_sign * (long)l_time2->day;
260
    else
261
      days-= l_sign*calc_daynr((uint32_t) l_time2->year,
262
			       (uint32_t) l_time2->month,
263
			       (uint32_t) l_time2->day);
264
  }
265
266
  microseconds= ((int64_t)days*86400L +
267
                 (int64_t)(l_time1->hour*3600L +
268
                            l_time1->minute*60L +
269
                            l_time1->second) -
270
                 l_sign*(int64_t)(l_time2->hour*3600L +
271
                                   l_time2->minute*60L +
272
                                   l_time2->second)) * 1000000L +
273
                (int64_t)l_time1->second_part -
274
                l_sign*(int64_t)l_time2->second_part;
275
276
  neg= 0;
277
  if (microseconds < 0)
278
  {
279
    microseconds= -microseconds;
280
    neg= 1;
281
  }
282
  *seconds_out= microseconds/1000000L;
283
  *microseconds_out= (long) (microseconds%1000000L);
284
  return neg;
285
}
286
287
} /* namespace drizzled */