diff options
author | Constantin Michael <constantin@codesynthesis.com> | 2011-12-04 23:30:58 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2011-12-05 11:29:55 +0200 |
commit | 2c12838925dc6fbc7d45ada0ddc40871c6a5d56b (patch) | |
tree | 73cf34737fd71a87703342654adc34079ae18168 | |
parent | b6e2b5a565e45f9381caa6d66439cd4ec7748b43 (diff) |
Handle microsecond resolution in PostgreSQL Qt date-time mappings
-rw-r--r-- | odb/qt/date-time/pgsql/qdate-time-traits.hxx | 30 | ||||
-rw-r--r-- | odb/qt/date-time/pgsql/qdate-traits.hxx | 9 | ||||
-rw-r--r-- | 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 <QtCore/QDateTime> -#include <odb/details/buffer.hxx> #include <odb/pgsql/traits.hxx> -#include <odb/qt/date-time/exceptions.hxx> 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 <uint> ( - 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 <qint64> (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<long long> (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<long long> (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 <QtCore/QDate> #include <odb/pgsql/traits.hxx> -#include <odb/qt/date-time/exceptions.hxx> 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<int> (endian_traits::ntoh (i) / 1000000)); + { + const QTime base (0, 0, 0); + + v = base.addMSecs ( + static_cast<int> (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<long long> (QTime (0, 0, 0).secsTo (v)) * 1000000); + static_cast<long long> (base.msecsTo (v)) * 1000); } } }; |