~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
 *
1237.9.4 by Padraig O'Sullivan
Removed the inclusion of drizzled/field.h in the server_includes header file.
4
 *  Copyright (C) 2008-2009 Sun Microsystems
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
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
23
#include "config.h"
1237.9.4 by Padraig O'Sullivan
Removed the inclusion of drizzled/field.h in the server_includes header file.
24
#include "drizzled/error.h"
25
#include "drizzled/util/test.h"
26
#include "drizzled/tztime.h"
27
#include "drizzled/session.h"
1237.9.8 by Monty Taylor
Fixed solaris build.
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
30
/* Some functions to calculate dates */
1 by brian
clean slate
31
32
#ifndef TESTTIME
33
34
int calc_weekday(long daynr,bool sunday_first_day_of_week)
35
{
51.1.71 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
36
  return ((int) ((daynr + 5L + (sunday_first_day_of_week ? 1L : 0L)) % 7));
1 by brian
clean slate
37
}
38
39
482 by Brian Aker
Remove uint.
40
uint32_t calc_week(DRIZZLE_TIME *l_time, uint32_t week_behaviour, uint32_t *year)
1 by brian
clean slate
41
{
482 by Brian Aker
Remove uint.
42
  uint32_t days;
520.1.7 by Brian Aker
More ulong fixes (including a bug on row return on error)
43
  uint32_t daynr= calc_daynr(l_time->year,l_time->month,l_time->day);
44
  uint32_t first_daynr= calc_daynr(l_time->year,1,1);
1 by brian
clean slate
45
  bool monday_first= test(week_behaviour & WEEK_MONDAY_FIRST);
46
  bool week_year= test(week_behaviour & WEEK_YEAR);
47
  bool first_weekday= test(week_behaviour & WEEK_FIRST_WEEKDAY);
48
1237.9.4 by Padraig O'Sullivan
Removed the inclusion of drizzled/field.h in the server_includes header file.
49
  uint32_t weekday= calc_weekday(first_daynr, !monday_first);
1 by brian
clean slate
50
  *year=l_time->year;
51
52
  if (l_time->month == 1 && l_time->day <= 7-weekday)
53
  {
54
    if ((!week_year) && ((first_weekday && weekday != 0) || (!first_weekday && weekday >= 4)))
55
      return 0;
56
    week_year= 1;
57
    (*year)--;
58
    first_daynr-= (days=calc_days_in_year(*year));
59
    weekday= (weekday + 53*7- days) % 7;
60
  }
61
62
  if ((first_weekday && weekday != 0) ||
63
      (!first_weekday && weekday >= 4))
64
    days= daynr - (first_daynr+ (7-weekday));
65
  else
66
    days= daynr - (first_daynr - weekday);
67
68
  if (week_year && days >= 52*7)
69
  {
70
    weekday= (weekday + calc_days_in_year(*year)) % 7;
71
    if ((!first_weekday && weekday < 4) || (first_weekday && weekday == 0))
72
    {
73
      (*year)++;
74
      return 1;
75
    }
76
  }
77
  return days/7+1;
78
}
79
80
1237.9.4 by Padraig O'Sullivan
Removed the inclusion of drizzled/field.h in the server_includes header file.
81
void get_date_from_daynr(long daynr,
82
                         uint32_t *ret_year,
83
                         uint32_t *ret_month,
84
			                   uint32_t *ret_day)
1 by brian
clean slate
85
{
482 by Brian Aker
Remove uint.
86
  uint32_t year,temp,leap_day,day_of_year,days_in_year;
481 by Brian Aker
Remove all of uchar.
87
  unsigned char *month_pos;
1 by brian
clean slate
88
89
  if (daynr <= 365L || daynr >= 3652500)
90
  {						/* Fix if wrong daynr */
91
    *ret_year= *ret_month = *ret_day =0;
92
  }
93
  else
94
  {
895 by Brian Aker
Completion (?) of uint conversion.
95
    year= (uint32_t) (daynr*100 / 36525L);
1 by brian
clean slate
96
    temp=(((year-1)/100+1)*3)/4;
895 by Brian Aker
Completion (?) of uint conversion.
97
    day_of_year=(uint32_t) (daynr - (long) year * 365L) - (year-1)/4 +temp;
1 by brian
clean slate
98
    while (day_of_year > (days_in_year= calc_days_in_year(year)))
99
    {
100
      day_of_year-=days_in_year;
101
      (year)++;
102
    }
103
    leap_day=0;
104
    if (days_in_year == 366)
105
    {
106
      if (day_of_year > 31+28)
107
      {
108
	day_of_year--;
109
	if (day_of_year == 31+28)
110
	  leap_day=1;		/* Handle leapyears leapday */
111
      }
112
    }
113
    *ret_month=1;
114
    for (month_pos= days_in_month ;
895 by Brian Aker
Completion (?) of uint conversion.
115
	 day_of_year > (uint32_t) *month_pos ;
1 by brian
clean slate
116
	 day_of_year-= *(month_pos++), (*ret_month)++)
117
      ;
118
    *ret_year=year;
119
    *ret_day=day_of_year+leap_day;
120
  }
51.1.71 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
121
  return;
1 by brian
clean slate
122
}
123
124
398.1.1 by Monty Taylor
Remove typedef enum enum_drizzle_timestamp_type timestamp_type;
125
enum enum_drizzle_timestamp_type
1237.9.4 by Padraig O'Sullivan
Removed the inclusion of drizzled/field.h in the server_includes header file.
126
str_to_datetime_with_warn(const char *str, 
127
                          uint32_t length, 
128
                          DRIZZLE_TIME *l_time,
482 by Brian Aker
Remove uint.
129
                          uint32_t flags)
