How to Generate Timestamps in C++

One way to generate a timestamp in C++ is to use the std::chrono library. Here is an example code that can get the current timestamp:

#include <chrono>
#include <iostream>

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

    // 将时间点转换为时间戳
    auto timestamp = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()).count();

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

    return 0;
}

In this example, we use std::chrono::system_clock::now() to get the current time point, then use std::chrono::duration_cast to convert the time point to seconds, and finally obtain the integer value of the timestamp using the count() method.

bannerAds