Struct local_datetime


#include <toml/datetime.hpp>

struct local_datetime


No description yet.


local_datetime overload
operator std::chrono::system_clock::time_point
operator std::time_t
operator= overload


Lines 365-442 in toml/datetime.hpp.

struct local_datetime
    local_date date;
    local_time time;

    local_datetime(local_date d, local_time t): date(d), time(t) {}

    explicit local_datetime(const std::tm& t): date(t), time(t){}

    explicit local_datetime(const std::chrono::system_clock::time_point& tp)
        const auto t = std::chrono::system_clock::to_time_t(tp);
        std::tm ltime = detail::localtime_s(&t);

        this->date = local_date(ltime);
        this->time = local_time(ltime);

        // std::tm lacks subsecond information, so diff between tp and tm
        // can be used to get millisecond & microsecond information.
        const auto t_diff = tp -
        this->time.millisecond = static_cast<std::uint16_t>(
        this->time.microsecond = static_cast<std::uint16_t>(
        this->time.nanosecond = static_cast<std::uint16_t>(
          std::chrono::duration_cast<std::chrono::nanoseconds >(t_diff).count());

    explicit 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
        using internal_duration =
            typename std::chrono::system_clock::time_point::duration;

        // Normally DST begins at A.M. 3 or 4. If we re-use conversion operator
        // of local_date and local_time independently, the conversion fails if
        // it is the day when DST begins or ends. Since local_date considers the
        // time is 00:00 A.M. and local_time does not consider DST because it
        // does not have any date information. We need to consider both date and
        // time information at the same time to convert it correctly.

        std::tm t;
        t.tm_sec   = static_cast<int>(this->time.second);
        t.tm_min   = static_cast<int>(this->time.minute);
        t.tm_hour  = static_cast<int>(this->time.hour);
        t.tm_mday  = static_cast<int>(this->;
        t.tm_mon   = static_cast<int>(this->date.month);
        t.tm_year  = static_cast<int>(this->date.year) - 1900;
        t.tm_wday  = 0; // the value will be ignored
        t.tm_yday  = 0; // the value will be ignored
        t.tm_isdst = -1;

        // std::mktime returns date as local time zone. no conversion needed
        auto dt = std::chrono::system_clock::from_time_t(std::mktime(&t));
        dt += std::chrono::duration_cast<internal_duration>(
                std::chrono::milliseconds(this->time.millisecond) +
                std::chrono::microseconds(this->time.microsecond) +
                std::chrono::nanoseconds (this->time.nanosecond));
        return dt;

    operator std::time_t() const
        return std::chrono::system_clock::to_time_t(

    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;

Add Discussion as Guest

Log in