318
+ − 1 // Copyright Toru Niina 2017.
+ − 2 // Distributed under the MIT License.
+ − 3 #ifndef TOML11_DATETIME_HPP
+ − 4 #define TOML11_DATETIME_HPP
+ − 5 #include <cstdint>
+ − 6 #include <cstdlib>
+ − 7 #include <ctime>
+ − 8
+ − 9 #include <array>
+ − 10 #include <chrono>
+ − 11 #include <iomanip>
+ − 12 #include <ostream>
+ − 13 #include <tuple>
+ − 14
+ − 15 namespace toml
+ − 16 {
+ − 17
+ − 18 // To avoid non-threadsafe std::localtime. In C11 (not C++11!), localtime_s is
+ − 19 // provided in the absolutely same purpose, but C++11 is actually not compatible
+ − 20 // with C11. We need to dispatch the function depending on the OS.
+ − 21 namespace detail
+ − 22 {
+ − 23 // TODO: find more sophisticated way to handle this
+ − 24 #if defined(_MSC_VER)
+ − 25 inline std::tm localtime_s(const std::time_t* src)
+ − 26 {
+ − 27 std::tm dst;
+ − 28 const auto result = ::localtime_s(&dst, src);
+ − 29 if (result) { throw std::runtime_error("localtime_s failed."); }
+ − 30 return dst;
+ − 31 }
+ − 32 inline std::tm gmtime_s(const std::time_t* src)
+ − 33 {
+ − 34 std::tm dst;
+ − 35 const auto result = ::gmtime_s(&dst, src);
+ − 36 if (result) { throw std::runtime_error("gmtime_s failed."); }
+ − 37 return dst;
+ − 38 }
+ − 39 #elif (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 1) || defined(_XOPEN_SOURCE) || defined(_BSD_SOURCE) || defined(_SVID_SOURCE) || defined(_POSIX_SOURCE)
+ − 40 inline std::tm localtime_s(const std::time_t* src)
+ − 41 {
+ − 42 std::tm dst;
+ − 43 const auto result = ::localtime_r(src, &dst);
+ − 44 if (!result) { throw std::runtime_error("localtime_r failed."); }
+ − 45 return dst;
+ − 46 }
+ − 47 inline std::tm gmtime_s(const std::time_t* src)
+ − 48 {
+ − 49 std::tm dst;
+ − 50 const auto result = ::gmtime_r(src, &dst);
+ − 51 if (!result) { throw std::runtime_error("gmtime_r failed."); }
+ − 52 return dst;
+ − 53 }
+ − 54 #else // fallback. not threadsafe
+ − 55 inline std::tm localtime_s(const std::time_t* src)
+ − 56 {
+ − 57 const auto result = std::localtime(src);
+ − 58 if (!result) { throw std::runtime_error("localtime failed."); }
+ − 59 return *result;
+ − 60 }
+ − 61 inline std::tm gmtime_s(const std::time_t* src)
+ − 62 {
+ − 63 const auto result = std::gmtime(src);
+ − 64 if (!result) { throw std::runtime_error("gmtime failed."); }
+ − 65 return *result;
+ − 66 }
+ − 67 #endif
+ − 68 } // detail
+ − 69
+ − 70 enum class month_t : std::uint8_t
+ − 71 {
+ − 72 Jan = 0,
+ − 73 Feb = 1,
+ − 74 Mar = 2,
+ − 75 Apr = 3,
+ − 76 May = 4,
+ − 77 Jun = 5,
+ − 78 Jul = 6,
+ − 79 Aug = 7,
+ − 80 Sep = 8,
+ − 81 Oct = 9,
+ − 82 Nov = 10,
+ − 83 Dec = 11
+ − 84 };
+ − 85
+ − 86 struct local_date
+ − 87 {
+ − 88 std::int16_t year{}; // A.D. (like, 2018)
+ − 89 std::uint8_t month{}; // [0, 11]
+ − 90 std::uint8_t day{}; // [1, 31]
+ − 91
+ − 92 local_date(int y, month_t m, int d)
+ − 93 : year (static_cast<std::int16_t>(y)),
+ − 94 month(static_cast<std::uint8_t>(m)),
+ − 95 day (static_cast<std::uint8_t>(d))
+ − 96 {}
+ − 97
+ − 98 explicit local_date(const std::tm& t)
+ − 99 : year (static_cast<std::int16_t>(t.tm_year + 1900)),
+ − 100 month(static_cast<std::uint8_t>(t.tm_mon)),
+ − 101 day (static_cast<std::uint8_t>(t.tm_mday))
+ − 102 {}
+ − 103
+ − 104 explicit local_date(const std::chrono::system_clock::time_point& tp)
+ − 105 {
+ − 106 const auto t = std::chrono::system_clock::to_time_t(tp);
+ − 107 const auto time = detail::localtime_s(&t);
+ − 108 *this = local_date(time);
+ − 109 }
+ − 110
+ − 111 explicit local_date(const std::time_t t)
+ − 112 : local_date(std::chrono::system_clock::from_time_t(t))
+ − 113 {}
+ − 114
+ − 115 operator std::chrono::system_clock::time_point() const
+ − 116 {
+ − 117 // std::mktime returns date as local time zone. no conversion needed
+ − 118 std::tm t;
+ − 119 t.tm_sec = 0;
+ − 120 t.tm_min = 0;
+ − 121 t.tm_hour = 0;
+ − 122 t.tm_mday = static_cast<int>(this->day);
+ − 123 t.tm_mon = static_cast<int>(this->month);
+ − 124 t.tm_year = static_cast<int>(this->year) - 1900;
+ − 125 t.tm_wday = 0; // the value will be ignored
+ − 126 t.tm_yday = 0; // the value will be ignored
+ − 127 t.tm_isdst = -1;
+ − 128 return std::chrono::system_clock::from_time_t(std::mktime(&t));
+ − 129 }
+ − 130
+ − 131 operator std::time_t() const
+ − 132 {
+ − 133 return std::chrono::system_clock::to_time_t(
+ − 134 std::chrono::system_clock::time_point(*this));
+ − 135 }
+ − 136
+ − 137 local_date() = default;
+ − 138 ~local_date() = default;
+ − 139 local_date(local_date const&) = default;
+ − 140 local_date(local_date&&) = default;
+ − 141 local_date& operator=(local_date const&) = default;
+ − 142 local_date& operator=(local_date&&) = default;
+ − 143 };
+ − 144
+ − 145 inline bool operator==(const local_date& lhs, const local_date& rhs)
+ − 146 {
+ − 147 return std::make_tuple(lhs.year, lhs.month, lhs.day) ==
+ − 148 std::make_tuple(rhs.year, rhs.month, rhs.day);
+ − 149 }
+ − 150 inline bool operator!=(const local_date& lhs, const local_date& rhs)
+ − 151 {
+ − 152 return !(lhs == rhs);
+ − 153 }
+ − 154 inline bool operator< (const local_date& lhs, const local_date& rhs)
+ − 155 {
+ − 156 return std::make_tuple(lhs.year, lhs.month, lhs.day) <
+ − 157 std::make_tuple(rhs.year, rhs.month, rhs.day);
+ − 158 }
+ − 159 inline bool operator<=(const local_date& lhs, const local_date& rhs)
+ − 160 {
+ − 161 return (lhs < rhs) || (lhs == rhs);
+ − 162 }
+ − 163 inline bool operator> (const local_date& lhs, const local_date& rhs)
+ − 164 {
+ − 165 return !(lhs <= rhs);
+ − 166 }
+ − 167 inline bool operator>=(const local_date& lhs, const local_date& rhs)
+ − 168 {
+ − 169 return !(lhs < rhs);
+ − 170 }
+ − 171
+ − 172 template<typename charT, typename traits>
+ − 173 std::basic_ostream<charT, traits>&
+ − 174 operator<<(std::basic_ostream<charT, traits>& os, const local_date& date)
+ − 175 {
+ − 176 os << std::setfill('0') << std::setw(4) << static_cast<int>(date.year ) << '-';
+ − 177 os << std::setfill('0') << std::setw(2) << static_cast<int>(date.month) + 1 << '-';
+ − 178 os << std::setfill('0') << std::setw(2) << static_cast<int>(date.day ) ;
+ − 179 return os;
+ − 180 }
+ − 181
+ − 182 struct local_time
+ − 183 {
+ − 184 std::uint8_t hour{}; // [0, 23]
+ − 185 std::uint8_t minute{}; // [0, 59]
+ − 186 std::uint8_t second{}; // [0, 60]
+ − 187 std::uint16_t millisecond{}; // [0, 999]
+ − 188 std::uint16_t microsecond{}; // [0, 999]
+ − 189 std::uint16_t nanosecond{}; // [0, 999]
+ − 190
+ − 191 local_time(int h, int m, int s,
+ − 192 int ms = 0, int us = 0, int ns = 0)
+ − 193 : hour (static_cast<std::uint8_t>(h)),
+ − 194 minute(static_cast<std::uint8_t>(m)),
+ − 195 second(static_cast<std::uint8_t>(s)),
+ − 196 millisecond(static_cast<std::uint16_t>(ms)),
+ − 197 microsecond(static_cast<std::uint16_t>(us)),
+ − 198 nanosecond (static_cast<std::uint16_t>(ns))
+ − 199 {}
+ − 200
+ − 201 explicit local_time(const std::tm& t)
+ − 202 : hour (static_cast<std::uint8_t>(t.tm_hour)),
+ − 203 minute(static_cast<std::uint8_t>(t.tm_min)),
+ − 204 second(static_cast<std::uint8_t>(t.tm_sec)),
+ − 205 millisecond(0), microsecond(0), nanosecond(0)
+ − 206 {}
+ − 207
+ − 208 template<typename Rep, typename Period>
+ − 209 explicit local_time(const std::chrono::duration<Rep, Period>& t)
+ − 210 {
+ − 211 const auto h = std::chrono::duration_cast<std::chrono::hours>(t);
+ − 212 this->hour = static_cast<std::uint8_t>(h.count());
+ − 213 const auto t2 = t - h;
+ − 214 const auto m = std::chrono::duration_cast<std::chrono::minutes>(t2);
+ − 215 this->minute = static_cast<std::uint8_t>(m.count());
+ − 216 const auto t3 = t2 - m;
+ − 217 const auto s = std::chrono::duration_cast<std::chrono::seconds>(t3);
+ − 218 this->second = static_cast<std::uint8_t>(s.count());
+ − 219 const auto t4 = t3 - s;
+ − 220 const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(t4);
+ − 221 this->millisecond = static_cast<std::uint16_t>(ms.count());
+ − 222 const auto t5 = t4 - ms;
+ − 223 const auto us = std::chrono::duration_cast<std::chrono::microseconds>(t5);
+ − 224 this->microsecond = static_cast<std::uint16_t>(us.count());
+ − 225 const auto t6 = t5 - us;
+ − 226 const auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(t6);
+ − 227 this->nanosecond = static_cast<std::uint16_t>(ns.count());
+ − 228 }
+ − 229
+ − 230 operator std::chrono::nanoseconds() const
+ − 231 {
+ − 232 return std::chrono::nanoseconds (this->nanosecond) +
+ − 233 std::chrono::microseconds(this->microsecond) +
+ − 234 std::chrono::milliseconds(this->millisecond) +
+ − 235 std::chrono::seconds(this->second) +
+ − 236 std::chrono::minutes(this->minute) +
+ − 237 std::chrono::hours(this->hour);
+ − 238 }
+ − 239
+ − 240 local_time() = default;
+ − 241 ~local_time() = default;
+ − 242 local_time(local_time const&) = default;
+ − 243 local_time(local_time&&) = default;
+ − 244 local_time& operator=(local_time const&) = default;
+ − 245 local_time& operator=(local_time&&) = default;
+ − 246 };
+ − 247
+ − 248 inline bool operator==(const local_time& lhs, const local_time& rhs)
+ − 249 {
+ − 250 return std::make_tuple(lhs.hour, lhs.minute, lhs.second, lhs.millisecond, lhs.microsecond, lhs.nanosecond) ==
+ − 251 std::make_tuple(rhs.hour, rhs.minute, rhs.second, rhs.millisecond, rhs.microsecond, rhs.nanosecond);
+ − 252 }
+ − 253 inline bool operator!=(const local_time& lhs, const local_time& rhs)
+ − 254 {
+ − 255 return !(lhs == rhs);
+ − 256 }
+ − 257 inline bool operator< (const local_time& lhs, const local_time& rhs)
+ − 258 {
+ − 259 return std::make_tuple(lhs.hour, lhs.minute, lhs.second, lhs.millisecond, lhs.microsecond, lhs.nanosecond) <
+ − 260 std::make_tuple(rhs.hour, rhs.minute, rhs.second, rhs.millisecond, rhs.microsecond, rhs.nanosecond);
+ − 261 }
+ − 262 inline bool operator<=(const local_time& lhs, const local_time& rhs)
+ − 263 {
+ − 264 return (lhs < rhs) || (lhs == rhs);
+ − 265 }
+ − 266 inline bool operator> (const local_time& lhs, const local_time& rhs)
+ − 267 {
+ − 268 return !(lhs <= rhs);
+ − 269 }
+ − 270 inline bool operator>=(const local_time& lhs, const local_time& rhs)
+ − 271 {
+ − 272 return !(lhs < rhs);
+ − 273 }
+ − 274
+ − 275 template<typename charT, typename traits>
+ − 276 std::basic_ostream<charT, traits>&
+ − 277 operator<<(std::basic_ostream<charT, traits>& os, const local_time& time)
+ − 278 {
+ − 279 os << std::setfill('0') << std::setw(2) << static_cast<int>(time.hour ) << ':';
+ − 280 os << std::setfill('0') << std::setw(2) << static_cast<int>(time.minute) << ':';
+ − 281 os << std::setfill('0') << std::setw(2) << static_cast<int>(time.second);
+ − 282 if(time.millisecond != 0 || time.microsecond != 0 || time.nanosecond != 0)
+ − 283 {
+ − 284 os << '.';
+ − 285 os << std::setfill('0') << std::setw(3) << static_cast<int>(time.millisecond);
+ − 286 if(time.microsecond != 0 || time.nanosecond != 0)
+ − 287 {
+ − 288 os << std::setfill('0') << std::setw(3) << static_cast<int>(time.microsecond);
+ − 289 if(time.nanosecond != 0)
+ − 290 {
+ − 291 os << std::setfill('0') << std::setw(3) << static_cast<int>(time.nanosecond);
+ − 292 }
+ − 293 }
+ − 294 }
+ − 295 return os;
+ − 296 }
+ − 297
+ − 298 struct time_offset
+ − 299 {
+ − 300 std::int8_t hour{}; // [-12, 12]
+ − 301 std::int8_t minute{}; // [-59, 59]
+ − 302
+ − 303 time_offset(int h, int m)
+ − 304 : hour (static_cast<std::int8_t>(h)),
+ − 305 minute(static_cast<std::int8_t>(m))
+ − 306 {}
+ − 307
+ − 308 operator std::chrono::minutes() const
+ − 309 {
+ − 310 return std::chrono::minutes(this->minute) +
+ − 311 std::chrono::hours(this->hour);
+ − 312 }
+ − 313
+ − 314 time_offset() = default;
+ − 315 ~time_offset() = default;
+ − 316 time_offset(time_offset const&) = default;
+ − 317 time_offset(time_offset&&) = default;
+ − 318 time_offset& operator=(time_offset const&) = default;
+ − 319 time_offset& operator=(time_offset&&) = default;
+ − 320 };
+ − 321
+ − 322 inline bool operator==(const time_offset& lhs, const time_offset& rhs)
+ − 323 {
+ − 324 return std::make_tuple(lhs.hour, lhs.minute) ==
+ − 325 std::make_tuple(rhs.hour, rhs.minute);
+ − 326 }
+ − 327 inline bool operator!=(const time_offset& lhs, const time_offset& rhs)
+ − 328 {
+ − 329 return !(lhs == rhs);
+ − 330 }
+ − 331 inline bool operator< (const time_offset& lhs, const time_offset& rhs)
+ − 332 {
+ − 333 return std::make_tuple(lhs.hour, lhs.minute) <
+ − 334 std::make_tuple(rhs.hour, rhs.minute);
+ − 335 }
+ − 336 inline bool operator<=(const time_offset& lhs, const time_offset& rhs)
+ − 337 {
+ − 338 return (lhs < rhs) || (lhs == rhs);
+ − 339 }
+ − 340 inline bool operator> (const time_offset& lhs, const time_offset& rhs)
+ − 341 {
+ − 342 return !(lhs <= rhs);
+ − 343 }
+ − 344 inline bool operator>=(const time_offset& lhs, const time_offset& rhs)
+ − 345 {
+ − 346 return !(lhs < rhs);
+ − 347 }
+ − 348
+ − 349 template<typename charT, typename traits>
+ − 350 std::basic_ostream<charT, traits>&
+ − 351 operator<<(std::basic_ostream<charT, traits>& os, const time_offset& offset)
+ − 352 {
+ − 353 if(offset.hour == 0 && offset.minute == 0)
+ − 354 {
+ − 355 os << 'Z';
+ − 356 return os;
+ − 357 }
+ − 358 int minute = static_cast<int>(offset.hour) * 60 + offset.minute;
+ − 359 if(minute < 0){os << '-'; minute = std::abs(minute);} else {os << '+';}
+ − 360 os << std::setfill('0') << std::setw(2) << minute / 60 << ':';
+ − 361 os << std::setfill('0') << std::setw(2) << minute % 60;
+ − 362 return os;
+ − 363 }
+ − 364
+ − 365 struct local_datetime
+ − 366 {
+ − 367 local_date date{};
+ − 368 local_time time{};
+ − 369
+ − 370 local_datetime(local_date d, local_time t): date(d), time(t) {}
+ − 371
+ − 372 explicit local_datetime(const std::tm& t): date(t), time(t){}
+ − 373
+ − 374 explicit local_datetime(const std::chrono::system_clock::time_point& tp)
+ − 375 {
+ − 376 const auto t = std::chrono::system_clock::to_time_t(tp);
+ − 377 std::tm ltime = detail::localtime_s(&t);
+ − 378
+ − 379 this->date = local_date(ltime);
+ − 380 this->time = local_time(ltime);
+ − 381
+ − 382 // std::tm lacks subsecond information, so diff between tp and tm
+ − 383 // can be used to get millisecond & microsecond information.
+ − 384 const auto t_diff = tp -
+ − 385 std::chrono::system_clock::from_time_t(std::mktime(<ime));
+ − 386 this->time.millisecond = static_cast<std::uint16_t>(
+ − 387 std::chrono::duration_cast<std::chrono::milliseconds>(t_diff).count());
+ − 388 this->time.microsecond = static_cast<std::uint16_t>(
+ − 389 std::chrono::duration_cast<std::chrono::microseconds>(t_diff).count());
+ − 390 this->time.nanosecond = static_cast<std::uint16_t>(
+ − 391 std::chrono::duration_cast<std::chrono::nanoseconds >(t_diff).count());
+ − 392 }
+ − 393
+ − 394 explicit local_datetime(const std::time_t t)
+ − 395 : local_datetime(std::chrono::system_clock::from_time_t(t))
+ − 396 {}
+ − 397
+ − 398 operator std::chrono::system_clock::time_point() const
+ − 399 {
+ − 400 using internal_duration =
+ − 401 typename std::chrono::system_clock::time_point::duration;
+ − 402
+ − 403 // Normally DST begins at A.M. 3 or 4. If we re-use conversion operator
+ − 404 // of local_date and local_time independently, the conversion fails if
+ − 405 // it is the day when DST begins or ends. Since local_date considers the
+ − 406 // time is 00:00 A.M. and local_time does not consider DST because it
+ − 407 // does not have any date information. We need to consider both date and
+ − 408 // time information at the same time to convert it correctly.
+ − 409
+ − 410 std::tm t;
+ − 411 t.tm_sec = static_cast<int>(this->time.second);
+ − 412 t.tm_min = static_cast<int>(this->time.minute);
+ − 413 t.tm_hour = static_cast<int>(this->time.hour);
+ − 414 t.tm_mday = static_cast<int>(this->date.day);
+ − 415 t.tm_mon = static_cast<int>(this->date.month);
+ − 416 t.tm_year = static_cast<int>(this->date.year) - 1900;
+ − 417 t.tm_wday = 0; // the value will be ignored
+ − 418 t.tm_yday = 0; // the value will be ignored
+ − 419 t.tm_isdst = -1;
+ − 420
+ − 421 // std::mktime returns date as local time zone. no conversion needed
+ − 422 auto dt = std::chrono::system_clock::from_time_t(std::mktime(&t));
+ − 423 dt += std::chrono::duration_cast<internal_duration>(
+ − 424 std::chrono::milliseconds(this->time.millisecond) +
+ − 425 std::chrono::microseconds(this->time.microsecond) +
+ − 426 std::chrono::nanoseconds (this->time.nanosecond));
+ − 427 return dt;
+ − 428 }
+ − 429
+ − 430 operator std::time_t() const
+ − 431 {
+ − 432 return std::chrono::system_clock::to_time_t(
+ − 433 std::chrono::system_clock::time_point(*this));
+ − 434 }
+ − 435
+ − 436 local_datetime() = default;
+ − 437 ~local_datetime() = default;
+ − 438 local_datetime(local_datetime const&) = default;
+ − 439 local_datetime(local_datetime&&) = default;
+ − 440 local_datetime& operator=(local_datetime const&) = default;
+ − 441 local_datetime& operator=(local_datetime&&) = default;
+ − 442 };
+ − 443
+ − 444 inline bool operator==(const local_datetime& lhs, const local_datetime& rhs)
+ − 445 {
+ − 446 return std::make_tuple(lhs.date, lhs.time) ==
+ − 447 std::make_tuple(rhs.date, rhs.time);
+ − 448 }
+ − 449 inline bool operator!=(const local_datetime& lhs, const local_datetime& rhs)
+ − 450 {
+ − 451 return !(lhs == rhs);
+ − 452 }
+ − 453 inline bool operator< (const local_datetime& lhs, const local_datetime& rhs)
+ − 454 {
+ − 455 return std::make_tuple(lhs.date, lhs.time) <
+ − 456 std::make_tuple(rhs.date, rhs.time);
+ − 457 }
+ − 458 inline bool operator<=(const local_datetime& lhs, const local_datetime& rhs)
+ − 459 {
+ − 460 return (lhs < rhs) || (lhs == rhs);
+ − 461 }
+ − 462 inline bool operator> (const local_datetime& lhs, const local_datetime& rhs)
+ − 463 {
+ − 464 return !(lhs <= rhs);
+ − 465 }
+ − 466 inline bool operator>=(const local_datetime& lhs, const local_datetime& rhs)
+ − 467 {
+ − 468 return !(lhs < rhs);
+ − 469 }
+ − 470
+ − 471 template<typename charT, typename traits>
+ − 472 std::basic_ostream<charT, traits>&
+ − 473 operator<<(std::basic_ostream<charT, traits>& os, const local_datetime& dt)
+ − 474 {
+ − 475 os << dt.date << 'T' << dt.time;
+ − 476 return os;
+ − 477 }
+ − 478
+ − 479 struct offset_datetime
+ − 480 {
+ − 481 local_date date{};
+ − 482 local_time time{};
+ − 483 time_offset offset{};
+ − 484
+ − 485 offset_datetime(local_date d, local_time t, time_offset o)
+ − 486 : date(d), time(t), offset(o)
+ − 487 {}
+ − 488 offset_datetime(const local_datetime& dt, time_offset o)
+ − 489 : date(dt.date), time(dt.time), offset(o)
+ − 490 {}
+ − 491 explicit offset_datetime(const local_datetime& ld)
+ − 492 : date(ld.date), time(ld.time), offset(get_local_offset(nullptr))
+ − 493 // use the current local timezone offset
+ − 494 {}
+ − 495 explicit offset_datetime(const std::chrono::system_clock::time_point& tp)
+ − 496 : offset(0, 0) // use gmtime
+ − 497 {
+ − 498 const auto timet = std::chrono::system_clock::to_time_t(tp);
+ − 499 const auto tm = detail::gmtime_s(&timet);
+ − 500 this->date = local_date(tm);
+ − 501 this->time = local_time(tm);
+ − 502 }
+ − 503 explicit offset_datetime(const std::time_t& t)
+ − 504 : offset(0, 0) // use gmtime
+ − 505 {
+ − 506 const auto tm = detail::gmtime_s(&t);
+ − 507 this->date = local_date(tm);
+ − 508 this->time = local_time(tm);
+ − 509 }
+ − 510 explicit offset_datetime(const std::tm& t)
+ − 511 : offset(0, 0) // assume gmtime
+ − 512 {
+ − 513 this->date = local_date(t);
+ − 514 this->time = local_time(t);
+ − 515 }
+ − 516
+ − 517 operator std::chrono::system_clock::time_point() const
+ − 518 {
+ − 519 // get date-time
+ − 520 using internal_duration =
+ − 521 typename std::chrono::system_clock::time_point::duration;
+ − 522
+ − 523 // first, convert it to local date-time information in the same way as
+ − 524 // local_datetime does. later we will use time_t to adjust time offset.
+ − 525 std::tm t;
+ − 526 t.tm_sec = static_cast<int>(this->time.second);
+ − 527 t.tm_min = static_cast<int>(this->time.minute);
+ − 528 t.tm_hour = static_cast<int>(this->time.hour);
+ − 529 t.tm_mday = static_cast<int>(this->date.day);
+ − 530 t.tm_mon = static_cast<int>(this->date.month);
+ − 531 t.tm_year = static_cast<int>(this->date.year) - 1900;
+ − 532 t.tm_wday = 0; // the value will be ignored
+ − 533 t.tm_yday = 0; // the value will be ignored
+ − 534 t.tm_isdst = -1;
+ − 535 const std::time_t tp_loc = std::mktime(std::addressof(t));
+ − 536
+ − 537 auto tp = std::chrono::system_clock::from_time_t(tp_loc);
+ − 538 tp += std::chrono::duration_cast<internal_duration>(
+ − 539 std::chrono::milliseconds(this->time.millisecond) +
+ − 540 std::chrono::microseconds(this->time.microsecond) +
+ − 541 std::chrono::nanoseconds (this->time.nanosecond));
+ − 542
+ − 543 // Since mktime uses local time zone, it should be corrected.
+ − 544 // `12:00:00+09:00` means `03:00:00Z`. So mktime returns `03:00:00Z` if
+ − 545 // we are in `+09:00` timezone. To represent `12:00:00Z` there, we need
+ − 546 // to add `+09:00` to `03:00:00Z`.
+ − 547 // Here, it uses the time_t converted from date-time info to handle
+ − 548 // daylight saving time.
+ − 549 const auto ofs = get_local_offset(std::addressof(tp_loc));
+ − 550 tp += std::chrono::hours (ofs.hour);
+ − 551 tp += std::chrono::minutes(ofs.minute);
+ − 552
+ − 553 // We got `12:00:00Z` by correcting local timezone applied by mktime.
+ − 554 // Then we will apply the offset. Let's say `12:00:00-08:00` is given.
+ − 555 // And now, we have `12:00:00Z`. `12:00:00-08:00` means `20:00:00Z`.
+ − 556 // So we need to subtract the offset.
+ − 557 tp -= std::chrono::minutes(this->offset);
+ − 558 return tp;
+ − 559 }
+ − 560
+ − 561 operator std::time_t() const
+ − 562 {
+ − 563 return std::chrono::system_clock::to_time_t(
+ − 564 std::chrono::system_clock::time_point(*this));
+ − 565 }
+ − 566
+ − 567 offset_datetime() = default;
+ − 568 ~offset_datetime() = default;
+ − 569 offset_datetime(offset_datetime const&) = default;
+ − 570 offset_datetime(offset_datetime&&) = default;
+ − 571 offset_datetime& operator=(offset_datetime const&) = default;
+ − 572 offset_datetime& operator=(offset_datetime&&) = default;
+ − 573
+ − 574 private:
+ − 575
+ − 576 static time_offset get_local_offset(const std::time_t* tp)
+ − 577 {
+ − 578 // get local timezone with the same date-time information as mktime
+ − 579 const auto t = detail::localtime_s(tp);
+ − 580
+ − 581 std::array<char, 6> buf;
+ − 582 const auto result = std::strftime(buf.data(), 6, "%z", &t); // +hhmm\0
+ − 583 if(result != 5)
+ − 584 {
+ − 585 throw std::runtime_error("toml::offset_datetime: cannot obtain "
+ − 586 "timezone information of current env");
+ − 587 }
+ − 588 const int ofs = std::atoi(buf.data());
+ − 589 const int ofs_h = ofs / 100;
+ − 590 const int ofs_m = ofs - (ofs_h * 100);
+ − 591 return time_offset(ofs_h, ofs_m);
+ − 592 }
+ − 593 };
+ − 594
+ − 595 inline bool operator==(const offset_datetime& lhs, const offset_datetime& rhs)
+ − 596 {
+ − 597 return std::make_tuple(lhs.date, lhs.time, lhs.offset) ==
+ − 598 std::make_tuple(rhs.date, rhs.time, rhs.offset);
+ − 599 }
+ − 600 inline bool operator!=(const offset_datetime& lhs, const offset_datetime& rhs)
+ − 601 {
+ − 602 return !(lhs == rhs);
+ − 603 }
+ − 604 inline bool operator< (const offset_datetime& lhs, const offset_datetime& rhs)
+ − 605 {
+ − 606 return std::make_tuple(lhs.date, lhs.time, lhs.offset) <
+ − 607 std::make_tuple(rhs.date, rhs.time, rhs.offset);
+ − 608 }
+ − 609 inline bool operator<=(const offset_datetime& lhs, const offset_datetime& rhs)
+ − 610 {
+ − 611 return (lhs < rhs) || (lhs == rhs);
+ − 612 }
+ − 613 inline bool operator> (const offset_datetime& lhs, const offset_datetime& rhs)
+ − 614 {
+ − 615 return !(lhs <= rhs);
+ − 616 }
+ − 617 inline bool operator>=(const offset_datetime& lhs, const offset_datetime& rhs)
+ − 618 {
+ − 619 return !(lhs < rhs);
+ − 620 }
+ − 621
+ − 622 template<typename charT, typename traits>
+ − 623 std::basic_ostream<charT, traits>&
+ − 624 operator<<(std::basic_ostream<charT, traits>& os, const offset_datetime& dt)
+ − 625 {
+ − 626 os << dt.date << 'T' << dt.time << dt.offset;
+ − 627 return os;
+ − 628 }
+ − 629
+ − 630 }//toml
+ − 631 #endif// TOML11_DATETIME