change almost everything about datetime

This commit is contained in:
ToruNiina 2018-12-08 19:04:41 +09:00
parent f326334147
commit 3ef33c1637
2 changed files with 530 additions and 192 deletions

View File

@ -5,26 +5,117 @@
#define BOOST_TEST_NO_LIB
#include <boost/test/included/unit_test.hpp>
#endif
#include <toml.hpp>
#include <thread>
#include <toml/datetime.hpp>
BOOST_AUTO_TEST_CASE(test_datetime_convertible)
BOOST_AUTO_TEST_CASE(test_local_date)
{
const auto now = std::chrono::system_clock::now();
toml::Datetime d1(now);
const std::chrono::system_clock::time_point cvt(d1);
toml::Datetime d2(cvt);
const toml::local_date date(2018, toml::month_t::Jan, 1);
const toml::local_date date1(date);
BOOST_CHECK_EQUAL(date, date1);
BOOST_CHECK_EQUAL(d1, d2);
const std::chrono::system_clock::time_point tp(date);
const toml::local_date date2(tp);
BOOST_CHECK_EQUAL(date, date2);
const auto time = std::chrono::system_clock::to_time_t(now);
toml::Datetime d3(time);
toml::Datetime d4(std::chrono::system_clock::from_time_t(time));
const toml::local_date date3(2017, toml::month_t::Dec, 31);
BOOST_CHECK(date > date3);
BOOST_CHECK_EQUAL(d3, d4);
std::this_thread::sleep_for(std::chrono::seconds(1));
const auto later = std::chrono::system_clock::now();
toml::Datetime d5(later);
BOOST_CHECK(d1 < d5);
std::ostringstream oss;
oss << date;
BOOST_CHECK_EQUAL(oss.str(), std::string("2018-01-01"));
}
BOOST_AUTO_TEST_CASE(test_local_time)
{
const toml::local_time time(12, 30, 45);
const toml::local_time time1(time);
BOOST_CHECK_EQUAL(time, time1);
const std::chrono::microseconds dur(time);
std::chrono::microseconds us(0);
us += std::chrono::hours (12);
us += std::chrono::minutes(30);
us += std::chrono::seconds(45);
BOOST_CHECK_EQUAL(dur.count(), us.count());
const toml::local_time time3(12, 15, 45);
BOOST_CHECK(time > time3);
{
std::ostringstream oss;
oss << time;
BOOST_CHECK_EQUAL(oss.str(), std::string("12:30:45"));
}
{
const toml::local_time time4(12, 30, 45, 123, 456);
std::ostringstream oss;
oss << time4;
BOOST_CHECK_EQUAL(oss.str(), std::string("12:30:45.123456"));
}
}
BOOST_AUTO_TEST_CASE(test_time_offset)
{
const toml::time_offset time(9, 30);
const toml::time_offset time1(time);
BOOST_CHECK_EQUAL(time, time1);
const std::chrono::minutes dur(time);
std::chrono::minutes m(0);
m += std::chrono::hours (9);
m += std::chrono::minutes(30);
BOOST_CHECK_EQUAL(dur.count(), m.count());
const toml::time_offset time2(9, 0);
BOOST_CHECK(time2 < time);
std::ostringstream oss;
oss << time;
BOOST_CHECK_EQUAL(oss.str(), std::string("+09:30"));
}
BOOST_AUTO_TEST_CASE(test_local_datetime)
{
const toml::local_datetime dt(toml::local_date(2018, toml::month_t::Jan, 1),
toml::local_time(12, 30, 45));
const toml::local_datetime dt1(dt);
BOOST_CHECK_EQUAL(dt, dt1);
const std::chrono::system_clock::time_point tp(dt);
const toml::local_datetime dt2(tp);
BOOST_CHECK_EQUAL(dt, dt2);
std::ostringstream oss;
oss << dt;
BOOST_CHECK_EQUAL(oss.str(), std::string("2018-01-01T12:30:45"));
}
BOOST_AUTO_TEST_CASE(test_offset_datetime)
{
const toml::offset_datetime dt(toml::local_date(2018, toml::month_t::Jan, 1),
toml::local_time(12, 30, 45),
toml::time_offset(9, 30));
const toml::offset_datetime dt1(dt);
BOOST_CHECK_EQUAL(dt, dt1);
const std::chrono::system_clock::time_point tp1(dt);
const toml::offset_datetime dt2(tp1);
const std::chrono::system_clock::time_point tp2(dt2);
BOOST_CHECK(tp1 == tp2);
{
std::ostringstream oss;
oss << dt;
BOOST_CHECK_EQUAL(oss.str(), std::string("2018-01-01T12:30:45+09:30"));
}
{
const toml::offset_datetime dt3(
toml::local_date(2018, toml::month_t::Jan, 1),
toml::local_time(12, 30, 45),
toml::time_offset(0, 0));
std::ostringstream oss;
oss << dt3;
BOOST_CHECK_EQUAL(oss.str(), std::string("2018-01-01T12:30:45Z"));
}
}

