aboutsummaryrefslogtreecommitdiff
path: root/odb/boost/date-time/pgsql/posix-time-traits.hxx
blob: 42ab7c46ee734ad493c29f958ee73dcafbac477a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// file      : odb/boost/date-time/pgsql/posix-time-traits.hxx
// copyright : Copyright (c) 2009-2019 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#ifndef ODB_BOOST_DATE_TIME_PGSQL_POSIX_TIME_TRAITS_HXX
#define ODB_BOOST_DATE_TIME_PGSQL_POSIX_TIME_TRAITS_HXX

#include <odb/pre.hxx>

#include <boost/date_time/posix_time/posix_time_types.hpp>

#include <odb/core.hxx>
#include <odb/pgsql/traits.hxx>
#include <odb/boost/date-time/exceptions.hxx>

namespace odb
{
  namespace pgsql
  {
    // Implementation of the mapping between boost::posix_time::ptime and
    // PostgreSQL TIMESTAMP. TIMESTAMP values are stored as micro-seconds
    // since the PostgreSQL epoch 2000-01-01.
    //
    template <>
    struct default_value_traits< ::boost::posix_time::ptime, id_timestamp>
    {
      typedef ::boost::gregorian::date date;
      typedef ::boost::posix_time::ptime ptime;
      typedef ::boost::posix_time::time_duration time_duration;

      typedef details::endian_traits endian_traits;

      typedef ptime value_type;
      typedef ptime query_type;
      typedef long long image_type;

      static const long long neg_inf = -0x7fffffffffffffffLL - 1;
      static const long long pos_inf = 0x7fffffffffffffffLL;

      static void
      set_value (ptime& v, long long i, bool is_null)
      {
        if (is_null)
          v = ptime (::boost::date_time::not_a_date_time);
        else
        {
          i = endian_traits::ntoh (i);

          if (i == neg_inf)
            v = ptime (::boost::date_time::neg_infin);
          else if (i == pos_inf)
            v = ptime (::boost::date_time::pos_infin);
          else
          {
            const ptime pg_epoch (date (2000, 1, 1),
                                  time_duration (0, 0, 0));

            // We need to split the microsecond image into hours and
            // microseconds to avoid overflow during the fractional seconds
            // calculation.
            //
            time_duration::hour_type h (
              static_cast<time_duration::hour_type> (i / 3600000000LL));

            i -= static_cast<long long> (h) * 3600000000LL;

            v = pg_epoch +
              time_duration (
                h,
                0,
                0,
                static_cast<time_duration::fractional_seconds_type> (
                  i * time_duration::ticks_per_second () / 1000000LL));
          }
        }
      }

      static void
      set_image (long long& i, bool& is_null, const ptime& v)
      {
        is_null = false;

        if (v.is_special ())
        {
          if (v.is_not_a_date_time ())
            is_null = true;
          else if (v.is_neg_infinity ())
            i = endian_traits::hton (neg_inf);
          else if (v.is_pos_infinity ())
            i = endian_traits::hton (pos_inf);
          else
            throw odb::boost::date_time::special_value ();
        }
        else
        {
          const ptime pg_epoch (date (2000, 1, 1), time_duration (0, 0, 0));
          i = endian_traits::hton (
            static_cast<long long> ((v - pg_epoch).total_microseconds ()));
        }
      }
    };

    // Implementation of the mapping between boost::posix_time::time_duration
    // and PostgreSQL TIME. The TIME values are stored as micro-seconds since
    // 00:00:00.
    //
    template <>
    struct default_value_traits< ::boost::posix_time::time_duration, id_time>
    {
      typedef ::boost::posix_time::time_duration time_duration;

      typedef details::endian_traits endian_traits;

      typedef time_duration value_type;
      typedef time_duration query_type;
      typedef long long image_type;

      static void
      set_value (time_duration& v, long long i, bool is_null)
      {
        if (is_null)
          v = time_duration (::boost::date_time::not_a_date_time);
        else
          v = time_duration (
            0,
            0,
            0,
            static_cast<time_duration::fractional_seconds_type> (
              endian_traits::ntoh (i) * time_duration::ticks_per_second () /
              1000000LL));
      }

      static void
      set_image (long long& i, bool& is_null, const time_duration& v)
      {
        if (v.is_special ())
        {
          if (v.is_not_a_date_time ())
            is_null = true;
          else
            throw odb::boost::date_time::special_value ();
        }
        else if (v.hours () < 0 || v.hours () > 24)
          throw odb::boost::date_time::value_out_of_range ();
        else
        {
          is_null = false;
          i = endian_traits::hton (
            static_cast<long long> (v.total_microseconds ()));
        }
      }
    };

    template <>
    struct default_type_traits< ::boost::posix_time::ptime>
    {
      static const database_type_id db_type_id = id_timestamp;
    };

    template <>
    struct default_type_traits< ::boost::posix_time::time_duration>
    {
      static const database_type_id db_type_id = id_time;
    };
  }
}

#include <odb/post.hxx>

#endif // ODB_BOOST_DATE_TIME_PGSQL_POSIX_TIME_TRAITS_HXX