zoukankan      html  css  js  c++  java
  • c语言时间库函数#include<time.h>

    日期与时间函数<time.h>

    头文件<time.h>中说明了一些用于处理日期和时间的类型和函数。其中的一部分函数用于处理当地时间,因为时区等原因,当地时间与日历时间可能不相同。clock_t和time_t是两个用于表示时间的算术类型,而struct tm则用于存放日历时间的各个成分。tm的各个成员的用途及取值范围如下:

    int tm_sec; /* 秒,0~61 */
    int tm_min; /* 分,0~59 */
    int tm_hour; /* 时,0~23 */
    int tm_mday; /* 日,1~31 */
    int tm_mon; /* 月(从1月开始),0~11 */
    int tm_year; /* 年(从1900年开始) */
    int tm_wday; /* 星期(从周日开始),0~6 */
    int tm_yday; /* 天数(从1月1日开始),0~365 */
    int tm_isdst; /* 夏令时标记 */

    其中,tm_isdst在使用夏令时时其值为正,在不使用夏令时时其值为0,如果该信息不能使用,其值为负。

    1 clock

    #include <time.h>
    clock_t clock(void);

    返回程序自开始执行到目前为止所占用的处理机时间。如果处理机时间不可使用,那么返回-1。clock()/CLOCKS_PER_SEC是以秒为单位表示的时间。

    2 time

    #include <time.h>
    time_t time(time_t *tp);

    返回当前日历时间。如果日历时间不能使用,则返回-1。如果tp不为NULL,那么同时把返回值赋给*tp。

    3 difftime

    #include <time.h>
    double difftime(time_t time2, time_t time1);

    返回time2-time1的值(以秒为单位)。

    4 mktime

    #include <time.h>
    time_t mktime(struct tm *tp);

    将结构*tp中的当地时间转换为time_t类型的日历时间,并返回该时间。如果不能转换,则返回-1。

    5 asctime

    #include <time.h>
    char *asctime(const struct tm *tp);

    将结构*tp中的时间转换成如下所示的字符串形式:

    day month date hours:minutes:seconds year

    如:

    Fri Apr 15 15:14:13 2005

    返回指向该字符串的指针。字符串存储在可被其他调用重写的静态对象中。

    6 ctime

    #include <time.h>
    char *ctime(const time_t *tp);

    将*tp中的日历时间转换为当地时间的字符串,并返回指向该字符串指针。字符串存储在可被其他调用重写的静态对象中。等价于如下调用:

    asctime(localtime(tp));

    7 gmtime

    #include <time.h>
    struct tm *gmtime(const time_t *tp);

    将*tp中的日历时间转换成struct tm结构形式的国际标准时间(UTC),并返回指向该结构的指针。如果转换失败,返回NULL。结构内容存储在可被其他调用重写的静态对象中。

    8 localtime

    #include <time.h>
    struct tm *localtime(const time_t *tp);

    将*tp中的日历时间转换成struct tm结构形式的本地时间,并返回指向该结构的指针。结构内容存储在可被其他调用重写的静态对象中。

    9 strftime

    #include <time.h>
    size_t strftime(char *s, size_t smax, const char *fmt, const struct tm *tp);

    根据fmt的格式说明把结构*tp中的日期与时间信息转换成指定的格式,并存储到s所指向的数组中,写到s中的字符数不能多于smax。函数返回实际写到s中的字符数(不包括'');如果产生的字符数多于smax,则返回0。

    fmt类似于printf()中的格式说明,它由0个或多个转换规格说明与普通字符组成。普通字符原封不动的拷贝到s中,每个%c按照下面所描述的格式用与当地环境相适应的值来替换。转换规格列表如下:

    格式

    说明

    %a

    一星期中各天的缩写名

    %A

    一星期中各天的全名

    %b

    缩写月份名

    %B

    月份全名

    %c

    当地时间和日期表示

    %d

    用整数表示的一个月中的第几天(01~31)

    %H

    用整数表示的时(24小时制,00~23)

    %I

    用整数表示的时(12小时制,01~12)

    %j

    用整数表示的一年中各天(001~366)

    %m

    用整数表示的月份(01~12)

    %M

    用整数表示的分(00~59)

    %p

    与AM/PM对应的当地表示方法

    %S

    用整数表示的秒(00~61)

    %U

    用整数表示一年中的星期数(00~53,将星期日看作为每周的第一天)

    %w

    用整数表示一周中的各天(0~6,星期日为0)

    %W

    用整数表示一年中的星期数(00~53,将星期一看作为每周的第一天)

    %x

    当地日期表示

    %X

    当地时间表示

    %y

    不带公元的年(00~99)

    %Y

    完整年份表示

    %Z

    时区名字(可获得时)

    %%

    %本身

  • 相关阅读:
    AtCoder Grand Contest 015 题解
    AtCoder Grand Contest 014 题解
    AtCoder Grand Contest 013 题解
    AtCoder Grand Contest 012 题解
    AtCoder Grand Contest 011 题解
    AtCoder Grand Contest 010 题解
    AtCoder Grand Contest 009 题解
    NOIP2017 Day2 题解
    博客园主题备份
    多项式全家桶
  • 原文地址:https://www.cnblogs.com/haore147/p/3647579.html
Copyright © 2011-2022 走看看