c++ - How to add milliseconds to the time duration since epoch? -


using std::chrono library, have variable type unsigned long, representing number of milliseconds 1 jan 1970 now, , want add 100 milliseconds it.

unsigned long = std::chrono::system_clock::now().time_since_epoch().count(); unsigned long t100ms = std::chrono::milliseconds(100).count(); unsigned long time = + t100ms; 

when printing t100ms, "1000".

if print value of std::chrono::system_clock::now every second, see incrementing number not increased 1000 @ each iteration (as 1 second equal 1000 milliseconds, should case).

question

does std::chrono::system_clock::now().time_since_epoch() not return amount of time has passed since 1 jan 1970 represented in milliseconds?

std::chrono::system_clock::now().time_since_epoch() elapsed time since epoch, expressed in whatever units system clock chooses use; these attoseconds, or years, or in between. clearly, adding number of milliseconds number of nanoseconds (for example) nonsensical.

if want perform arithmetic on duration values, should remain in duration types perform unit conversion you, , call count() when want print out result:

auto = std::chrono::system_clock::now().time_since_epoch(); auto t100ms = std::chrono::milliseconds(100); auto time = + t100ms; std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(time).count(); 

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -