扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

c++怎么获取当前时间戳

扬州沐宇科技
2024-01-23 17:11:28
c++

在C++中,可以使用std::chrono库来获取当前时间戳。下面是一个示例代码:

#include <iostream>
#include <chrono>

int main() {
  // 获取当前时间点
  auto now = std::chrono::system_clock::now();

  // 将当前时间点转换为时间戳(以秒为单位)
  std::time_t timestamp = std::chrono::system_clock::to_time_t(now);

  // 打印时间戳
  std::cout << "当前时间戳:" << timestamp << std::endl;

  return 0;
}

在上面的代码中,首先使用std::chrono::system_clock::now()函数获取当前时间点,然后使用std::chrono::system_clock::to_time_t()函数将时间点转换为时间戳。最后,将时间戳打印出来。

扫码添加客服微信