Search code examples
c++datec++-chrono

Set sys_seconds to the epoch i.e. zero


Am struggling to find a good way to reset sys_seconds to zero

date::sys_seconds st;
//st = date::sys_seconds(0);            //no matching conversion for functional-style cast from 'int' to 'date::sys_seconds'
//st = date::sys_seconds{date::sys_seconds(0)}; //no matching conversion for functional-style cast from 'int' to 'date::sys_seconds'
//st = std::chrono::system_clock::from_time_t(0);   //no viable overloaded '='
//st = date::sys_seconds(std::chrono::system_clock::from_time_t(0));    //no matching conversion for functional-style cast 
st = std::chrono::time_point_cast<std::chrono::seconds>(std::chrono::system_clock::from_time_t(0)); //works, but verbose

Is there a better way?


Solution

  • st = {};
    

    Explanation: The default constructor of sys_seconds sets things to zero. So this just default constructs a sys_seconds and assigns it to st. Other ways to do this include:

    st = date::sys_seconds{};
    

    and:

    using namespace std::chrono_literals;
    st = date::sys_seconds{0s};