时间点与时间段
// system_clock example #include <iostream> #include <ctime> #include <ratio> #include <> using namespace std; using namespace std::chrono; int main() { using std::chrono::system_clock; std::chrono::duration<int> one_day(1); system_clock::time_point today = system_clock::now(); system_clock::time_point tomorrow = today + one_day; std::time_t tt; tt = system_clock::to_time_t(today); // duration<long long, nano> nanoseconds; //纳秒 // duration<long long, micro> microseconds; //微妙 // duration<long long, milli> milliseconds; //毫秒 // duration<long long> seconds; // duration<int, ratio<60> > minutes; // duration<int, ratio<3600> > hours; std::chrono::milliseconds ms = std::chrono::duration_cast<std::chrono::milliseconds>(tomorrow- today); //std::cout << "time is: " << ms.count() << " "; std::cout << "time is: " <<today.time_since_epoch().count() << " "; //纳秒 std::cout << "time is: " << std::chrono::duration_cast<std::chrono::milliseconds>(today.time_since_epoch()).count() << " "; //毫秒 //std::cout << "today is: " << ctime(&tt); std::cout << "today is: " << tt <<" "; tt = system_clock::to_time_t(tomorrow); //std::cout << "tomorrow will be: " << ctime(&tt); std::cout << "tomorrow will be: " << tt<<" "; return 0; }
参考 C++11中的时间库std::chrono(引发关于时间的思考)_AlbertS Home of Technology-CSDN博客