View File

@ -7,225 +7,472 @@
namespace toml
{
template<typename unsignedT, typename intT>
struct basic_datetime
enum class month_t : std::int8_t
{
typedef unsignedT number_type;
typedef intT offset_type;
constexpr static unsignedT undef = std::numeric_limits<unsignedT>::max();
constexpr static intT nooffset = std::numeric_limits<intT>::max();
Jan = 0,
Feb = 1,
Mar = 2,
Apr = 3,
May = 4,
Jun = 5,
Jul = 6,
Aug = 7,
Sep = 8,
Oct = 9,
Nov = 10,
Dec = 11
};
unsignedT year;
unsignedT month;
unsignedT day;
unsignedT hour;
unsignedT minute;
unsignedT second;
unsignedT millisecond;
unsignedT microsecond;
intT offset_hour;
intT offset_minute;
struct local_date
{
std::int16_t year; // A.D. (like, 2018)
std::int8_t month; // [0, 11]
std::int8_t day; // [1, 31]
basic_datetime() = default;
~basic_datetime() = default;
basic_datetime(const basic_datetime&) = default;
basic_datetime(basic_datetime&&) = default;
basic_datetime& operator=(const basic_datetime&) = default;
basic_datetime& operator=(basic_datetime&&) = default;
basic_datetime(unsignedT y, unsignedT m, unsignedT d)
: year(y), month(m), day(d), hour(undef), minute(undef), second(undef),
millisecond(undef), microsecond(undef),
offset_hour(nooffset), offset_minute(nooffset)
{}
basic_datetime(unsignedT h, unsignedT m, unsignedT s,
unsignedT ms, unsignedT us)
: year(undef), month(undef), day(undef), hour(h), minute(m), second(s),
millisecond(ms), microsecond(us),
offset_hour(nooffset), offset_minute(nooffset)
{}
basic_datetime(unsignedT y, unsignedT mth, unsignedT d,
unsignedT h, unsignedT min, unsignedT s,
unsignedT ms, unsignedT us)
: year(y), month(mth), day(d), hour(h), minute(min), second(s),
millisecond(ms), microsecond(us),
offset_hour(nooffset), offset_minute(nooffset)
{}
basic_datetime(unsignedT y, unsignedT mth, unsignedT d,
unsignedT h, unsignedT min, unsignedT s,
unsignedT ss, unsignedT us, intT oh, intT om)
: year(y), month(mth), day(d), hour(h), minute(min), second(s),
millisecond(ss), microsecond(us), offset_hour(oh), offset_minute(om)
local_date(std::int16_t y, month_t m, std::int8_t d)
: year(y), month(static_cast<std::int8_t>(m)), day(d)
{}
basic_datetime(std::chrono::system_clock::time_point tp);
basic_datetime(std::time_t t);
local_date(const std::tm& t)
: year(t.tm_year + 1900), month(t.tm_mon), day(t.tm_mday)
{}
local_date(const std::chrono::system_clock::time_point& tp)
{
const auto t = std::chrono::system_clock::to_time_t(tp);
const auto tmp = std::localtime(&t); //XXX: not threadsafe!
assert(tmp); // if std::localtime fails, tmp is nullptr
const std::tm time = *tmp;
*this = local_date(time);
}
local_date(const std::time_t t)
: local_date(std::chrono::system_clock::from_time_t(t))
{}
operator std::chrono::system_clock::time_point() const
{
std::tm time;
if(this->year == undef || this->month == undef || this->day == undef)
{
const auto now = std::chrono::system_clock::now();
const auto t = std::chrono::system_clock::to_time_t(now);
std::tm* t_ = std::localtime(&t);
time.tm_year = t_->tm_year;
time.tm_mon = t_->tm_mon;
time.tm_mday = t_->tm_mday;
}
else
{
time.tm_year = this->year - 1900;
time.tm_mon = this->month - 1;
time.tm_mday = this->day;
}
time.tm_hour = (this->hour == undef) ? 0 : this->hour;
time.tm_min = (this->minute == undef) ? 0 : this->minute;
time.tm_sec = (this->second == undef) ? 0 : this->second;
auto tp = std::chrono::system_clock::from_time_t(std::mktime(&time));
tp += std::chrono::milliseconds(this->millisecond);
tp += std::chrono::microseconds(this->microsecond);
// mktime regards the tm struct as localtime. so adding offset is not needed.
return tp;
// std::mktime returns date as local time zone. no conversion needed
std::tm t;
t.tm_sec = 0;
t.tm_min = 0;
t.tm_hour = 0;
t.tm_mday = this->day;
t.tm_mon = this->month;
t.tm_year = this->year - 1900;
t.tm_wday = 0; // the value will be ignored
t.tm_yday = 0; // the value will be ignored
t.tm_isdst = -1;
return std::chrono::system_clock::from_time_t(std::mktime(&t));
}
operator std::time_t() const
{
return std::chrono::system_clock::to_time_t(
std::chrono::system_clock::time_point(*this));
std::chrono::system_clock::time_point(*this));
}
local_date() = default;
~local_date() = default;
local_date(local_date const&) = default;
local_date(local_date&&) = default;
local_date& operator=(local_date const&) = default;
local_date& operator=(local_date&&) = default;
};
template<typename uT, typename iT>
basic_datetime<uT, iT>::basic_datetime(std::chrono::system_clock::time_point tp)
inline bool operator==(const local_date& lhs, const local_date& rhs)
{
const auto t = std::chrono::system_clock::to_time_t(tp);
std::tm *time = std::localtime(&t);
this->year = time->tm_year + 1900;
this->month = time->tm_mon + 1;
this->day = time->tm_mday;
this->hour = time->tm_hour;
this->minute = time->tm_min;
this->second = time->tm_sec;
auto t_ = std::chrono::system_clock::from_time_t(std::mktime(time));
auto diff = tp - t_;
this->millisecond = std::chrono::duration_cast<std::chrono::milliseconds
>(diff).count() % 1000;
this->microsecond = std::chrono::duration_cast<std::chrono::microseconds
>(diff).count() % 1000;
std::tm *utc = std::gmtime(&t);
int total_offset = (this->hour - utc->tm_hour) * 60 +
(this->minute - utc->tm_min);
if(total_offset > 720) total_offset -= 1440;
else if(total_offset < -720) total_offset += 1440;
offset_hour = total_offset / 60;
offset_minute = total_offset - (offset_hour * 60);
return std::make_tuple(lhs.year, lhs.month, lhs.day) ==
std::make_tuple(rhs.year, rhs.month, rhs.day);
}
inline bool operator!=(const local_date& lhs, const local_date& rhs)
{
return !(lhs == rhs);
}
inline bool operator< (const local_date& lhs, const local_date& rhs)
{
return std::make_tuple(lhs.year, lhs.month, lhs.day) <
std::make_tuple(rhs.year, rhs.month, rhs.day);
}
inline bool operator<=(const local_date& lhs, const local_date& rhs)
{
return (lhs < rhs) || (lhs == rhs);
}
inline bool operator> (const local_date& lhs, const local_date& rhs)
{
return !(lhs <= rhs);
}
inline bool operator>=(const local_date& lhs, const local_date& rhs)
{
return !(lhs < rhs);
}
template<typename uT, typename iT>
basic_datetime<uT, iT>::basic_datetime(std::time_t t)
{
*this = basic_datetime(std::chrono::system_clock::from_time_t(t));
}
template<typename charT, typename traits, typename uT, typename iT>
template<typename charT, typename traits>
std::basic_ostream<charT, traits>&
operator<<(std::basic_ostream<charT, traits>& os, basic_datetime<uT, iT> const& dt)
operator<<(std::basic_ostream<charT, traits>& os, const local_date& date)
{
bool date = false;
if(dt.year != basic_datetime<uT, iT>::undef &&
dt.month != basic_datetime<uT, iT>::undef &&
dt.day != basic_datetime<uT, iT>::undef)
os << std::setfill('0') << std::setw(4) << static_cast<int>(date.year ) << '-';
os << std::setfill('0') << std::setw(2) << static_cast<int>(date.month + 1) << '-';
os << std::setfill('0') << std::setw(2) << static_cast<int>(date.day );
return os;
}
struct local_time
{
std::int8_t hour; // [0, 23]
std::int8_t minute; // [0, 59]
std::int8_t second; // [0, 60]
std::int16_t millisecond; // [0, 999]
std::int16_t microsecond; // [0, 999]
local_time(std::int8_t h, std::int8_t m, std::int8_t s)
: hour(h), minute(m), second(s), millisecond(0), microsecond(0)
{}
local_time(std::int8_t h, std::int8_t m, std::int8_t s,
std::int16_t ms, std::int16_t us)
: hour(h), minute(m), second(s), millisecond(ms), microsecond(us)
{}
local_time(const std::tm& t)
: hour(t.tm_hour), minute(t.tm_min), second(t.tm_sec),
millisecond(0), microsecond(0)
{}
operator std::chrono::microseconds() const
{
os << std::setfill('0') << std::setw(4) << dt.year << '-'
<< std::setfill('0') << std::setw(2) << dt.month << '-'
<< std::setfill('0') << std::setw(2) << dt.day;
date = true;
return std::chrono::microseconds(this->microsecond) +
std::chrono::milliseconds(this->millisecond) +
std::chrono::seconds(this->second) +
std::chrono::minutes(this->minute) +
std::chrono::hours(this->hour);
}
if(dt.hour != basic_datetime<uT, iT>::undef &&
dt.minute != basic_datetime<uT, iT>::undef &&
dt.second != basic_datetime<uT, iT>::undef)
local_time() = default;
~local_time() = default;
local_time(local_time const&) = default;
local_time(local_time&&) = default;
local_time& operator=(local_time const&) = default;
local_time& operator=(local_time&&) = default;
};
inline bool operator==(const local_time& lhs, const local_time& rhs)
{
return std::make_tuple(lhs.hour, lhs.minute, lhs.second, lhs.millisecond, lhs.microsecond) ==
std::make_tuple(rhs.hour, rhs.minute, rhs.second, rhs.millisecond, rhs.microsecond);
}
inline bool operator!=(const local_time& lhs, const local_time& rhs)
{
return !(lhs == rhs);
}
inline bool operator< (const local_time& lhs, const local_time& rhs)
{
return std::make_tuple(lhs.hour, lhs.minute, lhs.second, lhs.millisecond, lhs.microsecond) <
std::make_tuple(rhs.hour, rhs.minute, rhs.second, rhs.millisecond, rhs.microsecond);
}
inline bool operator<=(const local_time& lhs, const local_time& rhs)
{
return (lhs < rhs) || (lhs == rhs);
}
inline bool operator> (const local_time& lhs, const local_time& rhs)
{
return !(lhs <= rhs);
}
inline bool operator>=(const local_time& lhs, const local_time& rhs)
{
return !(lhs < rhs);
}
template<typename charT, typename traits>
std::basic_ostream<charT, traits>&
operator<<(std::basic_ostream<charT, traits>& os, const local_time& time)
{
os << std::setfill('0') << std::setw(2) << static_cast<int>(time.hour ) << ':';
os << std::setfill('0') << std::setw(2) << static_cast<int>(time.minute ) << ':';
os << std::setfill('0') << std::setw(2) << static_cast<int>(time.second );
if(time.millisecond != 0 || time.microsecond != 0)
{
if(date) os << 'T';
os << std::setfill('0') << std::setw(2) << dt.hour << ':'
<< std::setfill('0') << std::setw(2) << dt.minute << ':'
<< std::setfill('0') << std::setw(2) << dt.second << '.'
<< std::setfill('0') << std::setw(3) << dt.millisecond
<< std::setfill('0') << std::setw(3) << dt.microsecond;
}
if(dt.offset_hour != basic_datetime<uT, iT>::nooffset &&
dt.offset_minute != basic_datetime<uT, iT>::nooffset)
{
if(dt.offset_hour == 0 && dt.offset_minute == 0)
{
os << 'Z';
}
else
{
char sign = ' ';
iT oh = dt.offset_hour;
iT om = dt.offset_minute;
om += oh * 60;
if(om > 0) sign = '+'; else sign='-';
oh = om / 60;
om -= oh * 60;
os << sign << std::setfill('0') << std::setw(2) << std::abs(oh) << ':'
<< std::setfill('0') << std::setw(2) << std::abs(om);
}
os << '.';
os << std::setfill('0') << std::setw(3) << static_cast<int>(time.millisecond);
os << std::setfill('0') << std::setw(3) << static_cast<int>(time.microsecond);
}
return os;
}
template<typename uT, typename iT>
inline bool
operator==(basic_datetime<uT, iT> const& lhs, basic_datetime<uT, iT> const& rhs)
struct time_offset
{
return lhs.year == rhs.year && lhs.month == rhs.month &&
lhs.day == rhs.day && lhs.minute == rhs.minute &&
lhs.second == rhs.second && lhs.millisecond == rhs.millisecond &&
lhs.microsecond == rhs.microsecond &&
lhs.offset_hour == rhs.offset_hour &&
lhs.offset_minute == rhs.offset_minute;
}
std::int8_t hour; // [-12, 12]
std::int8_t minute; // [-59, 59]
template<typename uT, typename iT>
inline bool
operator!=(basic_datetime<uT, iT> const& lhs, basic_datetime<uT, iT> const& rhs)
time_offset(std::int8_t h, std::int8_t m): hour(h), minute(m) {}
operator std::chrono::minutes() const
{
return std::chrono::minutes(this->minute) +
std::chrono::hours(this->hour);
}
time_offset() = default;
~time_offset() = default;
time_offset(time_offset const&) = default;
time_offset(time_offset&&) = default;
time_offset& operator=(time_offset const&) = default;
time_offset& operator=(time_offset&&) = default;
};
inline bool operator==(const time_offset& lhs, const time_offset& rhs)
{
return std::make_tuple(lhs.hour, lhs.minute) ==
std::make_tuple(rhs.hour, rhs.minute);
}
inline bool operator!=(const time_offset& lhs, const time_offset& rhs)
{
return !(lhs == rhs);
}
template<typename uT, typename iT>
inline bool
operator<(basic_datetime<uT, iT> const& lhs, basic_datetime<uT, iT> const& rhs)
inline bool operator< (const time_offset& lhs, const time_offset& rhs)
{
return std::time_t(lhs) < std::time_t(rhs);
return std::make_tuple(lhs.hour, lhs.minute) <
std::make_tuple(rhs.hour, rhs.minute);
}
inline bool operator<=(const time_offset& lhs, const time_offset& rhs)
{
return (lhs < rhs) || (lhs == rhs);
}
inline bool operator> (const time_offset& lhs, const time_offset& rhs)
{
return !(lhs <= rhs);
}
inline bool operator>=(const time_offset& lhs, const time_offset& rhs)
{
return !(lhs < rhs);
}
template<typename uT, typename iT>
inline bool
operator<=(basic_datetime<uT, iT> const& lhs, basic_datetime<uT, iT> const& rhs)
template<typename charT, typename traits>
std::basic_ostream<charT, traits>&
operator<<(std::basic_ostream<charT, traits>& os, const time_offset& offset)
{
return std::time_t(lhs) <= std::time_t(rhs);
if(offset.hour == 0 && offset.minute == 0)
{
os << 'Z';
return os;
}
if(static_cast<int>(offset.hour) * static_cast<int>(offset.minute) < 0)
{
const int min = static_cast<int>(offset.hour) * 60 + offset.minute;
if(min < 0){os << '-';} else {os << '+';}
os << std::setfill('0') << std::setw(2) << min / 60 << ':';
os << std::setfill('0') << std::setw(2) << min % 60;
return os;
}
if(offset.hour < 0){os << '-';} else {os << '+';}
os << std::setfill('0') << std::setw(2) << static_cast<int>(offset.hour) << ':';
os << std::setfill('0') << std::setw(2) << static_cast<int>(offset.minute);
return os;
}
template<typename uT, typename iT>
inline bool
operator>(basic_datetime<uT, iT> const& lhs, basic_datetime<uT, iT> const& rhs)
struct local_datetime
{
return std::time_t(lhs) > std::time_t(rhs);
local_date date;
local_time time;
local_datetime(local_date d, local_time t): date(d), time(t) {}
local_datetime(const std::tm& t): date(t), time(t){}
local_datetime(const std::chrono::system_clock::time_point& tp)
{
const auto t = std::chrono::system_clock::to_time_t(tp);
const auto tmp = std::localtime(&t); //XXX: not threadsafe!
assert(tmp); // if std::localtime fails, tmp is nullptr
std::tm time = *tmp;
this->date = local_date(time);
this->time = local_time(time);
// std::tm lacks subsecond information, so diff between tp and tm
// can be used to get millisecond & microsecond information.
const auto t_diff = tp -
std::chrono::system_clock::from_time_t(std::mktime(&time));
this->time.millisecond = std::chrono::duration_cast<
std::chrono::milliseconds>(t_diff).count();
this->time.microsecond = std::chrono::duration_cast<
std::chrono::microseconds>(t_diff).count();
}
local_datetime(const std::time_t t)
: local_datetime(std::chrono::system_clock::from_time_t(t))
{}
operator std::chrono::system_clock::time_point() const
{
// std::mktime returns date as local time zone. no conversion needed
return std::chrono::system_clock::time_point(this->date) +
std::chrono::microseconds(this->time);
}
operator std::time_t() const
{
return std::chrono::system_clock::to_time_t(
std::chrono::system_clock::time_point(*this));
}
local_datetime() = default;
~local_datetime() = default;
local_datetime(local_datetime const&) = default;
local_datetime(local_datetime&&) = default;
local_datetime& operator=(local_datetime const&) = default;
local_datetime& operator=(local_datetime&&) = default;
};
inline bool operator==(const local_datetime& lhs, const local_datetime& rhs)
{
return std::make_tuple(lhs.date, lhs.time) ==
std::make_tuple(rhs.date, rhs.time);
}
inline bool operator!=(const local_datetime& lhs, const local_datetime& rhs)
{
return !(lhs == rhs);
}
inline bool operator< (const local_datetime& lhs, const local_datetime& rhs)
{
return std::make_tuple(lhs.date, lhs.time) <
std::make_tuple(rhs.date, rhs.time);
}
inline bool operator<=(const local_datetime& lhs, const local_datetime& rhs)
{
return (lhs < rhs) || (lhs == rhs);
}
inline bool operator> (const local_datetime& lhs, const local_datetime& rhs)
{
return !(lhs <= rhs);
}
inline bool operator>=(const local_datetime& lhs, const local_datetime& rhs)
{
return !(lhs < rhs);
}
template<typename uT, typename iT>
inline bool
operator>=(basic_datetime<uT, iT> const& lhs, basic_datetime<uT, iT> const& rhs)
template<typename charT, typename traits>
std::basic_ostream<charT, traits>&
operator<<(std::basic_ostream<charT, traits>& os, const local_datetime& dt)
{
return std::time_t(lhs) >= std::time_t(rhs);
os << dt.date << 'T' << dt.time;
return os;
}
struct offset_datetime
{
local_date date;
local_time time;
time_offset offset;
offset_datetime(local_date d, local_time t, time_offset o)
: date(d), time(t), offset(o)
{}
offset_datetime(const local_datetime& ld)
: date(ld.date), time(ld.time), offset(get_local_offset())
{}
offset_datetime(const std::chrono::system_clock::time_point& tp)
: offset_datetime(local_datetime(tp))
{}
offset_datetime(const std::time_t& t)
: offset_datetime(local_datetime(t))
{}
offset_datetime(const std::tm& t)
: offset_datetime(local_datetime(t))
{}
operator std::chrono::system_clock::time_point() const
{
// get date-time in local timezone
std::chrono::system_clock::time_point tp =
std::chrono::system_clock::time_point(this->date) +
std::chrono::microseconds(this->time);
// get date-time in UTC by subtracting current offset
const auto ofs = get_local_offset();
tp -= std::chrono::hours (ofs.hour);
tp -= std::chrono::minutes(ofs.minute);
// add offset defined in this struct
tp += std::chrono::minutes(this->offset);
return tp;
}
operator std::time_t() const
{
return std::chrono::system_clock::to_time_t(
std::chrono::system_clock::time_point(*this));
}
offset_datetime() = default;
~offset_datetime() = default;
offset_datetime(offset_datetime const&) = default;
offset_datetime(offset_datetime&&) = default;
offset_datetime& operator=(offset_datetime const&) = default;
offset_datetime& operator=(offset_datetime&&) = default;
private:
static time_offset get_local_offset()
{
// get current timezone
const auto tmp1 = std::time(nullptr);
const auto tmp2 = std::localtime(&tmp1); // XXX not threadsafe!
assert(tmp2);
std::tm t = *tmp2;
std::array<char, 6> buf;
const auto result = std::strftime(buf.data(), 6, "%z", &t); // +hhmm\0
if(result != 5)
{
throw std::runtime_error("toml::offset_datetime: cannot obtain "
"timezone information of current env");
}
const int ofs = std::atoi(buf.data());
const int ofs_h = ofs / 100;
const int ofs_m = ofs - (ofs_h * 100);
return time_offset(ofs_h, ofs_m);
}
};
inline bool operator==(const offset_datetime& lhs, const offset_datetime& rhs)
{
return std::make_tuple(lhs.date, lhs.time, lhs.offset) ==
std::make_tuple(rhs.date, rhs.time, rhs.offset);
}
inline bool operator!=(const offset_datetime& lhs, const offset_datetime& rhs)
{
return !(lhs == rhs);
}
inline bool operator< (const offset_datetime& lhs, const offset_datetime& rhs)
{
return std::make_tuple(lhs.date, lhs.time, lhs.offset) <
std::make_tuple(rhs.date, rhs.time, rhs.offset);
}
inline bool operator<=(const offset_datetime& lhs, const offset_datetime& rhs)
{
return (lhs < rhs) || (lhs == rhs);
}
inline bool operator> (const offset_datetime& lhs, const offset_datetime& rhs)
{
return !(lhs <= rhs);
}
inline bool operator>=(const offset_datetime& lhs, const offset_datetime& rhs)
{
return !(lhs < rhs);
}
template<typename charT, typename traits>
std::basic_ostream<charT, traits>&
operator<<(std::basic_ostream<charT, traits>& os, const offset_datetime& dt)
{
os << dt.date << 'T' << dt.time << dt.offset;
return os;
}
}//toml
#endif// TOML11_DATETIME