Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 number of days since date

Tags:

c++

time

c++11

I need to store date in flat file. Is there any simple metod to get number of days since particular date (for example since 1 AD) using standard libary in C++11? The only one I know is to:

  • obtain std::tm structure, set all time values to 0
  • convert it to std::time_t (I do not know proper method yet)
  • divide time_t to get the resolution of one day

Can I perform it easier?

like image 267
Dejwi Avatar asked Dec 06 '25 03:12

Dejwi


1 Answers

What you said sounds reasonable to start with. You can convert the struct tm to time_t using mktime. Note that mktime interprets its input as local time according to the system/environment settings, and there is no UTC counterpart that's as widely available without relying on "extra" libraries, but maybe that's not a problem for you.

like image 182
John Zwinck Avatar answered Dec 09 '25 00:12

John Zwinck



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!