From 2c12838925dc6fbc7d45ada0ddc40871c6a5d56b Mon Sep 17 00:00:00 2001 From: Constantin Michael Date: Sun, 4 Dec 2011 23:30:58 +0200 Subject: Handle microsecond resolution in PostgreSQL Qt date-time mappings --- odb/qt/date-time/pgsql/qdate-time-traits.hxx | 30 ++++++++-------------------- 1 file changed, 8 insertions(+), 22 deletions(-) (limited to 'odb/qt/date-time/pgsql/qdate-time-traits.hxx') diff --git a/odb/qt/date-time/pgsql/qdate-time-traits.hxx b/odb/qt/date-time/pgsql/qdate-time-traits.hxx index 288425e..11200cf 100644 --- a/odb/qt/date-time/pgsql/qdate-time-traits.hxx +++ b/odb/qt/date-time/pgsql/qdate-time-traits.hxx @@ -10,9 +10,7 @@ #include -#include #include -#include namespace odb { @@ -31,11 +29,6 @@ namespace odb typedef QDateTime query_type; typedef long long image_type; - // The difference between the Unix epoch and the PostgreSQL epoch - // in seconds. - // - static const long long epoch_diff = 946684800LL; - static void set_value (QDateTime& v, long long i, bool is_null) { @@ -44,9 +37,11 @@ namespace odb // v = QDateTime (); else - v.setTime_t ( - static_cast ( - endian_traits::ntoh (i) / 1000000 + epoch_diff)); + { + const QDateTime pg_epoch (QDate (2000, 1, 1), QTime (0, 0, 0)); + v = pg_epoch.addMSecs ( + static_cast (endian_traits::ntoh (i) / 1000LL)); + } } static void @@ -54,21 +49,12 @@ namespace odb { if (v.isNull ()) is_null = true; - // QDateTime::toTime_t returns an unsigned integer. Values less - // than the Unix epoch are not supported. - // - else if (v < QDateTime (QDate (1970, 1, 1), - QTime (0, 0, 0), - Qt::UTC)) - throw odb::qt::date_time::value_out_of_range (); else { is_null = false; - - long long pg_seconds (static_cast (v.toTime_t ()) - - epoch_diff); - - i = endian_traits::hton (pg_seconds * 1000000); + const QDateTime pg_epoch (QDate (2000, 1, 1), QTime (0, 0, 0)); + i = endian_traits::hton ( + static_cast (pg_epoch.msecsTo (v)) * 1000LL); } } }; -- cgit v1.1