Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add time of day information to year_month_day with Howard Hinnant's date library

Tags:

c++

datetime

I'm working on a specific implementation in C++. Suppose that I have a date::year_month_day variable from Howard Hinnant's date library called X and I want to convert it to a std::chrono::time_point that represents the same time as X but with an added offset of A hours, B minutes and C seconds.

How can I achieve this?

like image 578
John Avatar asked Nov 22 '25 21:11

John


1 Answers

using namespace std::chrono;
auto tp = date::sys_days{X} + hours{A} + minutes{B} + seconds{C};

The type of tp is std::chrono::time_point<system_clock, seconds>, and represents a time point in UTC.

The only thing Howard Hinnant's date library adds in this example is the conversion from date::year_month_day to sys_days, which itself is just a typedef for time_point<system_clock, days>. After that conversion, you're working entirely within the C++11/14 <chrono> library.

like image 133
Howard Hinnant Avatar answered Nov 26 '25 16:11

Howard Hinnant



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!