c++ - Most elegant way to combine chrono::time_point from hours, minutes, seconds etc -
i have "human readable" variables hours
, minutes
, seconds
, day
, month
, year
contains values corresponding names (let's have systemtime
structure <windows.h>
).
way found create chrono::time_point
is:
systemtime systime = ...; // came source (file, network, etc. ) tm t; t.tm_sec = systime.wsecond; t.tm_min = systime.wminute; t.tm_hour = systime.whour; t.tm_mday = systime.wday; t.tm_mon = systime.wmonth - 1; t.tm_year = systime.wyear - 1900; t.tm_isdst = 0; std::chrono::system_clock::time_point datetime = std::chrono::system_clock::from_time_t( mktime( & t ) );
first, lost milliseconds systemtime
.
second, (mmm...) don't sort of conversion ))
could give more elegant way issue ?
using this open source, header-only library, can:
#include "date.h" #include <iostream> struct systemtime { int wmilliseconds; int wsecond; int wminute; int whour; int wday; int wmonth; int wyear; }; int main() { systemtime systime = {123, 38, 9, 10, 8, 7, 2015}; std::chrono::system_clock::time_point datetime = date::sys_days(date::year(systime.wyear) /date::month(systime.wmonth) /date::day(systime.wday)) + std::chrono::hours(systime.whour) + std::chrono::minutes(systime.wminute) + std::chrono::seconds(systime.wsecond) + std::chrono::milliseconds(systime.wmilliseconds); std::cout << datetime << '\n'; }
which outputs:
2015-07-08 10:09:38.123000
in "date.h", may have play around these macros things compile vs.:
# define constdata const # define constcd11 # define constcd14
with std-conforming c++14 compiler, these macros should set to:
# define constdata constexpr # define constcd11 constexpr # define constcd14 constexpr
Comments
Post a Comment