| 1 | // file : odb/boost/date-time/mysql/posix-time-traits.hxx |
| 2 | // copyright : Copyright (c) 2009-2015 Code Synthesis Tools CC |
| 3 | // license : GNU GPL v2; see accompanying LICENSE file |
| 4 | |
| 5 | #ifndef ODB_BOOST_DATE_TIME_MYSQL_POSIX_TIME_TRAITS_HXX |
| 6 | #define ODB_BOOST_DATE_TIME_MYSQL_POSIX_TIME_TRAITS_HXX |
| 7 | |
| 8 | #include <odb/pre.hxx> |
| 9 | |
| 10 | #include <cstdlib> // std::abs |
| 11 | |
| 12 | #include <boost/date_time/posix_time/posix_time_types.hpp> |
| 13 | |
| 14 | #include <odb/core.hxx> |
| 15 | #include <odb/mysql/traits.hxx> |
| 16 | #include <odb/boost/date-time/exceptions.hxx> |
| 17 | |
| 18 | namespace odb |
| 19 | { |
| 20 | namespace mysql |
| 21 | { |
| 22 | template <> |
| 23 | struct default_value_traits< ::boost::posix_time::ptime, id_datetime> |
| 24 | { |
| 25 | typedef ::boost::posix_time::ptime ptime; |
| 26 | typedef ::boost::posix_time::time_duration time_duration; |
| 27 | typedef ::boost::gregorian::date date; |
| 28 | typedef ptime value_type; |
| 29 | typedef ptime query_type; |
| 30 | typedef MYSQL_TIME image_type; |
| 31 | |
| 32 | static void |
| 33 | set_value (ptime& v, const MYSQL_TIME& i, bool is_null) |
| 34 | { |
| 35 | if (is_null) |
| 36 | v = ptime (::boost::date_time::not_a_date_time); |
| 37 | else |
| 38 | { |
| 39 | // Since MySQL 5.6.4, the microseconds part is no longer ignored. |
| 40 | // |
| 41 | unsigned long long fract_s (i.second_part); |
| 42 | fract_s = fract_s * time_duration::ticks_per_second () / 1000000ULL; |
| 43 | |
| 44 | v = ptime ( |
| 45 | date (i.year, i.month, i.day), |
| 46 | time_duration ( |
| 47 | i.hour, |
| 48 | i.minute, |
| 49 | i.second, |
| 50 | static_cast<time_duration::fractional_seconds_type> (fract_s))); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | static void |
| 55 | set_image (MYSQL_TIME& i, bool& is_null, const ptime& v) |
| 56 | { |
| 57 | if (v.is_special ()) |
| 58 | { |
| 59 | if (v.is_not_a_date_time ()) |
| 60 | is_null = true; |
| 61 | else |
| 62 | throw odb::boost::date_time::special_value (); |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | is_null = false; |
| 67 | i.neg = false; |
| 68 | |
| 69 | const date& d (v.date ()); |
| 70 | i.year = d.year (); |
| 71 | i.month = d.month (); |
| 72 | i.day = d.day (); |
| 73 | |
| 74 | const time_duration& t (v.time_of_day ()); |
| 75 | i.hour = t.hours (); |
| 76 | i.minute = t.minutes (); |
| 77 | i.second = t.seconds (); |
| 78 | |
| 79 | unsigned long long ms (t.fractional_seconds ()); |
| 80 | ms = ms * 1000000ULL / time_duration::ticks_per_second (); |
| 81 | i.second_part = static_cast<unsigned long> (ms); |
| 82 | } |
| 83 | } |
| 84 | }; |
| 85 | |
| 86 | template <> |
| 87 | struct default_value_traits< ::boost::posix_time::ptime, id_timestamp> |
| 88 | { |
| 89 | typedef ::boost::posix_time::ptime ptime; |
| 90 | typedef ::boost::posix_time::time_duration time_duration; |
| 91 | typedef ::boost::gregorian::date date; |
| 92 | typedef ptime value_type; |
| 93 | typedef ptime query_type; |
| 94 | typedef MYSQL_TIME image_type; |
| 95 | |
| 96 | static void |
| 97 | set_value (ptime& v, const MYSQL_TIME& i, bool is_null) |
| 98 | { |
| 99 | if (is_null) |
| 100 | v = ptime (::boost::date_time::not_a_date_time); |
| 101 | else |
| 102 | { |
| 103 | // Since MySQL 5.6.4, the microseconds part is no longer ignored. |
| 104 | // |
| 105 | unsigned long long fract_s (i.second_part); |
| 106 | fract_s = fract_s * time_duration::ticks_per_second () / 1000000ULL; |
| 107 | |
| 108 | v = ptime ( |
| 109 | date (i.year, i.month, i.day), |
| 110 | time_duration ( |
| 111 | i.hour, |
| 112 | i.minute, |
| 113 | i.second, |
| 114 | static_cast<time_duration::fractional_seconds_type> (fract_s))); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | static void |
| 119 | set_image (MYSQL_TIME& i, bool& is_null, const ptime& v) |
| 120 | { |
| 121 | if (v.is_special ()) |
| 122 | { |
| 123 | if (v.is_not_a_date_time ()) |
| 124 | is_null = true; |
| 125 | else |
| 126 | throw odb::boost::date_time::special_value (); |
| 127 | } |
| 128 | else if (v < ptime (date (1970, ::boost::date_time::Jan, 1), |
| 129 | time_duration (0, 0, 1)) || |
| 130 | v > ptime (date (2038, ::boost::date_time::Jan, 19), |
| 131 | time_duration (3, 14, 7))) |
| 132 | throw odb::boost::date_time::value_out_of_range (); |
| 133 | else |
| 134 | { |
| 135 | is_null = false; |
| 136 | i.neg = false; |
| 137 | |
| 138 | const date& d (v.date ()); |
| 139 | i.year = d.year (); |
| 140 | i.month = d.month (); |
| 141 | i.day = d.day (); |
| 142 | |
| 143 | const time_duration& t (v.time_of_day ()); |
| 144 | i.hour = t.hours (); |
| 145 | i.minute = t.minutes (); |
| 146 | i.second = t.seconds (); |
| 147 | |
| 148 | unsigned long long ms (t.fractional_seconds ()); |
| 149 | ms = ms * 1000000ULL / time_duration::ticks_per_second (); |
| 150 | i.second_part = static_cast<unsigned long> (ms); |
| 151 | } |
| 152 | } |
| 153 | }; |
| 154 | |
| 155 | template <> |
| 156 | struct default_value_traits< ::boost::posix_time::time_duration, id_time> |
| 157 | { |
| 158 | typedef ::boost::posix_time::time_duration time_duration; |
| 159 | typedef time_duration value_type; |
| 160 | typedef time_duration query_type; |
| 161 | typedef MYSQL_TIME image_type; |
| 162 | |
| 163 | static const unsigned short max_hours = 838; |
| 164 | |
| 165 | static void |
| 166 | set_value (time_duration& v, const MYSQL_TIME& i, bool is_null) |
| 167 | { |
| 168 | if (is_null) |
| 169 | v = time_duration (::boost::date_time::not_a_date_time); |
| 170 | else |
| 171 | { |
| 172 | // Since MySQL 5.6.4, the microseconds part is no longer ignored. |
| 173 | // |
| 174 | unsigned long long fract_s (i.second_part); |
| 175 | fract_s = fract_s * time_duration::ticks_per_second () / 1000000ULL; |
| 176 | |
| 177 | v = time_duration ( |
| 178 | i.hour, |
| 179 | i.minute, |
| 180 | i.second, |
| 181 | static_cast<time_duration::fractional_seconds_type> (fract_s)); |
| 182 | |
| 183 | if (i.neg) |
| 184 | v = v.invert_sign (); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | static void |
| 189 | set_image (MYSQL_TIME& i, bool& is_null, const time_duration& v) |
| 190 | { |
| 191 | if (v.is_special ()) |
| 192 | { |
| 193 | if (v.is_not_a_date_time ()) |
| 194 | is_null = true; |
| 195 | else |
| 196 | throw odb::boost::date_time::special_value (); |
| 197 | } |
| 198 | else if (std::abs (v.hours ()) > max_hours) |
| 199 | throw odb::boost::date_time::value_out_of_range (); |
| 200 | else |
| 201 | { |
| 202 | is_null = false; |
| 203 | i.neg = v.is_negative (); |
| 204 | |
| 205 | i.year = 0; |
| 206 | i.month = 0; |
| 207 | i.day = 0; |
| 208 | |
| 209 | i.hour = std::abs (v.hours ()); |
| 210 | i.minute = std::abs (v.minutes ()); |
| 211 | i.second = std::abs (v.seconds ()); |
| 212 | |
| 213 | // Some compilers, e.g., VC8, don't have 64-bit abs() overload. |
| 214 | // |
| 215 | time_duration::fractional_seconds_type sms (v.fractional_seconds ()); |
| 216 | unsigned long long ms (sms >= 0 ? sms : -sms); |
| 217 | ms = ms * 1000000ULL / time_duration::ticks_per_second (); |
| 218 | i.second_part = static_cast<unsigned long> (ms); |
| 219 | } |
| 220 | } |
| 221 | }; |
| 222 | |
| 223 | template <> |
| 224 | struct default_type_traits< ::boost::posix_time::ptime> |
| 225 | { |
| 226 | static const database_type_id db_type_id = id_datetime; |
| 227 | }; |
| 228 | |
| 229 | template <> |
| 230 | struct default_type_traits< ::boost::posix_time::time_duration> |
| 231 | { |
| 232 | static const database_type_id db_type_id = id_time; |
| 233 | }; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | #include <odb/post.hxx> |
| 238 | |
| 239 | #endif // ODB_BOOST_DATE_TIME_MYSQL_POSIX_TIME_TRAITS_HXX |