1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
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.
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.
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
20
#include <drizzled/server_includes.h>
22
#include <drizzled/functions/time/date_format.h>
24
void Item_func_date_format::fix_length_and_dec()
26
Session* session= current_session;
28
Must use this_item() in case it's a local SP variable
29
(for ->max_length and ->str_value)
31
Item *arg1= args[1]->this_item();
34
const CHARSET_INFO * const cs= session->variables.collation_connection;
35
uint32_t repertoire= arg1->collation.repertoire;
36
if (!session->variables.lc_time_names->is_ascii)
37
repertoire|= MY_REPERTOIRE_EXTENDED;
38
collation.set(cs, arg1->collation.derivation, repertoire);
39
if (arg1->type() == STRING_ITEM)
40
{ // Optimize the normal case
42
max_length= format_length(&arg1->str_value) *
43
collation.collation->mbmaxlen;
48
max_length=cmin(arg1->max_length,(uint32_t) MAX_BLOB_WIDTH) * 10 *
49
collation.collation->mbmaxlen;
50
set_if_smaller(max_length,MAX_BLOB_WIDTH);
52
maybe_null=1; // If wrong date
55
bool Item_func_date_format::eq(const Item *item, bool binary_cmp) const
57
Item_func_date_format *item_func;
59
if (item->type() != FUNC_ITEM)
61
if (func_name() != ((Item_func*) item)->func_name())
65
item_func= (Item_func_date_format*) item;
66
if (!args[0]->eq(item_func->args[0], binary_cmp))
69
We must compare format string case sensitive.
70
This needed because format modifiers with different case,
71
for example %m and %M, have different meaning.
73
if (!args[1]->eq(item_func->args[1], 1))
78
uint32_t Item_func_date_format::format_length(const String *format)
81
const char *ptr=format->ptr();
82
const char *end=ptr+format->length();
84
for (; ptr != end ; ptr++)
86
if (*ptr != '%' || ptr == end-1)
91
case 'M': /* month, textual */
92
case 'W': /* day (of the week), textual */
93
size += 64; /* large for UTF8 locale data */
95
case 'D': /* day (of the month), numeric plus english suffix */
96
case 'Y': /* year, numeric, 4 digits */
97
case 'x': /* Year, used with 'v' */
98
case 'X': /* Year, used with 'v, where week starts with Monday' */
101
case 'a': /* locale's abbreviated weekday name (Sun..Sat) */
102
case 'b': /* locale's abbreviated month name (Jan.Dec) */
103
size += 32; /* large for UTF8 locale data */
105
case 'j': /* day of year (001..366) */
108
case 'U': /* week (00..52) */
109
case 'u': /* week (00..52), where week starts with Monday */
110
case 'V': /* week 1..53 used with 'x' */
111
case 'v': /* week 1..53 used with 'x', where week starts with Monday */
112
case 'y': /* year, numeric, 2 digits */
113
case 'm': /* month, numeric */
114
case 'd': /* day (of the month), numeric */
115
case 'h': /* hour (01..12) */
116
case 'I': /* --||-- */
117
case 'i': /* minutes, numeric */
118
case 'l': /* hour ( 1..12) */
119
case 'p': /* locale's AM or PM */
120
case 'S': /* second (00..61) */
121
case 's': /* seconds, numeric */
122
case 'c': /* month (0..12) */
123
case 'e': /* day (0..31) */
126
case 'k': /* hour ( 0..23) */
127
case 'H': /* hour (00..23; value > 23 OK, padding always 2-digit) */
128
size += 7; /* docs allow > 23, range depends on sizeof(unsigned int) */
130
case 'r': /* time, 12-hour (hh:mm:ss [AP]M) */
133
case 'T': /* time, 24-hour (hh:mm:ss) */
136
case 'f': /* microseconds */
139
case 'w': /* day (of the week), numeric */
151
String *Item_func_date_format::val_str(String *str)
160
if (get_arg0_date(&l_time, TIME_FUZZY_DATE))
166
if (!(res=args[0]->val_str(str)) ||
167
(str_to_time_with_warn(res->ptr(), res->length(), &l_time)))
170
l_time.year=l_time.month=l_time.day=0;
174
if (!(format = args[1]->val_str(str)) || !format->length())
180
size=format_length(format);
182
if (size < MAX_DATE_STRING_REP_LENGTH)
183
size= MAX_DATE_STRING_REP_LENGTH;
186
str= &value; // Save result here
187
if (str->alloc(size))
190
DATE_TIME_FORMAT date_time_format;
191
date_time_format.format.str= (char*) format->ptr();
192
date_time_format.format.length= format->length();
194
/* Create the result string */
195
str->set_charset(collation.collation);
196
if (!make_date_time(&date_time_format, &l_time,
197
is_time_format ? DRIZZLE_TIMESTAMP_TIME :
198
DRIZZLE_TIMESTAMP_DATE,