温馨提示×

c++ datetime怎样表示和使用

c++
小樊
110
2024-07-12 15:16:34
栏目: 编程语言

在C++中,可以使用std::chrono和std::time库来表示和操作日期时间。以下是一个简单的示例来展示如何使用C++表示和操作日期时间:

#include <iostream>
#include <chrono>
#include <ctime>

int main() {
    // 获取当前时间
    auto now = std::chrono::system_clock::now();
    
    // 将时间转换为时间戳
    std::time_t time = std::chrono::system_clock::to_time_t(now);
    
    // 输出当前时间
    std::cout << "Current time: " << std::ctime(&time) << std::endl;
    
    // 创建一个时间点
    std::chrono::system_clock::time_point future = now + std::chrono::hours(24);
    
    // 将时间点转换为时间戳
    std::time_t future_time = std::chrono::system_clock::to_time_t(future);
    
    // 输出未来时间
    std::cout << "Future time: " << std::ctime(&future_time) << std::endl;
    
    return 0;
}

在上面的示例中,我们首先获取当前时间,然后将当前时间转换为时间戳并输出。接着我们创建一个未来时间点,将其转换为时间戳并输出。这是一个简单的使用C++表示和操作日期时间的示例。

0