1 by brian
clean slate
130
{
131
  int was_cut;
520.1.22 by Brian Aker
Second pass of thd cleanup
132
  Session *session= current_session;
398.1.1 by Monty Taylor
Remove typedef enum enum_drizzle_timestamp_type timestamp_type;
133
  enum enum_drizzle_timestamp_type ts_type;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
134
1 by brian
clean slate
135
  ts_type= str_to_datetime(str, length, l_time,
520.1.22 by Brian Aker
Second pass of thd cleanup
136
                           (flags | (session->variables.sql_mode &
1 by brian
clean slate
137
                                     (MODE_INVALID_DATES |
138
                                      MODE_NO_ZERO_DATE))),
139
                           &was_cut);
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
140
  if (was_cut || ts_type <= DRIZZLE_TIMESTAMP_ERROR)
520.1.22 by Brian Aker
Second pass of thd cleanup
141
    make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
461 by Monty Taylor
Removed NullS. bu-bye.
142
                                 str, length, ts_type,  NULL);
1 by brian
clean slate
143
  return ts_type;
144
}
145
146
147
bool
482 by Brian Aker
Remove uint.
148
str_to_time_with_warn(const char *str, uint32_t length, DRIZZLE_TIME *l_time)
1 by brian
clean slate
149
{
150
  int warning;
151
  bool ret_val= str_to_time(str, length, l_time, &warning);
152
  if (ret_val || warning)
520.1.22 by Brian Aker
Second pass of thd cleanup
153
    make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
461 by Monty Taylor
Removed NullS. bu-bye.
154
                                 str, length, DRIZZLE_TIMESTAMP_TIME, NULL);
1 by brian
clean slate
155
  return ret_val;
156
}
157
158
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
159
void localtime_to_TIME(DRIZZLE_TIME *to, struct tm *from)
1 by brian
clean slate
160
{
161
  to->neg=0;
162
  to->second_part=0;
163
  to->year=	(int) ((from->tm_year+1900) % 10000);
164
  to->month=	(int) from->tm_mon+1;
165
  to->day=	(int) from->tm_mday;
166
  to->hour=	(int) from->tm_hour;
167
  to->minute=	(int) from->tm_min;
168
  to->second=   (int) from->tm_sec;
169
}
170
907.1.2 by Jay Pipes
Merging in old r902 temporal changes
171
void make_date(const DRIZZLE_TIME *l_time, String *str)
172
{
910.4.7 by Stewart Smith
ensure there is enough space allocated to String for string representation of date/time types.
173
  str->alloc(MAX_DATE_STRING_REP_LENGTH);
174
  uint32_t length= (uint32_t) my_date_to_str(l_time, str->c_ptr());
907.1.2 by Jay Pipes
Merging in old r902 temporal changes
175
  str->length(length);
176
  str->set_charset(&my_charset_bin);
177
}
178
179
180
void make_datetime(const DRIZZLE_TIME *l_time, String *str)
181
{
910.4.7 by Stewart Smith
ensure there is enough space allocated to String for string representation of date/time types.
182
  str->alloc(MAX_DATE_STRING_REP_LENGTH);
183
  uint32_t length= (uint32_t) my_datetime_to_str(l_time, str->c_ptr());
1 by brian
clean slate
184
  str->length(length);
185
  str->set_charset(&my_charset_bin);
186
}
187
188
1237.9.4 by Padraig O'Sullivan
Removed the inclusion of drizzled/field.h in the server_includes header file.
189
void make_truncated_value_warning(Session *session, 
190
                                  DRIZZLE_ERROR::enum_warning_level level,
1 by brian
clean slate
191
                                  const char *str_val,
1237.9.4 by Padraig O'Sullivan
Removed the inclusion of drizzled/field.h in the server_includes header file.
192
				                          uint32_t str_length,
398.1.1 by Monty Taylor
Remove typedef enum enum_drizzle_timestamp_type timestamp_type;
193
                                  enum enum_drizzle_timestamp_type time_type,
1 by brian
clean slate
194
                                  const char *field_name)
