0
votes

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 >

2
You may consider using auto for the type, because your n_time now=n_time::now(); is wrong type. Besides this, your initial method is correct.sz ppeter

2 Answers

2
votes

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
    }
}
1
votes

I heard there was a function called time_since_epoch() and I'm not sure if that would be better to use instead

Not really, see e.g. When is std::chrono epoch?:

There is a de-facto (unofficial) standard that std::chrono::system_clock::time_point has an epoch consistent with Unix Time. This is defined as the time duration that has elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970, not counting leap seconds.

The error you see is generated by this line

n_time now = n_time::now();

Which should be

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

Given using n_time = std::chrono::high_resolution_clock;