c++ 日期和时间

c++标准库没有提供专门的日期类型, 而是继承了c语言用于日期和时间的结构和函数. 需要c++中引入头文件.

关键字 解释
UTC 协调世界时
Calendar Time 日历时间
epoch 时间点
clock tick 时钟计时单元

关于UTC简称: 英文(CUT, Coordinated Universal Time)和法文(TUC, Temps Universel Cordonné)的缩写不同, 作为妥协, 就简称UTC(Universel Time Coordinated)了.

格式化时间

#include <ctime> //加载时间库
#include <iostream>
int main()
{
    std::time_t nowtime = std::time(NULL); //获取时间戳
    std::cout << "nowtime = " << nowtime << std::endl; //输出nowtime = 1626935009

    struct tm * local;
    local = std::localtime(&nowtime);
    char buf[80];
    strftime(buf, 80, "%Y_%m_%d_%H_%M_%S", local); //将时间格式化
    std::cout << "strftime= " << buf << std::endl; //输出strftime= 2021_07_22_14_23_29

    return 0;
}

posted @ 2022-06-02 14:25  编程驴子  阅读(109)  评论(0编辑  收藏  举报