Skip to content

Commit

Permalink
fix milliseconds in TimestampToISO8601TimeString
Browse files Browse the repository at this point in the history
System clock is using nanosecond precision,
to get number of millisecond from epoch start,
it is necessary to use duration_cast to milliseconds.
  • Loading branch information
Karry committed Jan 31, 2024
1 parent 7e7378e commit 8dbbbfc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
10 changes: 9 additions & 1 deletion Tests/src/TimeParse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@

TEST_CASE("Parse ISO8601 time string") {
osmscout::Timestamp ts;
std::string testString="2017-03-12T14:31:56.0Z";
std::string testString="2017-03-12T14:31:56.000Z";
REQUIRE(osmscout::ParseISO8601TimeString(testString, ts));
REQUIRE(std::chrono::duration_cast<std::chrono::milliseconds>(ts.time_since_epoch()).count()==1489329116000);
REQUIRE(osmscout::TimestampToISO8601TimeString(ts)==testString);
}

TEST_CASE("Parse ISO8601 time string with millisecond precision") {
osmscout::Timestamp ts;
std::string testString="2017-03-12T14:31:56.012Z";
REQUIRE(osmscout::ParseISO8601TimeString(testString, ts));
REQUIRE(std::chrono::duration_cast<std::chrono::milliseconds>(ts.time_since_epoch()).count()==1489329116012);
REQUIRE(osmscout::TimestampToISO8601TimeString(ts)==testString);
}
4 changes: 2 additions & 2 deletions libosmscout/src/osmscout/util/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -718,9 +718,9 @@ namespace osmscout {
stream << buff.data();

// add milliseconds
auto millisFromEpoch = timestamp.time_since_epoch().count();
auto millisFromEpoch = duration_cast<milliseconds>(timestamp.time_since_epoch()).count();
stream << ".";
stream << (millisFromEpoch - ((millisFromEpoch / 1000) * 1000));
stream << std::setfill('0') << std::setw(3) << (millisFromEpoch - ((millisFromEpoch / 1000) * 1000));
stream << "Z";
return stream.str();
}
Expand Down

0 comments on commit 8dbbbfc

Please sign in to comment.