using namespace isc::util;
using namespace isc::data;
+using namespace boost::posix_time;
namespace isc {
namespace db {
void
PsqlBindArray::addTimestamp(const boost::posix_time::ptime& timestamp) {
- time_t input_time = boost::posix_time::to_time_t(timestamp);
+ // Convert the ptime to time_t, then use the existing conversion
+ // function to make db time.
+ //
+ // Sadly boost::posix_time::to_time_t() was not added until 1.58,
+ // so do it ourselves.
+ ptime epoch(boost::gregorian::date(1970,1,1));
+ time_duration::sec_type since_epoch = (timestamp - epoch).total_seconds();
+ time_t input_time(since_epoch);
+
+ if (input_time > DatabaseConnection::MAX_DB_TIME) {
+ isc_throw(isc::BadValue, "Time value is too large: " << input_time);
+ }
+
// Converts to timestamp to local date/time string.
addTempString(PgSqlExchange::convertToDatabaseTime(input_time));
}
/// Precision is seconds.
///
/// @param timestamp Timestamp value to be sent to the database.
+ /// @throw BadValue if the timestamp exceeds DatabaseConnection::MAX_DB_TIME.
void addTimestamp(const boost::posix_time::ptime& timestamp);
/// @brief Adds a timestamp of the current time to the bind array.
ASSERT_NO_THROW(conn_->prepareStatement(statement[0]));
+ // Create an empty array.
+ PsqlBindArrayPtr bind_array(new PsqlBindArray());
+
+ // Make sure we catch values that are too big.
time_duration duration = hours(10) + minutes(14) + seconds(15);
+ ptime day_too_far(date(3021, Jan, 21), duration);
+ ASSERT_THROW_MSG(bind_array->addTimestamp(day_too_far), BadValue,
+ "Time value is too large: 33168132855");
- // US National Ice Cream day
+ // Now add reasonable day, US National Ice Cream day.
ptime nice_day(date(2021, Jul, 18), duration);
-
- // Add timestamp with default/fractional seconds.
- PsqlBindArrayPtr bind_array(new PsqlBindArray());
bind_array->addTimestamp(nice_day);
std::cout << "bind array: " << bind_array->toText() << std::endl;