0
点赞
收藏
分享

微信扫一扫

【Linux系统获取时间的函数】

愚鱼看书说故事 2022-04-24 阅读 44
linux

文章目录


前言

在linux系统里面,我们有时候需要获取当前时间,用它来统计程序运行的时间,或者显示标准格式的日期和时间等等。那么怎么获取时间呢?下面就来了解一下。


Linux的API

获取时间

time函数

首先看看它在man 第2册中的介绍:

NAME
       time - get time in seconds

SYNOPSIS
       #include <time.h>

       time_t time(time_t *tloc);

DESCRIPTION
       time() returns the time as the number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).

       If tloc is non-NULL, the return value is also stored in the memory pointed to by tloc.

RETURN VALUE
       On  success, the value of time in seconds since the Epoch is returned.  On error, ((time_t) -1) is returned, and errno is set appro‐priately.

概括一下,如下表:

函数原型time_t time(time_t *tloc);备注
要包含的头文件#include <time.h>
形参time_t *tloc指针非空就把时间值放入指针指向的内存;指针为空就忽略
返回值成功:当前时间(单位为秒)/ 失败:((time_t) -1)返回的时间是从1970-01-01 00:00:00 +0000 (UTC)开始算起间隔的秒数

再啰嗦一遍: time函数,返回自Epoch以来的秒数,这是肯定的,同时可以接受一个time_t*类型的指针作为形参,执行该函数将会把这个秒数放入到指针所指向的位置 ,如果为NULL当然不用管了;

gettimeofday函数

函数原型:
include <sys/time.h>
int gettimeofday(struct timeval *tv, struct timezone *tz);

第一个参数 是一个结构体:

里面有秒数和微秒数,都是自Epoch以来的秒数。

第二个参数是一个时区结构,已经过时了,所以tz参数通常应该指定为NULL

该函数会把自Epoch以来的秒数和微秒数放到tv结构体。

函数调用成功返回0,失败返回-1,并设置errno。

time函数、gettimeofday函数使用代码示例

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>

#include <time.h>

int main()
{
    time_t tt;
    struct timeval tv;
    

    printf("Time Ret: %ld\n",time(NULL));
    printf("Time Ret: %ld\n",time(&tt));
    printf("Time Arg: %ld\n",tt);

    int ret=gettimeofday(&tv,NULL);
    printf("tv.tv_sec: %ld\n",tv.tv_sec);
    printf("tv.tv_usec: %ld\n",tv.tv_usec);
    return 0;
}

时间转换

问: 为什么要转换时间?
答:因为得到的时间是自Epoch以来的秒数,根本看不懂,于是需要把它转换为标准格式的日期和时间。

下面的函数可以实现这个功能。

ctime函数

最简单的是ctime函数,直接给返回一个长达26字节的字符串,包含了标准格式的日期和时间。

ctime函数使用代码示例

int main()
{
    time_t tt;   
    tt=time(NULL);
    printf("%s\n",ctime(&tt));
    return 0;
}

运行效果如下,下方是作为对比的date命令。
time

asctime函数

原型如下:
char *asctime(const struct tm *tm);

这个函数可以把任意一个tm结构体转换为标准格式的日期和时间。

asctime函数使用代码示例

#include <stdio.h>
#include <time.h>


int main()
{
    time_t tt;
    struct tm* ptm;
    char *p;

    tt=time(NULL);	//获取当前时间
    ptm=gmtime(&tt);	//分解时间得到tv结构指针
    p = asctime(ptm);	//格式化输出
    printf("now time is :%s\n", p);

    return 0;
}

运行效果如下:
asctime

时间的分解与合成

时间的分解

可以使用下面这个函数把时间分解,

首先是结构体 tm

struct tm {
               int tm_sec;    /* Seconds (0-60) */
               int tm_min;    /* Minutes (0-59) */
               int tm_hour;   /* Hours (0-23) */
               int tm_mday;   /* Day of the month (1-31) */
               int tm_mon;    /* Month (0-11) */
               int tm_year;   /* Year - 1900 */
               int tm_wday;   /* Day of the week (0-6, Sunday = 0) */
               int tm_yday;   /* Day in the year (0-365, 1 Jan = 0) */
               int tm_isdst;  /* Daylight saving time */
           };

可以看到这个结构体中包含了最基本的年月日、时分秒、星期,还有DST(夏令时)等。
更有意思的是,秒并不是从0到59,而是从0~60,这是为了考虑闰秒的问题。另外,年并不是直接是年,而是从1900开始的偏移量,所以如果今年是2022年的话,这个字段得到的结果是122。

什么是夏令时?

测试代码如下:

int main()
{
    time_t tt;   
    struct tm* ptm;
    tt=time(NULL);
    printf("%s",ctime(&tt));
    ptm=gmtime(&tt);
    printf("Hour: %d\n",ptm->tm_hour);
    return 0;
}

测试结果如下:

zzc@zzc-virtual-machine:~/share$ ./1
Sun Apr 24 00:00:30 2022
Hour: 16, DST:0

从结果来看,为什么ctime返回的时间是凌晨0点(24时),而gmtime返回的时间是16时?

因为ctime返回的是本地时间(这里是北京时间),而gmtime返回的是GMT时间(格林威治标准时间);北京时间是GMT+8,比格林威治标准时间早了8小时。

如果想直接分解成本地时间,那就把gmtime换成localtime。
localtime的函数原型:

时间的合成

时间的合成是时间的分解的逆向操作,所使用的函数是mktime,可以猜到,这个函数的参数是一个tm的结构体(或指针),返回值是time_t类型,函数原型如下:


总结

本文详细介绍了linux系统下有关时间的函数。

获取时间可以使用time、gettimeofday;
转换时间可以使用asctime、ctime;
分解时间可以使用gmtime、localtime;
合成时间可以使用mktime

感谢您的阅读,如有错误,欢迎批评指正。

举报

相关推荐

0 条评论