std::chrono::time_point::time_since_epoch

duration time_since_epoch() const;
(C++11 起)
(C++14 前)
constexpr duration time_since_epoch() const;
(C++14 起)

返回表示 *thisclock 的纪元间的时间量的 duration

参数

(无)

返回值

time_pointclock 的纪元间的时间量

示例

#include <iostream>
#include <chrono>
#include <ctime>
 
int main()
{
    std::chrono::time_point<std::chrono::system_clock> p1, p2, p3;
 
    p2 = std::chrono::system_clock::now();
    p3 = p2 - std::chrono::hours(24);
 
    std::time_t epoch_time = std::chrono::system_clock::to_time_t(p1);
    std::cout << "epoch: " << std::ctime(&epoch_time);
    std::time_t today_time = std::chrono::system_clock::to_time_t(p2);
    std::cout << "today: " << std::ctime(&today_time);
 
    std::cout << "hours since epoch: "
              << std::chrono::duration_cast<std::chrono::hours>(
                   p2.time_since_epoch()).count() 
              << '\n';
    std::cout << "yesterday, hours since epoch: "
              << std::chrono::duration_cast<std::chrono::hours>(
                   p3.time_since_epoch()).count() 
              << '\n';
}

可能的输出:

epoch: Thu Jan  1 00:00:00 1970
today: Fri Jun 30 10:44:11 2017
hours since epoch: 416338
yesterday, hours since epoch: 416314

版本历史

  • (当前 | 先前 2017年10月10日 (二) 19:27Fruderica讨论 | 贡献. . (1,569字节) (-445). . (撤销)
  • 当前 | 先前 2014年10月26日 (日) 16:42P12bot讨论 | 贡献 . . (2,014字节) (0). . (Fix some translations) (撤销)
  • 当前 | 先前 2013年7月2日 (二) 07:22P12bot讨论 | 贡献 . . (2,014字节) (-21). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 14:42P12bot讨论 | 贡献 . . (2,035字节) (+369). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 09:51P12讨论 | 贡献 . . (1,666字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 06:00TranslationBot讨论 | 贡献. . (1,666字节) (-35). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年10月25日 (四) 13:15P12讨论 | 贡献 . . (1,701字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 12:00TranslationBot讨论 | 贡献. . (1,701字节) (+1,701). . (Translated from the English version using Google Translate)