c++ - How to get higher precision (fractions of a second) in a printout of current time? -
i've tried couple methods print out time system_clock can't other whole seconds:
system_clock::time_point = system_clock::now(); std::time_t now_c = system_clock::to_time_t(now); std::cout<<ctime(&now_c); std::cout<<std::put_time(std::localtime(&now_c), "%t")<<" "; does now() function hold high precision data, or can't find function extracts information printing?
note: not looking calculate time interval. want current time fractions of second, , print out via cout. can't find way this.
and know std::chrono::high_resolution_clock see no way print out now(). additionally, setprecision function has no effect on output of put_time or ctime.
i've been getting answers not address question.
you can following:
#include <chrono> #include <ctime> #include <iostream> #include <string> std::string getlocaltime() { auto now(std::chrono::system_clock::now()); auto seconds_since_epoch( std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch())); // construct time_t using 'seconds_since_epoch' rather 'now' since // implementation-defined whether value rounded or truncated. std::time_t now_t( std::chrono::system_clock::to_time_t( std::chrono::system_clock::time_point(seconds_since_epoch))); char temp[10]; if (!std::strftime(temp, 10, "%h:%m:%s.", std::localtime(&now_t))) return ""; return std::string(temp) + std::to_string((now.time_since_epoch() - seconds_since_epoch).count()); } int main() { std::cout << getlocaltime() << '\n'; return 0; }
Comments
Post a Comment