How to get the current timestamp in C++?

In C++, you can use the std::chrono library to retrieve the current timestamp. Here is an example code:

#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;
}

In the given code snippet, the current time is first retrieved using the std::chrono::system_clock::now() function, then converted to a timestamp using std::chrono::system_clock::to_time_t() function. Finally, the timestamp is printed out.

bannerAds