c时间函数strftime、struct timespec 和 struct timeval

时间和日期

由UNIX内核提供的基本时间服务是计算自协调世界时(Coordinated Universal Time,UTC)公元1970年1月1日00:00:00这一特定 时间以来经过的秒数。1.10节中曾提及这种秒数是以数据类型time_t表示 的,我们称它们为日历时间。日历时间包括时间和日期。UNIX在这方 面与其他操作系统的区别是:(a)以协调统一时间而非本地时间计 时;(b)可自动进行转换,如变换到夏令时;(c)将时间和日期作为 一个量值保存。

一般由函数int clock_gettime(clockid_t, struct timespec *)获取特定时钟的时间,常用如下4种时钟:
CLOCK_REALTIME 统当前时间,从1970年1.1日算起
CLOCK_MONOTONIC 系统的启动时间,不能被设置
CLOCK_PROCESS_CPUTIME_ID 本进程运行时间
CLOCK_THREAD_CPUTIME_ID 本线程运行时间

struct tm *localtime(const time_t *clock);  //线程不安全
struct tm* localtime_r( const time_t* timer, struct tm* result );//线程安全
size_t strftime (char* ptr, size_t maxsize, const char* format,const struct tm* timeptr );

time函数返回当前时间和日期。

#include 
time_t time(time_t *calptr);

返回值:若成功,返回时间值;若出错,返回-1 时间值作为函数值返回。如果参数非空,则时间值也存放在由calptr
指向的单元内。

POSXI.1的实时扩展增加了对多个系统时钟的支持。在Single UNIX
Specification V4中,控制这些时钟的接口从可选组被移至基本组。时钟 通过clockid_t类型进行标识。图6-8给出了标准值。


image.png

clock_gettime函数可用于获取指定时钟的时间,返回的时间在4.2节 介绍的timespec结构中,它把时间表示为秒和纳秒。

#include 
int clock_gettime(clockid_t clock_id, struct timespec *tsp);

写一段简单的代码

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


int main() {
    time_t time_unix = time(NULL);
    printf("this  code is intended for time\n");
    printf("unix CST: %ld\n", time_unix);

    /* test clock_gettime. */
    struct timespec time_sys;
    int ret = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &time_sys);
    printf("tv_nsec: %ld ns, tv_sec: %ld s\n", time_sys.tv_nsec, time_sys.tv_sec);
    return 0;
}
image.png

当时钟ID设置为CLOCK_REALTIME时,clock_gettime函数提供了
与time函数类似的功能,不过在系统支持高精度时间值的情况下, clock_gettime可能比time函数得到更高精度的时间值。

#include 
int clock_getres(clockid_t clock_id, struct timespec *tsp);

返回值:若成功,返回0;若出错,返回-1 clock_getres函数把参数tsp指向的timespec结构初始化为与clock_id参
数对应的时钟精度。例如,如果精度为1毫秒,则tv_sec字段就是0, tv_nsec字段就是1 000 000。

例如:

/*
 * @Author: machineplay
 * @Date: 2020-02-04 20:25:49
 * @Description: only for fun
 */
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


/**
 * main function.
 * @param 
 * @return: 0
 */
int main(int argc, char *argv[]) {

    /*  test time. */
    time_t now_time = time(NULL);
    printf("time_t :%ld\n", now_time);

    /* test sys_time. */
    int ret = 0;
    struct timespec time_sys;
    clock_getres(CLOCK_PROCESS_CPUTIME_ID, &time_sys);
    printf("process_time: %ld ns, %ld s\n", time_sys.tv_nsec, time_sys.tv_sec);
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time_sys);
    printf("process_time: %ld ns, %ld s\n", time_sys.tv_nsec, time_sys.tv_sec);

    

    return 0;
}

精度为1000ns


image.png

要对特定的时钟设置时间,可以调用clock_settime函数。

要对特定的时钟设置时间,可以调用clock_settime函数。

#include 
int clock_settime(clockid_t clock_id, const struct timespec *tsp);

返回值:若成功,返回0;若出错,返回-1 我们需要适当的特权来更改时钟值,但是有些时钟是不能修改的。

SUSv4指定gettimeofday函数现在已弃用。然而,一些程序仍然使用 这个函数,因为与time函数相比,gettimeofday提供了更高的精度(可到 微秒级)。

#include 
int gettimeofday(struct timeval *restrict tp, void *restrict tzp);

tzp的唯一合法值是NULL,其他值将产生不确定的结果。某些平台
支持用tzp说明时区,但这完全依实现而定,Single UNIX Specification对 此并没有定义。

gettimeofday函数以距特定时间(1970年1月1日00 : 00 : 00)的秒数的 方式将当前时间存放在tp指向的timeval结构中,而该结构将当前时间表 示为秒和微秒。

一旦取得这种从上述特定时间经过的秒数的整型时间值后,通常要 调用函数将其转换为分解的时间结构,然后调用另一个函数生成人们可 读的时间和日期。图6-9说明了各种时间函数之间的关系。(图中以虚 线表示的3个函数localtime、mktime和strftime都受到环境变量TZ的影响, 我们将在本节的最后部分对其进行说明。点划线表示了如何从时间相关 的结构获得日历时间。)
个函数localtime和gmtime将日历时间转换成分解的时间,并将这 些存放在一个tm结构中。

