Skip to content

Commit

Permalink
♻️ use ISO8601 format for time_point
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdd committed Apr 4, 2024
1 parent 7d16d17 commit d9d4a95
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 6 deletions.
5 changes: 1 addition & 4 deletions include/logfmtxx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,8 @@ namespace logfmtxx {
}
else if constexpr (std::is_same_v<T, std::chrono::system_clock::time_point>) {
auto time = std::chrono::system_clock::to_time_t(value);
auto timetxt = std::string{std::ctime(&time)};
timetxt.pop_back();

auto stream = std::ostringstream{};
stream << std::quoted(timetxt);
stream << std::put_time(std::gmtime(&time), "%Y-%m-%dT%H:%M:%SZ");
return stream.str();
}
else if constexpr (std::is_same_v<T, level>) {
Expand Down
2 changes: 1 addition & 1 deletion tests/logger.spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct dummy_clock {
TEST_CASE("it should format log records with logfmt format") {
auto logger = logfmtxx::logger<dummy_clock>{
[](const std::string& message) {
CHECK(message == "time=\"Mon Jan 01 01:00:00 2001\" level=info message=\"hello\" key1=\"value1\" key2=\"value2\"");
CHECK(message == "time=2001-01-01T00:00:00Z level=info message=\"hello\" key1=\"value1\" key2=\"value2\"");
},
logfmtxx::field{"key1", "value1"}
};
Expand Down
2 changes: 1 addition & 1 deletion tests/serialization.spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ TEST_CASE("it should serialize arithmetic types") {
TEST_CASE("it should serialize time points") {
auto now = std::chrono::system_clock::from_time_t(978307200);
auto result = logfmtxx::details::serialize(now);
CHECK(result == "\"Mon Jan 01 01:00:00 2001\"");
CHECK(result == "2001-01-01T00:00:00Z");
}

TEST_CASE("it should serialize log levels") {
Expand Down

0 comments on commit d9d4a95

Please sign in to comment.