jeudi 5 août 2021

Are all of zero() in chrono the same thing

I've found many zero() in the namespace std::chrono:

std::chrono::system_clock::duration::zero();
std::chrono::minutes::zero();
std::chrono::seconds::zero();
std::chrono::milliseconds::zero();
...

Are they all the same thing?

I'm coding with std::chrono and I have many variables whose types are std::chrono::seconds, std::chrono::milliseconds. If I want to check if a variable is zero or not, which one should I use?

std::chrono::seconds start = std::chrono::duration_cast<T>(std::chrono::system_clock::now().time_since_epoch()); // get current time
// something
std::chrono::seconds end = std::chrono::duration_cast<T>(std::chrono::system_clock::now().time_since_epoch()); // get current time
if (end - start == 0) // std::chrono::system_clock::duration::zero()? std::seconds::zero()? std::milliseconds::zero()?
{
    // something
}

In this if condition, which kind of zero should I use? Or I can use anyone because they are exactly the same? You may want to say end == start works too, I can live with that but I still want to know if all of these zeros are the same thing.

Aucun commentaire:

Enregistrer un commentaire