diff --git a/exporters/elasticsearch/src/es_log_recordable.cc b/exporters/elasticsearch/src/es_log_recordable.cc index e45747b63f..56d7cea71b 100644 --- a/exporters/elasticsearch/src/es_log_recordable.cc +++ b/exporters/elasticsearch/src/es_log_recordable.cc @@ -210,10 +210,11 @@ nlohmann::json ElasticSearchRecordable::GetJSON() noexcept void ElasticSearchRecordable::SetTimestamp( opentelemetry::common::SystemTimestamp timestamp) noexcept { + const std::chrono::system_clock::time_point timePoint{timestamp}; + // If built with with at least cpp 20 then use std::format // Otherwise use the old style to format the timestamp in UTC #if __cplusplus >= 202002L - const std::chrono::system_clock::time_point timePoint{timestamp}; const std::string dateStr = std::format("{:%FT%T%Ez}", timePoint); #else const static int dateToSecondsSize = 19; @@ -221,14 +222,13 @@ void ElasticSearchRecordable::SetTimestamp( const static int timeZoneSize = 1; const static int dateSize = dateToSecondsSize + millisecondsSize + timeZoneSize; - std::chrono::system_clock::time_point timestamp = std::chrono::system_clock::now(); - std::time_t time = std::chrono::system_clock::to_time_t(timestamp); + std::time_t time = std::chrono::system_clock::to_time_t(timePoint); std::tm tm = *std::gmtime(&time); char bufferDate[dateSize]; // example: 2024-10-18T07:26:00.123456Z std::strftime(bufferDate, sizeof(bufferDate), "%Y-%m-%dT%H:%M:%S", &tm); auto microseconds = - std::chrono::duration_cast(timestamp.time_since_epoch()) % + std::chrono::duration_cast(timePoint.time_since_epoch()) % std::chrono::seconds(1); char bufferMilliseconds[millisecondsSize];