Skip to content

TOML++ C++20/C++14 date support #291

@ArchNemSyS

Description

@ArchNemSyS

Firstly thankyou for TOML++ it is a pleasure to work with !!!

Is your feature request related to a problem? Please describe.

for my simple use of toml files all values parse/serialize neatly except dates. Internally I am using std::chrono::time_point which are a bit of a pain to work with TOML, whilst one can use the old c structs or floor math the amount of generated boilerplate code is tedious and repetitive. See parsing and serializing code below.

Describe the solution you'd like

toml::node::value_or that can return a std::chrono::time_point

Additional constructors for toml::date_time / toml::date / toml::time taking c++20 year_month_day and hh_mm_ss respectively.

c++14 support available if using the library version HowardHinnant date

Additional context

   // parse toml
    toml::date_time date_time = account_tbl["timestamp"].value_or(
        toml::date_time(toml::date(1970, 1, 1)));
    using namespace std::chrono;
    auto tpt = sys_days{year{date_time.date.year} / date_time.date.month /
                        date_time.date.day} +
               hours(date_time.time.hour) + minutes(date_time.time.minute) +
               seconds(date_time.time.second);

stackoverflow example

   // serialize to toml
    using namespace std::chrono;
    
    // Get a local time_point with system_clock::duration precision
    auto now = zoned_time{current_zone(), system_clock::now()}.get_local_time();

     // Get a local time_point with days precision
    auto ld = floor<days>(now);

    // Convert local days-precision time_point to a local {y, m, d} calendar
    year_month_day ymd{ld};

    // Split time since local midnight into {h, m, s, subseconds}
    hh_mm_ss hms{now - ld};

    int year{ymd.year()};
    int month = unsigned{ymd.month()};
    int day = unsigned{ymd.day()};
    int hour = hms.hours().count();
    int minute = hms.minutes().count();
    int second = hms.seconds().count();

    auto d = toml::date(year, month, day);
    auto t = toml::time(hour, minute, second);
     auto timestamp = toml::date_time(d,t);

Metadata

Metadata

Assignees

Labels

featureNew feature or enhancement request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions