21 #include <drizzled/function/time/date_format.h>
22 #include <drizzled/session.h>
23 #include <drizzled/time_functions.h>
24 #include <drizzled/internal/m_string.h>
25 #include <drizzled/typelib.h>
26 #include <drizzled/system_variables.h>
41 type::timestamp_t type,
String *str)
47 const char *ptr, *end;
55 end= (ptr= format->c_ptr()) + format->length();
56 for (; ptr != end ; ptr++)
58 if (*ptr !=
'%' || ptr+1 == end)
66 str->append(locale->month_names->type_names[l_time->month-1], strlen(locale->month_names->type_names[l_time->month-1]));
71 str->append(locale->ab_month_names->type_names[l_time->month-1], strlen(locale->ab_month_names->type_names[l_time->month-1]));
74 if (type == type::DRIZZLE_TIMESTAMP_TIME)
76 weekday= calc_weekday(calc_daynr(l_time->year,l_time->month,
78 str->append(locale->day_names->type_names[weekday], strlen(locale->day_names->type_names[weekday]));
81 if (type == type::DRIZZLE_TIMESTAMP_TIME)
83 weekday=calc_weekday(calc_daynr(l_time->year,l_time->month, l_time->day),0);
84 str->append(locale->ab_day_names->type_names[weekday], strlen(locale->ab_day_names->type_names[weekday]));
87 if (type == type::DRIZZLE_TIMESTAMP_TIME)
89 length= internal::int10_to_str(l_time->day, intbuff, 10) - intbuff;
90 str->append_with_prefill(intbuff, length, 1,
'0');
91 if (l_time->day >= 10 && l_time->day <= 19)
92 str->append(STRING_WITH_LEN(
"th"));
95 switch (l_time->day %10) {
97 str->append(STRING_WITH_LEN(
"st"));
100 str->append(STRING_WITH_LEN(
"nd"));
103 str->append(STRING_WITH_LEN(
"rd"));
106 str->append(STRING_WITH_LEN(
"th"));
112 length= internal::int10_to_str(l_time->year, intbuff, 10) - intbuff;
113 str->append_with_prefill(intbuff, length, 4,
'0');
116 length= internal::int10_to_str(l_time->year%100, intbuff, 10) - intbuff;
117 str->append_with_prefill(intbuff, length, 2,
'0');
120 length= internal::int10_to_str(l_time->month, intbuff, 10) - intbuff;
121 str->append_with_prefill(intbuff, length, 2,
'0');
124 length= internal::int10_to_str(l_time->month, intbuff, 10) - intbuff;
125 str->append_with_prefill(intbuff, length, 1,
'0');
128 length= internal::int10_to_str(l_time->day, intbuff, 10) - intbuff;
129 str->append_with_prefill(intbuff, length, 2,
'0');
132 length= internal::int10_to_str(l_time->day, intbuff, 10) - intbuff;
133 str->append_with_prefill(intbuff, length, 1,
'0');
136 length= internal::int10_to_str(l_time->second_part, intbuff, 10) - intbuff;
137 str->append_with_prefill(intbuff, length, 6,
'0');
140 length= internal::int10_to_str(l_time->hour, intbuff, 10) - intbuff;
141 str->append_with_prefill(intbuff, length, 2,
'0');
145 hours_i= (l_time->hour%24 + 11)%12+1;
146 length= internal::int10_to_str(hours_i, intbuff, 10) - intbuff;
147 str->append_with_prefill(intbuff, length, 2,
'0');
150 length= internal::int10_to_str(l_time->minute, intbuff, 10) - intbuff;
151 str->append_with_prefill(intbuff, length, 2,
'0');
154 if (type == type::DRIZZLE_TIMESTAMP_TIME)
156 length= internal::int10_to_str(calc_daynr(l_time->year,l_time->month,
158 calc_daynr(l_time->year,1,1) + 1, intbuff, 10) - intbuff;
159 str->append_with_prefill(intbuff, length, 3,
'0');
162 length= internal::int10_to_str(l_time->hour, intbuff, 10) - intbuff;
163 str->append_with_prefill(intbuff, length, 1,
'0');
166 hours_i= (l_time->hour%24 + 11)%12+1;
167 length= internal::int10_to_str(hours_i, intbuff, 10) - intbuff;
168 str->append_with_prefill(intbuff, length, 1,
'0');
171 hours_i= l_time->hour%24;
172 str->append(hours_i < 12 ?
"AM" :
"PM",2);
175 length= snprintf(intbuff,
sizeof(intbuff),
176 ((l_time->hour % 24) < 12) ?
177 "%02d:%02d:%02d AM" :
"%02d:%02d:%02d PM",
178 (l_time->hour+11)%12+1,
181 str->append(intbuff, length);
185 length= internal::int10_to_str(l_time->second, intbuff, 10) - intbuff;
186 str->append_with_prefill(intbuff, length, 2,
'0');
189 length= snprintf(intbuff,
sizeof(intbuff),
194 str->append(intbuff, length);
200 if (type == type::DRIZZLE_TIMESTAMP_TIME)
202 length= internal::int10_to_str(calc_week(l_time,
204 WEEK_FIRST_WEEKDAY : WEEK_MONDAY_FIRST,
206 intbuff, 10) - intbuff;
207 str->append_with_prefill(intbuff, length, 2,
'0');
214 if (type == type::DRIZZLE_TIMESTAMP_TIME)
216 length= internal::int10_to_str(calc_week(l_time,
218 (WEEK_YEAR | WEEK_FIRST_WEEKDAY) :
219 (WEEK_YEAR | WEEK_MONDAY_FIRST)),
221 intbuff, 10) - intbuff;
222 str->append_with_prefill(intbuff, length, 2,
'0');
229 if (type == type::DRIZZLE_TIMESTAMP_TIME)
231 (void) calc_week(l_time,
233 WEEK_YEAR | WEEK_FIRST_WEEKDAY :
234 WEEK_YEAR | WEEK_MONDAY_FIRST),
236 length= internal::int10_to_str(year, intbuff, 10) - intbuff;
237 str->append_with_prefill(intbuff, length, 4,
'0');
241 if (type == type::DRIZZLE_TIMESTAMP_TIME)
243 weekday=calc_weekday(calc_daynr(l_time->year,l_time->month,
245 length= internal::int10_to_str(weekday, intbuff, 10) - intbuff;
246 str->append_with_prefill(intbuff, length, 1,
'0');
258 void Item_func_date_format::fix_length_and_dec()
263 const charset_info_st *
const cs= getSession().variables.getCollation();
264 collation.set(cs, arg1->collation.derivation);
265 if (arg1->type() == STRING_ITEM)
268 max_length= format_length(&arg1->str_value) *
269 collation.collation->mbmaxlen;
274 max_length= min(arg1->max_length,(uint32_t) MAX_BLOB_WIDTH) * 10 *
275 collation.collation->mbmaxlen;
276 set_if_smaller(max_length,MAX_BLOB_WIDTH);
281 bool Item_func_date_format::eq(
const Item *item,
bool binary_cmp)
const
285 if (item->type() != FUNC_ITEM)
287 if (func_name() != ((
Item_func*) item)->func_name())
292 if (!args[0]->eq(item_func->args[0], binary_cmp))
299 if (!args[1]->eq(item_func->args[1], 1))
304 uint32_t Item_func_date_format::format_length(
const String *format)
307 const char *ptr=format->ptr();
308 const char *end=ptr+format->length();
310 for (; ptr != end ; ptr++)
312 if (*ptr !=
'%' || ptr == end-1)
386 if (get_arg0_date(l_time, TIME_FUZZY_DATE))
391 String *res=args[0]->val_str(str);
392 if (not res || str_to_time_with_warn(getSession(), *res, l_time))
395 l_time.year=l_time.month=l_time.day=0;
399 if (!(format = args[1]->val_str(str)) || !format->length())
405 size= format_length(format);
407 if (size < type::Time::MAX_STRING_LENGTH)
408 size= type::Time::MAX_STRING_LENGTH;
416 str->set_charset(collation.collation);
419 is_time_format ? type::DRIZZLE_TIMESTAMP_TIME :
420 type::DRIZZLE_TIMESTAMP_DATE,
TODO: Rename this file - func.h is stupid.
static bool make_date_time(Session &session, String *format, type::Time *l_time, type::timestamp_t type, String *str)
drizzle_system_variables & variables