|
1 | 1 | // Copyright The OpenTelemetry Authors
|
2 | 2 | // SPDX-License-Identifier: Apache-2.0
|
3 | 3 |
|
| 4 | +#include <iomanip> |
4 | 5 | #include <string>
|
5 | 6 |
|
6 | 7 | #include "opentelemetry/exporters/elasticsearch/es_log_recordable.h"
|
@@ -70,21 +71,17 @@ void ElasticSearchRecordable::SetTimestamp(
|
70 | 71 | #if __cplusplus >= 202002L
|
71 | 72 | const std::string dateStr = std::format("{:%FT%T%Ez}", timePoint);
|
72 | 73 | #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 |
| - |
78 | 74 | std::time_t time = std::chrono::system_clock::to_time_t(timePoint);
|
79 | 75 | std::tm tm = *std::gmtime(&time);
|
80 | 76 | auto microseconds =
|
81 | 77 | std::chrono::duration_cast<std::chrono::microseconds>(timePoint.time_since_epoch()) %
|
82 | 78 | std::chrono::seconds(1);
|
83 | 79 |
|
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(); |
88 | 85 | #endif
|
89 | 86 |
|
90 | 87 | json_["@timestamp"] = dateStr;
|
|
0 commit comments