MCPcopy Create free account
hub / github.com/bytedance/bolt / tmToStringView

Method tmToStringView

bolt/type/Timestamp.cpp:269–389  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

267}
268
269StringView Timestamp::tmToStringView(
270 const std::tm& tmValue,
271 uint64_t nanos,
272 const TimestampToStringOptions& options,
273 char* const startPosition) {
274 BOLT_DCHECK_LT(nanos, 1'000'000'000);
275
276 const auto appendDigits = [](const int value,
277 const std::optional<uint32_t> minWidth,
278 char* const position) {
279 const auto numDigits = countDigits(value);
280 uint32_t offset = 0;
281 // Append leading zeros when there is the requirement for minimum width.
282 if (minWidth.has_value() && numDigits < minWidth.value()) {
283 const auto leadingZeros = minWidth.value() - numDigits;
284 std::memset(position, '0', leadingZeros);
285 offset += leadingZeros;
286 }
287 const auto [endPosition, errorCode] =
288 std::to_chars(position + offset, position + offset + numDigits, value);
289 std::ignore = endPosition;
290 BOLT_DCHECK_EQ(
291 errorCode,
292 std::errc(),
293 "Failed to convert value to varchar: {}.",
294 std::make_error_code(errorCode).message());
295 offset += numDigits;
296 return offset;
297 };
298
299 char* writePosition = startPosition;
300 if (options.mode != TimestampToStringOptions::Mode::kTimeOnly) {
301 int year = kTmYearBase + tmValue.tm_year;
302 const bool leadingPositiveSign = options.leadingPositiveSign && year > 9999;
303 const bool negative = year < 0;
304
305 // Sign.
306 if (negative) {
307 *writePosition++ = '-';
308 year = -year;
309 } else if (leadingPositiveSign) {
310 *writePosition++ = '+';
311 }
312
313 // Year.
314 writePosition += appendDigits(
315 year,
316 options.zeroPaddingYear ? std::optional<uint32_t>(4) : std::nullopt,
317 writePosition);
318
319 // Month.
320 *writePosition++ = '-';
321 writePosition += appendDigits(1 + tmValue.tm_mon, 2, writePosition);
322
323 // Day.
324 *writePosition++ = '-';
325 writePosition += appendDigits(tmValue.tm_mday, 2, writePosition);
326

Callers

nothing calls this directly

Calls 6

countDigitsFunction · 0.85
memsetFunction · 0.85
has_valueMethod · 0.80
messageMethod · 0.80
StringViewClass · 0.70
valueMethod · 0.45

Tested by

no test coverage detected