Skip to content

Commit 9512ee3

Browse files
committed
reuse format logic
1 parent c68c453 commit 9512ee3

File tree

8 files changed

+510
-274
lines changed

8 files changed

+510
-274
lines changed

be/src/vec/runtime/time_value.h

Lines changed: 3 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -162,150 +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;
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));
309168
}
310169

311170
private:

0 commit comments

Comments
 (0)