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 ++++++++-------------------- odb/qt/date-time/pgsql/qdate-traits.hxx | 9 ++++++--- odb/qt/date-time/pgsql/qtime-traits.hxx | 12 ++++++++--- 3 files changed, 23 insertions(+), 28 deletions(-) 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); } } }; diff --git a/odb/qt/date-time/pgsql/qdate-traits.hxx b/odb/qt/date-time/pgsql/qdate-traits.hxx index 94345e1..67a60df 100644 --- a/odb/qt/date-time/pgsql/qdate-traits.hxx +++ b/odb/qt/date-time/pgsql/qdate-traits.hxx @@ -11,7 +11,6 @@ #include #include -#include namespace odb { @@ -38,7 +37,10 @@ namespace odb // v.setDate (0, 0, 0); else - v = QDate (2000, 1, 1).addDays (endian_traits::ntoh (i)); + { + const QDate pg_epoch (2000, 1, 1); + v = pg_epoch.addDays (endian_traits::ntoh (i)); + } } static void @@ -49,7 +51,8 @@ namespace odb else { is_null = false; - i = endian_traits::hton (QDate (2000, 1, 1).daysTo (v)); + const QDate pg_epoch (2000, 1, 1); + i = endian_traits::hton (pg_epoch.daysTo (v)); } } }; diff --git a/odb/qt/date-time/pgsql/qtime-traits.hxx b/odb/qt/date-time/pgsql/qtime-traits.hxx index c822017..22b8326 100644 --- a/odb/qt/date-time/pgsql/qtime-traits.hxx +++ b/odb/qt/date-time/pgsql/qtime-traits.hxx @@ -38,8 +38,12 @@ namespace odb // v.setHMS (24, 0, 0); else - v = QTime (0, 0, 0).addSecs ( - static_cast (endian_traits::ntoh (i) / 1000000)); + { + const QTime base (0, 0, 0); + + v = base.addMSecs ( + static_cast (endian_traits::ntoh (i) / 1000)); + } } static void @@ -50,8 +54,10 @@ namespace odb else { is_null = false; + const QTime base (0, 0, 0); + i = endian_traits::hton ( - static_cast (QTime (0, 0, 0).secsTo (v)) * 1000000); + static_cast (base.msecsTo (v)) * 1000); } } }; -- cgit v1.1