195
{
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
196
  char warn_buff[DRIZZLE_ERRMSG_SIZE];
1 by brian
clean slate
197
  const char *type_str;
383.1.12 by Brian Aker
Much closer toward UTF8 being around all the time...
198
  CHARSET_INFO *cs= &my_charset_utf8_general_ci;
1 by brian
clean slate
199
  char buff[128];
205 by Brian Aker
uint32 -> uin32_t
200
  String str(buff,(uint32_t) sizeof(buff), system_charset_info);
1 by brian
clean slate
201
  str.copy(str_val, str_length, system_charset_info);
202
  str[str_length]= 0;               // Ensure we have end 0 for snprintf
203
204
  switch (time_type) {
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
205
    case DRIZZLE_TIMESTAMP_DATE:
1 by brian
clean slate
206
      type_str= "date";
207
      break;
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
208
    case DRIZZLE_TIMESTAMP_TIME:
1 by brian
clean slate
209
      type_str= "time";
210
      break;
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
211
    case DRIZZLE_TIMESTAMP_DATETIME:  // FALLTHROUGH
1 by brian
clean slate
212
    default:
213
      type_str= "datetime";
214
      break;
215
  }
216
  if (field_name)
217
    cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff),
218
                       ER(ER_TRUNCATED_WRONG_VALUE_FOR_FIELD),
219
                       type_str, str.c_ptr(), field_name,
520.1.22 by Brian Aker
Second pass of thd cleanup
220
                       (uint32_t) session->row_count);
1 by brian
clean slate
221
  else
222
  {
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
223
    if (time_type > DRIZZLE_TIMESTAMP_ERROR)
1 by brian
clean slate
224
      cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff),
225
                         ER(ER_TRUNCATED_WRONG_VALUE),
226
                         type_str, str.c_ptr());
227
    else
228
      cs->cset->snprintf(cs, warn_buff, sizeof(warn_buff),
229
                         ER(ER_WRONG_VALUE), type_str, str.c_ptr());
230
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
231
  push_warning(session, level,
1 by brian
clean slate
232
               ER_TRUNCATED_WRONG_VALUE, warn_buff);
233
}
234
235
236
bool
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
237
calc_time_diff(DRIZZLE_TIME *l_time1, DRIZZLE_TIME *l_time2, int l_sign, int64_t *seconds_out,
1 by brian
clean slate
238
               long *microseconds_out)
239
{
240
  long days;
241
  bool neg;
152 by Brian Aker
longlong replacement
242
  int64_t microseconds;
1 by brian
clean slate
243
244
  /*
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
245
    We suppose that if first argument is DRIZZLE_TIMESTAMP_TIME
1 by brian
clean slate
246
    the second argument should be TIMESTAMP_TIME also.
247
    We should check it before calc_time_diff call.
248
  */
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
249
  if (l_time1->time_type == DRIZZLE_TIMESTAMP_TIME)  // Time value
1 by brian
clean slate
250
    days= (long)l_time1->day - l_sign * (long)l_time2->day;
251
  else
252
  {
895 by Brian Aker
Completion (?) of uint conversion.
253
    days= calc_daynr((uint32_t) l_time1->year,
254
		     (uint32_t) l_time1->month,
255
		     (uint32_t) l_time1->day);
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
256
    if (l_time2->time_type == DRIZZLE_TIMESTAMP_TIME)
1 by brian
clean slate
257
      days-= l_sign * (long)l_time2->day;
258
    else
895 by Brian Aker
Completion (?) of uint conversion.
259
      days-= l_sign*calc_daynr((uint32_t) l_time2->year,
260
			       (uint32_t) l_time2->month,
261
			       (uint32_t) l_time2->day);
1 by brian
clean slate
262
  }
263
398.1.8 by Monty Taylor
Enabled -Wlong-long.
264
  microseconds= ((int64_t)days*86400L +
152 by Brian Aker
longlong replacement
265
                 (int64_t)(l_time1->hour*3600L +
1 by brian
clean slate
266
                            l_time1->minute*60L +
267
                            l_time1->second) -
152 by Brian Aker
longlong replacement
268
                 l_sign*(int64_t)(l_time2->hour*3600L +
1 by brian
clean slate
269
                                   l_time2->minute*60L +
398.1.8 by Monty Taylor
Enabled -Wlong-long.
270
                                   l_time2->second)) * 1000000L +
152 by Brian Aker
longlong replacement
271
                (int64_t)l_time1->second_part -
272
                l_sign*(int64_t)l_time2->second_part;
1 by brian
clean slate
273
274
  neg= 0;
275
  if (microseconds < 0)
276
  {
277
    microseconds= -microseconds;
278
    neg= 1;
279
  }
280
  *seconds_out= microseconds/1000000L;
281
  *microseconds_out= (long) (microseconds%1000000L);
282
  return neg;
283
}
284
285
286
#endif