获取1.5小时之前的代码

#include <iostream>
#include <chrono>
#include <ctime>
#include <iomanip>
#include <sstream>

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

    // 计算1.5小时前的时间点
    auto oneAndHalfHoursAgo = now - std::chrono::hours(1) - std::chrono::minutes(30);

    // 将时间点转换为time_t以便格式化输出
    std::time_t time_t_now = std::chrono::system_clock::to_time_t(now);
    std::time_t time_t_oneAndHalfHoursAgo = std::chrono::system_clock::to_time_t(oneAndHalfHoursAgo);

    // 格式化输出
    char buffer[80];
    strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", localtime(&time_t_now));
    std::cout << "当前时间: " << buffer << std::endl;

    strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", localtime(&time_t_oneAndHalfHoursAgo));
    std::cout << "1.5小时前: " << buffer << std::endl;

    return 0;
}

 

posted @ 2025-01-23 16:36  leochan007  阅读(17)  评论(0)    收藏  举报