@@ -162,222 +162,9 @@ class TimeValue {
162162 time = -time;
163163 }
164164
165- int32_t hour = TimeValue::hour (time);
166- int32_t minute = TimeValue::minute (time);
167- int32_t second = TimeValue::second (time);
168- int32_t microsecond = TimeValue::microsecond (time);
169-
170- char * const begin = to;
171- char buf[64 ];
172- char * pos = nullptr ;
173- char * cursor = buf;
174- const char * ptr = format;
175- const char * end = format + len;
176- char ch = ' \0 ' ;
177-
178- while (ptr < end) {
179- if (to - begin + SAFE_FORMAT_STRING_MARGIN > max_valid_length) [[unlikely]] {
180- return false ;
181- }
182- if (*ptr != ' %' || (ptr + 1 ) == end) {
183- *to++ = *ptr++;
184- continue ;
185- }
186- ptr++;
187- switch (ch = *ptr++) {
188- case ' H' :
189- // Hour (00..838 for TIME type, with at least 2 digits)
190- if (hour < 100 ) {
191- to = write_two_digits_to_string (hour, to);
192- } else {
193- pos = int_to_str (hour, cursor);
194- to = append_with_prefix (cursor, static_cast <int >(pos - cursor), ' 0' , 2 , to);
195- }
196- break ;
197- case ' h' :
198- case ' I' :
199- // Hour (01..12)
200- to = write_two_digits_to_string ((hour % 24 + 11 ) % 12 + 1 , to);
201- break ;
202- case ' i' :
203- // Minutes, numeric (00..59)
204- to = write_two_digits_to_string (minute, to);
205- break ;
206- case ' k' :
207- // Hour (0..23) without leading zero
208- pos = int_to_str (hour, cursor);
209- to = append_with_prefix (cursor, static_cast <int >(pos - cursor), ' 0' , 1 , to);
210- break ;
211- case ' l' :
212- // Hour (1..12) without leading zero
213- pos = int_to_str ((hour % 24 + 11 ) % 12 + 1 , cursor);
214- to = append_with_prefix (cursor, static_cast <int >(pos - cursor), ' 0' , 1 , to);
215- break ;
216- case ' s' :
217- case ' S' :
218- // Seconds (00..59)
219- to = write_two_digits_to_string (second, to);
220- break ;
221- case ' f' :
222- // Microseconds (000000..999999)
223- pos = int_to_str (microsecond, cursor);
224- to = append_with_prefix (cursor, static_cast <int >(pos - cursor), ' 0' , 6 , to);
225- break ;
226- case ' p' : {
227- // AM or PM
228- if (hour % 24 >= 12 ) {
229- to = append_string (" PM" , to);
230- } else {
231- to = append_string (" AM" , to);
232- }
233- break ;
234- }
235- case ' r' : {
236- // Time, 12-hour (hh:mm:ss followed by AM or PM)
237- int32_t hour_12 = (hour + 11 ) % 12 + 1 ;
238- *to++ = (char )(' 0' + (hour_12 / 10 ));
239- *to++ = (char )(' 0' + (hour_12 % 10 ));
240- *to++ = ' :' ;
241- *to++ = (char )(' 0' + (minute / 10 ));
242- *to++ = (char )(' 0' + (minute % 10 ));
243- *to++ = ' :' ;
244- *to++ = (char )(' 0' + (second / 10 ));
245- *to++ = (char )(' 0' + (second % 10 ));
246- if (hour % 24 >= 12 ) {
247- to = append_string (" PM" , to);
248- } else {
249- to = append_string (" AM" , to);
250- }
251- break ;
252- }
253- case ' T' : {
254- // Time, 24-hour (hh:mm:ss or hhh:mm:ss for TIME type)
255- if (hour < 100 ) {
256- *to++ = (char )(' 0' + (hour / 10 ));
257- *to++ = (char )(' 0' + (hour % 10 ));
258- } else {
259- // For hours >= 100, convert to string with at least 2 digits
260- pos = int_to_str (hour, cursor);
261- to = append_with_prefix (cursor, static_cast <int >(pos - cursor), ' 0' , 2 , to);
262- }
263- *to++ = ' :' ;
264- *to++ = (char )(' 0' + (minute / 10 ));
265- *to++ = (char )(' 0' + (minute % 10 ));
266- *to++ = ' :' ;
267- *to++ = (char )(' 0' + (second / 10 ));
268- *to++ = (char )(' 0' + (second % 10 ));
269- break ;
270- }
271- case ' %' :
272- *to++ = ' %' ;
273- break ;
274- case ' Y' :
275- // Year, 4 digits - 4 zeros
276- to = append_string (" 0000" , to);
277- break ;
278- case ' y' :
279- case ' m' :
280- case ' d' :
281- // Year (2 digits), Month, Day - insert 2 zeros
282- to = write_two_digits_to_string (0 , to);
283- break ;
284- case ' c' :
285- case ' e' :
286- // Month (0..12) or Day without leading zero - insert 1 zero
287- to = append_string (" 0" , to);
288- break ;
289- case ' M' :
290- case ' W' :
291- case ' j' :
292- case ' D' :
293- case ' U' :
294- case ' u' :
295- case ' V' :
296- case ' v' :
297- case ' x' :
298- case ' X' :
299- case ' w' :
300- // These specifiers are not supported for TIME type
301- return false ;
302- default :
303- *to++ = ch;
304- break ;
305- }
306- }
307- *to++ = ' \0 ' ;
308- return true ;
309- }
310-
311- private:
312- static constexpr char digits100[201 ] =
313- " 00010203040506070809"
314- " 10111213141516171819"
315- " 20212223242526272829"
316- " 30313233343536373839"
317- " 40414243444546474849"
318- " 50515253545556575859"
319- " 60616263646566676869"
320- " 70717273747576777879"
321- " 80818283848586878889"
322- " 90919293949596979899" ;
323-
324- static char * int_to_str (uint64_t val, char * to) {
325- char buf[64 ];
326- char * ptr = buf;
327- // Use do/while for 0 value
328- do {
329- *ptr++ = ' 0' + (val % 10 );
330- val /= 10 ;
331- } while (val);
332-
333- while (ptr > buf) {
334- *to++ = *--ptr;
335- }
336- return to;
337- }
338-
339- static char * append_string (const char * from, char * to) {
340- while (*from) {
341- *to++ = *from++;
342- }
343- return to;
344- }
345-
346- static char * append_with_prefix (const char * str, int str_len, char prefix, int target_len,
347- char * to) {
348- // full_len is the lower bound. if less, use prefix to pad. if greater, accept all.
349- int diff = target_len - str_len;
350- // use prefix to pad
351- while (diff-- > 0 ) {
352- *to++ = prefix;
353- }
354-
355- memcpy (to, str, str_len);
356- return to + str_len;
357- }
358-
359- static char * write_two_digits_to_string (int number, char * dst) {
360- memcpy (dst, &digits100[number * 2 ], 2 );
361- return dst + 2 ;
362- }
363-
364- static bool is_date_related_specifier (char spec) {
365- switch (spec) {
366- case ' Y' :
367- case ' y' :
368- case ' M' :
369- case ' m' :
370- case ' b' :
371- case ' c' :
372- case ' d' :
373- case ' D' :
374- case ' e' :
375- case ' j' :
376- case ' U' :
377- return true ;
378- default :
379- return false ;
380- }
165+ return DateV2Value<DateTimeV2ValueType>::to_format_string_without_check (
166+ format, len, to, max_valid_length, 0 , 0 , 0 , TimeValue::hour (time),
167+ TimeValue::minute (time), TimeValue::second (time), TimeValue::microsecond (time));
381168 }
382169};
383170} // namespace doris
0 commit comments