struct  tm {     /* a broken-down time */ 
int  tm_sec;    /* seconds after the minute: [0 -
60] */
int  tm_min;    /* minutes after the hour: [0 - 59]
*/
int  tm_hour;   /* hours after midnight: [0 - 23] */ 
int  tm_mday;   /* day of the month: [1 - 31] */ 
int  tm_mon;    /* months since January: [0 - 11]
*/
int  tm_year;   /* years since 1900 */ 
int  tm_wday;   /* days since Sunday: [0 - 6] */ 
int  tm_yday;   /* days since January 1: [0 - 365]
*/
int  tm_isdst;  /* daylight saving time flag: <0, 0,
>0 */ };

秒可以超过59的理由是可以表示润秒。注意,除了月日字段,其他 字段的值都以0开始。如果夏令时生效,则夏令时标志值为正;如果为 非夏令时时间,则该标志值为0;如果此信息不可用,则其值为负。

Single UNIX Specification的以前版本允许双润秒,于是, tm_sec值的有效范围是0~61。
UTC的正式定义不允许双润秒,所以,现在tm_sec值的有效范围定 义为0~60。


time
#include 
struct tm *gmtime(const time_t *calptr); struct tm *localtime(const time_t *calptr);

两个函数的返回值:指向分解的tm结构的指针;若出错,返回NULL localtime和gmtime之间的区别是:localtime将日历时间转换成本地时
间(考虑到本地时区和夏令时标志),而 gmtime 则将日历时间转换成 协调统一时间的年、月、日、时、分、秒、周日分解结构。
函数mktime以本地时间的年、月、日等作为参数,将其变换成 time_t值。

#include 
time_t mktime(struct tm *tmptr);

返回值:若成功,返回日历时间;若出错,返回-1

函数strftime是一个类似于printf的时间值函数。它非常复杂,可以
通过可用的多个参数来定制产生的字符串。

#include 
size_t strftime(char *restrict buf, size_t maxsize,
              const char *restrict format,
const struct tm *restrict tmptr);
size_t strftime_l(char *restrict buf, size_t maxsize,
              const char *restrict format,
const struct tm *restrict tmptr, locale_t locale);

两个函数的返回值:若有空间,返回存入数组的字符数;否则,返回0
两个较早的函数——asctime和ctime能用于产生一个26字节的可打 印的字符串,类似于date(1)命令默认的输出。然而,这些函数现在已 经被标记为弃用,因为它们易受到缓冲区溢出问题的影响。

strftime_l允许调用者将区域指定为参数,除此之外,strftime和 strftime_l函数是相同的。strftime使用通过TZ环境变量指定的区域。
tmptr参数是要格式化的时间值,由一个指向分解时间值tm结构的 指针说明。格式化结果存放在一个长度为maxsize个字符的buf数组中, 如果buf长度足以存放格式化结果及一个null终止符,则该函数返回在buf 中存放的字符数(不包括null终止符);否则该函数返回0。
format参数控制时间值的格式。如同printf函数一样,转换说明的形 式是百分号之后跟一个特定字符。format中的其他字符则按原样输出。 两个连续的百分号在输出中产生一个百分号。与printf函数的不同之处 是,每个转换说明产生一个不同的定长输出字符串,在format字符串中没有字段宽度修饰符。图6-10中列出了37种ISO C规定的转换说明。

image.png
#include 
#include 
#include 
#include 
#include 

int main() {
    /* time of timeval. */
    struct timeval now_time;

    int ret = gettimeofday(&now_time, NULL);
    printf("now_time since 1970: %d us, %ld s\n", now_time.tv_usec, now_time.tv_sec);

    /* time of timespec. */
    struct timespec time_spec;

    /* struct of tm*/
    struct tm detail_time;
    struct tm *tm_ptr = &detail_time;

    time_t tmp_time = time(NULL);
    printf("time_t : %ld\n", tmp_time);

    /* gmtime. */
    tm_ptr = gmtime(&tmp_time);


    /* show detail time. */
    printf("year: %d\n, month: %d\n, day: %d\n, hour: %d\n, min: %d\n, second %d\n",
           detail_time.tm_year, 
           detail_time.tm_mon, 
           detail_time.tm_mday, 
           detail_time.tm_hour, 
           detail_time.tm_min, 
           detail_time.tm_sec);

    /* test mktime. */
    tmp_time = mktime(tm_ptr);
    printf("timt_t after mktime %ld\n", tmp_time);

    /* change to localtime. */
    tm_ptr = localtime(&tmp_time);
    /* test strftime. */
    char buf[255];
    strftime(buf, sizeof(buf), "%c", tm_ptr);
    printf("strftime time: %s\n", buf);



    /* time */
    memset(buf, 0, 255);
    strftime(buf, sizeof(buf), "%Y%m%d,%H:%M:%S", tm_ptr);
    printf("time: %s\n", buf);
    return 0;

    /* local time. */
}


image.png
strptime函数是strftime的反过来版本,把字符串时间转换成分解
时间。
#include 
char *strptime(const char *restrict buf, const char
*restrict format,
              struct tm *restrict tmptr);

所以记得strftime年月日时分秒

%Y-%m-%d %H:%M:%S

你可能感兴趣的:(c时间函数strftime、struct timespec 和 struct timeval)