std::chrono::time_point<Clock,Duration>::time_since_epoch
来自cppreference.com
< cpp | chrono | time point
duration time_since_epoch() const; |
(C++11 起) (C++14 起为 constexpr ) |
|
返回表示 *this 与 clock
的纪元间的时间量的 duration。
参数
(无)
返回值
此 time_point
与 clock
的纪元间的时间量。
示例
运行此代码
#include <chrono> #include <ctime> #include <iostream> int main() { const auto p0 = std::chrono::time_point<std::chrono::system_clock>{}; const auto p1 = std::chrono::system_clock::now(); const auto p2 = p1 - std::chrono::hours(24); std::time_t epoch_time = std::chrono::system_clock::to_time_t(p0); std::cout << "纪元: " << std::ctime(&epoch_time); std::time_t today_time = std::chrono::system_clock::to_time_t(p1); std::cout << "今天: " << std::ctime(&today_time); std::cout << "纪元起小时数: " << std::chrono::duration_cast<std::chrono::hours>( p1.time_since_epoch()).count() << '\n'; std::cout << "昨天,纪元起小时数: " << std::chrono::duration_cast<std::chrono::hours>( p2.time_since_epoch()).count() << '\n'; }
可能的输出:
纪元: Thu Jan 1 00:00:00 1970 今天: Fri Jun 30 10:44:11 2017 纪元起小时数: 416338 昨天,纪元起小时数: 416314