https://blog.csdn.net/qq_37286579/article/details/130317105
https://blog.csdn.net/ambizxzh/article/details/74136068 (说明了年为什么要加1900,月要加1)

#include <iostream>
#include <ctime>

int main() {
    // 获取当前时间的 Unix 时间戳
    std::time_t now = std::time(nullptr);

    // 将 Unix 时间戳转换为本地时间
    std::tm* local_time = std::localtime(&now);

    // 获取年、月、日、时、分、秒等信息
    int year = local_time->tm_year + 1900;
    int month = local_time->tm_mon + 1;
    int day = local_time->tm_mday;
    int hour = local_time->tm_hour;
    int minute = local_time->tm_min;
    int second = local_time->tm_sec;

    // 输出当前时间
    std::cout << "Current time: " << year << "-" << month << "-" << day << " "
              << hour << ":" << minute << ":" << second << std::endl;

    return 0;
}

时间格式化函数strftime

https://www.freesion.com/article/5169257634/
https://www.runoob.com/cprogramming/c-function-strftime.html

%F  等价于 %Y-%m-%d,
%T   等价于 %H:%M:%S

获取微秒时间gettimeofday

https://www.xjx100.cn/news/387855.html?action=onClick


0 条评论

发表回复

您的电子邮箱地址不会被公开。