Search code examples
c++c++-chrono

How to reassign a time_point from clock::now()?


Given a std::chrono::sys_seconds time, one might expect to reassign time from its clock with something like:

time = decltype(time)::clock::now();

However, this would likely fail, because decltype(time)::clock::duration has nothing to do with decltype(time). Instead it is a predefined unit(likely finer than seconds), so you would have to manually cast it to a coarser unit.

Which means you would need to write something like:

time = std::chrono::time_point_cast<decltype(time)::duration>(decltype(time)::clock::now());

So my question is if there is a shorter syntax to do this?


Solution

  • an obvious solution is just write your own function

    template<typename Clock, typename Duration>
    void SetNow(std::chrono::time_point<Clock,Duration>& time){
        time = std::chrono::time_point_cast<Duration>(Clock::now());
    }
    
    // use
    void foo(){
        auto time = std::chrono::system_clock::now();
        SetNow(time);
    }
    

    you can also do some fancy overload

    struct Now_t{
        template<typename Clock,typename Duration>
        operator std::chrono::time_point<Clock,Duration>()const{
            return std::chrono::time_point_cast<Duration>(Clock::now());
        }
        consteval Now_t operator()()const{return {};} // enable time = Now();
    } Now;
    
    // use
    void foo() {
        auto time = std::chrono::system_clock::now();
        time = Now;
        time = Now();
    }