Skip to content

Commit 29b912b

Browse files
committed
fix: format string in maintainer mode
1 parent ba2b98e commit 29b912b

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

exporters/elasticsearch/src/es_log_recordable.cc

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
#include <iomanip>
45
#include <string>
56

67
#include "opentelemetry/exporters/elasticsearch/es_log_recordable.h"
@@ -70,21 +71,17 @@ void ElasticSearchRecordable::SetTimestamp(
7071
#if __cplusplus >= 202002L
7172
const std::string dateStr = std::format("{:%FT%T%Ez}", timePoint);
7273
#else
73-
constexpr auto dateToSecondsSize = sizeof("YYYY-MM-DDTHH:MM:SS") - 1;
74-
constexpr auto millisecondsSize = sizeof(".123456") - 1;
75-
constexpr auto timeZoneSize = sizeof("Z") - 1;
76-
constexpr auto dateSize = dateToSecondsSize + millisecondsSize + timeZoneSize;
77-
7874
std::time_t time = std::chrono::system_clock::to_time_t(timePoint);
7975
std::tm tm = *std::gmtime(&time);
8076
auto microseconds =
8177
std::chrono::duration_cast<std::chrono::microseconds>(timePoint.time_since_epoch()) %
8278
std::chrono::seconds(1);
8379

84-
char dateStr[dateSize + 1]; // example: 2024-10-18T07:26:00.123456Z
85-
std::snprintf(dateStr, sizeof(dateStr), "%04d-%02d-%02dT%02d:%02d:%02d.%06ldZ", tm.tm_year + 1900,
86-
tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
87-
static_cast<long>(microseconds.count()));
80+
std::ostringstream oss;
81+
oss << std::put_time(&tm, "%FT%T") << '.' << std::setw(6) << std::setfill('0')
82+
<< microseconds.count() << 'Z';
83+
84+
const std::string dateStr = oss.str();
8885
#endif
8986

9087
json_["@timestamp"] = dateStr;

0 commit comments

Comments
 (0)