Convert C# DateTime to C++ std::chrono::system_clock::time_point -
i need, in c++/clr code, convert datetime
coming c# c++ std::chrono::system_clock:time_point
equivalent.
will code job os there better way ?
std::chrono::system_clock::time_point convertdatetime(datetime datetime) { int32 unixtimestamp = (int32)(datetime.utcnow.subtract(new datetime(1970, 1, 1))).totalseconds; std::chrono::system_clock::time_point ret = std::chrono::system_clock::from_time_t(unixtimestamp ); return ret; }
i don't think code works, though don't have datetime
available test (i'm not on windows). utcnow
"static" function returns current expressed "the current utc time", means: time duration past 1970-01-01 00:00:00 utc. believe formulation independent of value of parameter datetime
.
i recommend trafficking in terms of datetime.ticks
number of 100-nanosecond ticks past 1900-01-01 00:00:00 utc utc defined proleptically.
using free, open source, header-only library, can find difference between datetime
epoch , system_clock
epoch in terms of 100-nanosecond ticks vs uses:
it first convenient define ticks
100-nanoseconds units:
using ticks = std::chrono::duration<std::int64_t, std::ratio_multiply<std::ratio<100>, std::nano>>;
and convertdatetime
can written:
std::chrono::system_clock::time_point convertdatetime(datetime datetime) { using namespace date; using namespace std::chrono; return system_clock::time_point{ticks{datetime.ticks} - (sys_days{1970_y/jan/1} - sys_days{1900_y/jan/1})}; }
if dislike idea of using this date library, can hard code magic constant in above function so:
std::chrono::system_clock::time_point convertdatetime(datetime datetime) { using namespace std::chrono; return system_clock::time_point{ticks{datetime.ticks - 22089888000000000}}; }
either 1 of these gets system_clock::time_point
exact same precision datetime
(on vs).
Comments
Post a Comment