Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set a specific time using the std::chrono library?

Instead of using std::chrono::system_clock::now();

I would like to set my own specific hours\minutes\seconds. Similar to how one would go about using struct tm to manually construct a date-time.

What is the best way of doing this?

like image 469
Fuad Avatar asked May 18 '26 02:05

Fuad


1 Answers

I've found a solution I was looking for. See: A solution by Howard Hinnant

Basically you should use a 24hr duration

using days = std::chrono::duration<int, std::ratio<86400>>;
auto last_midnight = std::chrono::time_point_cast<days>(std::chrono::system_clock::now());

Then add the 24hr timestamp to it :)

like image 122
Fuad Avatar answered May 20 '26 18:05

Fuad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!