std::chrono::system_clock获取时间

static std::string CaculateCurrentTimeStamp() {
  // Y-M-D H:M:S
  char time_buffer[20];
  auto current_time = std::chrono::system_clock::now();
  auto current_time_t = std::chrono::system_clock::to_time_t(current_time);
  std::strftime(time_buffer,
                sizeof(time_buffer),
                "%Y-%m-%d %H:%M:%S",
                std::localtime(&current_time_t));

  // caulate milliseconds
  auto ms = current_time.time_since_epoch();
  auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(ms).count();
  auto const msecs = diff % 1000;

  // full time format: Y-M-D H:M:S.MS
  std::string full_time_buffer(time_buffer);
  full_time_buffer.append(".");
  full_time_buffer.append(std::to_string(msecs));
  return full_time_buffer;
}

  

猜你喜欢

转载自www.cnblogs.com/zhanghu52030/p/9145460.html