Search code examples
c++c++-chrono

How to get the difference in milliseconds between the start time and now() in C++?


My initial code was

using n_time = std::chrono::high_resolution_clock;
n_time::time_point c_time = n_time::now();
n_time::time_point start = n_time::now();


auto gyro::duration(){
    return std::chrono::duration_cast<std::chrono::milliseconds>(c_time-start).count();
}

but then I realized that I wanted to find the milliseconds from a start argument to now(), and I wasn't sure if my initial code would give me that. So I tried

auto timeSince(n_time::time_point start) {
    n_time now = n_time::now();
    return std::chrono::duration_cast<std::chrono::milliseconds>(now - start).count();
}

Is this how you would find the milliseconds after start? I heard there was a function called time_since_epoch() and I'm not sure if that would be better to use instead?

This clock is for a specific robot function I'm trying to write, so I need to make sure that the clock doesn't cause a conversion error:

void straight(int distance, int speed) { 
    int time = (distance / speed) * 1000; // milliseconds        
    while (timeSince() < time) {
        // code
    }
}

The duration() function does not give any errors in my function, but the timeSince() gives me conversion errors such as

error: conversion from 'int' to non-scalar type 'std::chrono::_V2::system_clock::time_point {aka std::chrono::time_point >


Solution

  • If you take a close look at the C++ standard related to std::chrono::high_resolution_clock, you will see that it contains a member type std::chrono::high_resolution_clock::time_point which is equivalent to std::chrono::time_point<std::chrono::high_resolution_clock>.

    Besides the above member type, std::chrono::high_resolution_clock contains member function now which returns a std::chrono::time_point<std::chrono::high_resolution_clock>, i.e. std::chrono::high_resolution_clock::time_point, representing the current value of the clock.

    In your function, you have

    n_time now = n_time::now();
    

    i.e. you are trying to assign std::chrono::high_resolution_clock::time_point to a variable of type std::chrono::high_resolution_clock which does not sound clear, right? So, instead you should have

    n_time::time_point now = n_time::now();
    

    Here is the full code:

    #include <chrono>
    #include <iostream>
    
    using n_time = std::chrono::high_resolution_clock;
    
    auto timeSince(n_time::time_point start) {
        n_time::time_point now = n_time::now();
        return std::chrono::duration_cast<std::chrono::milliseconds>(now - start).count();
    }
    
    int main() {
        auto now = n_time::now() - std::chrono::seconds{3600};
        std::cout << timeSince(now) << std::endl;
        return 0;
    }
    

    And the result is in milliseconds:

    3600000
    

    UPDATE

    Here is how you can use the above working example in your case:

    void straight(int distance, int speed) {
        auto start = n_time::now();
    
        // some time has passed since this function has started
    
        int time = (distance / speed) * 1000; // milliseconds        
        while (timeSince(start) < time) {
            // code
        }
    }