Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions include/rfl/Timestamp.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef RFL_TIMESTAMP_HPP_
#define RFL_TIMESTAMP_HPP_

#include <cstring>
#include <ctime>
#include <iomanip>
#include <sstream>
Expand Down Expand Up @@ -35,7 +36,7 @@ class Timestamp {
/// @throws std::runtime_error if the string doesn't match the format
Timestamp(const char* _str) : tm_(std::tm{}) {
const auto r = strptime(_str, _format.str().c_str(), &tm_);
if (r == NULL) {
if (r == NULL || *r != '\0') {
throw std::runtime_error("String '" + std::string(_str) +
"' did not match format '" + Format().str() +
"'.");
Expand Down Expand Up @@ -169,7 +170,12 @@ class Timestamp {
if (input.fail()) {
return NULL;
}
return (char*)(_s + input.tellg());
const auto pos = input.tellg();
if (pos == std::streampos(-1)) {
// The stream reached EOF, so all of the input was consumed.
return (char*)(_s + std::strlen(_s));
}
return (char*)(_s + static_cast<std::ptrdiff_t>(pos));
}
#endif

Expand Down
9 changes: 9 additions & 0 deletions tests/json/test_timestamp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,13 @@ TEST(json, test_timestamp) {
bart,
R"({"firstName":"Bart","lastName":"Simpson","birthday":"1987-04-19"})");
}

TEST(json, test_timestamp_trailing_characters) {
EXPECT_TRUE(!TS::from_string("1987-04-19T10:00:00"));
EXPECT_TRUE(!TS::from_string("1987-04-19x"));

const auto res = rfl::json::read<Person>(
R"({"firstName":"Bart","lastName":"Simpson","birthday":"1987-04-19x"})");
EXPECT_TRUE(!res);
}
} // namespace test_timestamp
Loading