std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
I'd like to print the now represented by in milliseconds. How should I describe it?
std::cout<<std::chrono::duration_cast<std::chrono::milliseconds>(now)<<std::endl;
encountered an error and could not output it.
c++
If the C++ compiler correctly supports the C++20 standard library (*), the code below will produce the desired output.
※: As of December 2022 only works with the latest Microsoft Visual C++.
#include<iostream>
# include <chrono>
int main()
{
auto now=std::chrono::system_clock::now();
auto now_ms=std::chrono::time_point_cast<std::chrono::milliseconds>(now);
std::cout<<now_ms<<std::endl;
}
Demo: https://godbolt.org/z/oWdY5eK59
In the questionnaire, std::chrono::steady_clock
is used, but this clock is not directly linked to a real-world clock in exchange for providing a "never retrograde watch" warranty.You should consider it as a tool to calculate the elapsed time between two points ( duration
).
If std::chrono::system_clock
is normally required to obtain the current time in the operating system time.
© 2024 OneMinuteCode. All rights reserved.