首页  

C++11 chrono 获取时间戳     所属分类 c 浏览量 912

#include <chrono>
#include <iostream>
using std::cout; using std::endl;
using std::chrono::duration_cast;
using std::chrono::milliseconds;
using std::chrono::microseconds;
using std::chrono::seconds;
using std::chrono::system_clock;

int main() {
  auto sec_since_epoch = duration_cast<seconds>(system_clock::now().time_since_epoch()).count();
  auto millisec_since_epoch = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
  auto microsec_since_epoch = duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
  cout << "seconds since epoch     : " << sec_since_epoch << endl;
  cout << "milliseconds since epoch: " << millisec_since_epoch << endl;
  cout << "microseconds since epoch: " << microsec_since_epoch << endl;
  
  long seconds = (long)sec_since_epoch;
  long milliseconds = (long)millisec_since_epoch;
  long microseconds = (long)microsec_since_epoch;
  
 
  cout << seconds << endl;
  cout << milliseconds << endl;
  cout << microseconds << endl;
  
  // error: no matching function for call to 'duration_cast'
  // long seconds2 = duration_cast<seconds>(system_clock::now().time_since_epoch()).count();
  // cout << seconds2 << endl;
  
  return EXIT_SUCCESS;
}


上一篇     下一篇
C语言获取时间戳秒

C语言获取时间戳秒毫秒微秒

java 和 C 循环和递归性能比较

C语言字符串转浮点数

C语言排序和搜索

C语言 rand srand rand_r