Struct local_date

Synopsis

#include <toml/datetime.hpp>

struct local_date

Description

No description yet.

Methods

local_date overload
~local_date
operator std::chrono::system_clock::time_point
operator std::time_t
operator= overload

Source

Lines 86-143 in toml/datetime.hpp.

struct local_date
{
    std::int16_t year;   // A.D. (like, 2018)
    std::uint8_t month;  // [0, 11]
    std::uint8_t day;    // [1, 31]

    local_date(int y, month_t m, int d)
        : year (static_cast<std::int16_t>(y)),
          month(static_cast<std::uint8_t>(m)),
          day  (static_cast<std::uint8_t>(d))
    {}

    explicit local_date(const std::tm& t)
        : year (static_cast<std::int16_t>(t.tm_year + 1900)),
          month(static_cast<std::uint8_t>(t.tm_mon)),
          day  (static_cast<std::uint8_t>(t.tm_mday))
    {}

    explicit local_date(const std::chrono::system_clock::time_point& tp)
    {
        const auto t    = std::chrono::system_clock::to_time_t(tp);
        const auto time = detail::localtime_s(&t);
        *this = local_date(time);
    }

    explicit 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::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  = static_cast<int>(this->day);
        t.tm_mon   = static_cast<int>(this->month);
        t.tm_year  = static_cast<int>(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));
    }

    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;
};





Add Discussion as Guest

Log in