11 #ifndef MSGPACK_V1_TYPE_CPP11_CHRONO_HPP
12 #define MSGPACK_V1_TYPE_CPP11_CHRONO_HPP
20 #include <boost/numeric/conversion/cast.hpp>
30 template <
typename Clock,
typename Duration>
31 struct as<std::chrono::time_point<Clock, Duration>> {
35 std::chrono::time_point<Clock, Duration> tp;
39 _msgpack_load32(uint32_t, o.
via.
ext.
data(), &sec);
40 tp += std::chrono::seconds(sec);
44 _msgpack_load64(uint64_t, o.
via.
ext.
data(), &value);
45 uint32_t nanosec = boost::numeric_cast<uint32_t>(value >> 34);
46 uint64_t sec = value & 0x00000003ffffffffLL;
47 tp += std::chrono::duration_cast<Duration>(
48 std::chrono::nanoseconds(nanosec));
49 tp += std::chrono::seconds(sec);
53 _msgpack_load32(uint32_t, o.
via.
ext.
data(), &nanosec);
55 _msgpack_load64(int64_t, o.
via.
ext.
data() + 4, &sec);
58 tp += std::chrono::seconds(sec);
59 tp += std::chrono::duration_cast<Duration>(
60 std::chrono::nanoseconds(nanosec));
64 tp += std::chrono::seconds(sec);
68 tp += std::chrono::seconds(sec);
69 int64_t ns = boost::numeric_cast<int64_t>(nanosec) - 1000000000L;
70 tp += std::chrono::duration_cast<Duration>(
71 std::chrono::nanoseconds(ns));
82 template <
typename Clock,
typename Duration>
83 struct convert<std::chrono::time_point<Clock, Duration>> {
87 std::chrono::time_point<Clock, Duration> tp;
91 _msgpack_load32(uint32_t, o.
via.
ext.
data(), &sec);
92 tp += std::chrono::seconds(sec);
97 _msgpack_load64(uint64_t, o.
via.
ext.
data(), &value);
98 uint32_t nanosec = boost::numeric_cast<uint32_t>(value >> 34);
99 uint64_t sec = value & 0x00000003ffffffffLL;
100 tp += std::chrono::duration_cast<Duration>(
101 std::chrono::nanoseconds(nanosec));
102 tp += std::chrono::seconds(sec);
107 _msgpack_load32(uint32_t, o.
via.
ext.
data(), &nanosec);
109 _msgpack_load64(int64_t, o.
via.
ext.
data() + 4, &sec);
112 tp += std::chrono::seconds(sec);
113 tp += std::chrono::duration_cast<Duration>(
114 std::chrono::nanoseconds(nanosec));
118 tp += std::chrono::seconds(sec);
122 tp += std::chrono::seconds(sec);
123 int64_t ns = boost::numeric_cast<int64_t>(nanosec) - 1000000000L;
124 tp += std::chrono::duration_cast<Duration>(
125 std::chrono::nanoseconds(ns));
138 template <
typename Clock,
typename Duration>
139 struct pack<std::chrono::time_point<Clock, Duration>> {
140 template <
typename Stream>
142 int64_t count = boost::numeric_cast<int64_t>(v.time_since_epoch().count());
144 Duration::period::ratio::num *
145 (1000000000L / Duration::period::ratio::den);
147 int64_t nanosec = count % (1000000000L / nano_num) * nano_num;
150 nanosec = 1000000000L + nanosec;
154 * Duration::period::ratio::num
155 / Duration::period::ratio::den;
157 if ((sec >> 34) == 0) {
158 uint64_t data64 = (boost::numeric_cast<uint64_t>(nanosec) << 34) | boost::numeric_cast<uint64_t>(sec);
159 if ((data64 & 0xffffffff00000000L) == 0) {
162 uint32_t data32 = boost::numeric_cast<uint32_t>(data64);
164 _msgpack_store32(buf, data32);
171 _msgpack_store64(buf, data64);
181 _msgpack_store32(&buf[0], boost::numeric_cast<uint32_t>(nanosec));
182 _msgpack_store64(&buf[4], sec);
189 template <
typename Clock,
typename Duration>
192 int64_t count = boost::numeric_cast<int64_t>(v.time_since_epoch().count());
195 Duration::period::ratio::num *
196 (1000000000L / Duration::period::ratio::den);
198 int64_t nanosec = count % (1000000000L / nano_num) * nano_num;
201 nanosec = 1000000000L + nanosec;
205 * Duration::period::ratio::num
206 / Duration::period::ratio::den;
207 if ((sec >> 34) == 0) {
208 uint64_t data64 = (boost::numeric_cast<uint64_t>(nanosec) << 34) | boost::numeric_cast<uint64_t>(sec);
209 if ((data64 & 0xffffffff00000000L) == 0) {
214 p[0] =
static_cast<char>(-1);
215 uint32_t data32 = boost::numeric_cast<uint32_t>(data64);
216 _msgpack_store32(&p[1], data32);
224 p[0] =
static_cast<char>(-1);
225 _msgpack_store64(&p[1], data64);
234 p[0] =
static_cast<char>(-1);
235 _msgpack_store32(&p[1], boost::numeric_cast<uint32_t>(nanosec));
236 _msgpack_store64(&p[1 + 4], sec);
250 #endif // MSGPACK_V1_TYPE_CPP11_CHRONO_HPP