• 时间与日期


    文章目录


    使用时间与日期需引用头文件。
    有四个与时间相关的类型:clock_t、time_t、size_t 和 tm。类型 clock_t、size_t 和 time_t 能够把系统时间和日期表示为某种整数。结构类型 tm 把日期和时间以 C 结构的形式保存,tm 结构的定义如下:

    struct tm {
      int tm_sec;   // 秒,正常范围从 0 到 59,但允许至 61
      int tm_min;   // 分,范围从 0 到 59
      int tm_hour;  // 小时,范围从 0 到 23
      int tm_mday;  // 一月中的第几天,范围从 1 到 31
      int tm_mon;   // 月,范围从 0 到 11
      int tm_year;  // 自 1900 年起的年数
      int tm_wday;  // 一周中的第几天,范围从 0 到 6,从星期日算起
      int tm_yday;  // 一年中的第几天,范围从 0 到 365,从 1 月 1 日算起
      int tm_isdst; // 夏令时
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    函数

    序号函数描述
    1time_t time(time_t *time)返回系统的当前日历时间,自 1970 年 1 月 1 日以来经过的秒数。如果系统没有时间,则返回 -1。
    2char *ctime(const time_t *time)返回一个表示当地时间的字符串指针,字符串形式 day month year hours:minutes:seconds year\n\0。
    3struct tm *localtime(const time_t *time)返回一个指向表示本地时间的 tm 结构的指针。
    4clock_t clock(void)返回程序执行起(一般为程序的开头),处理器时钟所使用的时间。如果时间不可用,则返回 -1。
    5char * asctime(const struct tm * time)返回一个指向字符串的指针,字符串包含了 time 所指向结构中存储的信息,返回形式为:day month date hours:minutes:seconds year\n\0。
    6struct tm *gmtime(const time_t * time)返回一个指向 time 的指针,time 为 tm 结构,用协调世界时(UTC)也被称为格林尼治标准时间(GMT)表示。
    7time_t mktime(struct tm *time)返回日历时间,相当于 time 所指向结构中存储的时间。
    8double difftime(time_t time2,time_t time1)返回 time1 和 time2 之间相差的秒数。
    9size_t strftime()可用于格式化日期和时间为指定的格式。
    #include 
    #include 
    #include 
    #include 
    using namespace std;
    
    int main ()
    {
        //基于当前系统的当前日期/时间
        time_t now =time(0);
        //把now转换为字符串形式
        char *dt=ctime(&now);
        cout<<"本地日期和时间:"<<dt<<endl;
        struct tm *info=localtime(&now);
        cout<<"日期和时间为:";
        cout<<info->tm_year+1900<<"-";
        cout<<info->tm_mon+1<<"-";
        cout<<info->tm_mday<<" ";
        cout<<info->tm_hour<<":";
        cout<<info->tm_min<<":";
        cout<<info->tm_sec<<endl;
    
        //格式化日期时间
        char buffer[80];
        strftime(buffer,80,"%Y-%m-%d %H:%M:%S",info);
        cout<<"格式化的日期&时间:|"<<buffer<<"|"<<endl;
    
        //把now转换为tm结构
        tm *gmtm=gmtime(&now);
        dt=asctime(gmtm);
        cout<<"UTC日期和时间:"<<dt<<endl;
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    结果

    本地日期和时间:Mon Aug 15 15:44:07 2022
    日期和时间为:2022-8-15 15:44:7
    格式化的日期&时间:|2022-08-15 15:44:07|
    UTC日期和时间:Mon Aug 15 07:44:07 2022

    格式化

    说明符替换为实例
    %a缩写的星期几名称Sun
    %A完整的星期几名称Sunday
    %b缩写的月份名称Mar
    %B完整的月份名称March
    %c日期和时间表示法Sun Aug 19 02:56:02 2012
    %d一月中的第几天(01-31)19
    %H24小时格式的小时(00-23)14
    %I12小时格式的小时(01-12)05
    %j一年中的第几天(001-366)231
    %m十进制数表示月份(01-12)08
    %M分(00-59)55
    %pAM或PM名称PM
    %S秒(00-61)02
    %U一年中的第几周,一第一个星期日作为第一周的第一天(00-53)33
    %w十进制数表示星期几,星期日表示0(0-6)4
    %W一年中的第几周,以第一个星期一作为第一周的第一天(00-53)34
    %x日期表示法08/19/12
    %X时间表示法02:50:06
    %y年份,最后两个数字(00-99)01
    %Y年份2012
    %Z时区的名称或缩写CDT
    %%一个%符号%
  • 相关阅读:
    Llama-2 推理和微调的硬件要求总结:RTX 3080 就可以微调最小模型
    lv5 嵌入式开发-11 消息队列
    Spring Cloud Alibaba —— 高可用流量控制组件
    (附源码)springboot流浪动物救助系统 毕业设计 180920
    【java、springMVC】REST风格
    软考系统架构之案例篇(架构设计相关概念)
    JWL-11/2-99.9A电流继电器
    10年开发大佬,用300案例,附学习路线,详解多线程编程核心
    npm 最新淘宝镜像配置 + nrm工具配置及使用
    【Python】网络编程
  • 原文地址:https://blog.csdn.net/xue208212674/article/details/126344879