linux下的时间操作

1 概述

由UNIX内核提供的基本时间服务是计算自国际标准时间公元1970年1月1日00:00:00以来经过的秒数。这种秒数是以数据类型time_t表示的,我们称之为日历时间。一旦取得这种以秒计的整型时间值后,通常要调用另一个时间函数将其转换为人们可读的时间和日期,我们称之为分解时间。为了将分解时间以各种形式显示出来,通常要调用一些函数将其转换成相应的字符串。具体转换如下图:


2 日历时间

2.1 time函数

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

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

时间值总是作为函数值返回。如果参数不为空,则时间值也存放在由calptr指向的单元内。

简单应用:

time_t cur_time;
cur_time = time(NULL);
or
time(&cur_time);

2.2 gettimeofday函数

与time函数相比,gettimeofday提供了更高的分辨率(最高为微妙级)。

#include <sys/time.h>
int gettimeofday(struct timeval *restrict tp, void *restrict tzp);

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

gettimeofday函数将当前时间存放在tp指向的timeval结构中,而该结构存储秒和微妙。

struct timeval{
time_t tv_sec;/* seconds */
long     tv_usec; /* microseconds */
}

3 分解时间

两个函数localtime和gmtime将日历时间转换成以年/月/日/时/分/秒/周日表示的时间,并将这些存放在一个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 since 1900 */
               int tm_wday;        /* day of the week since Sunday: [0 - 6]*/
               int tm_yday;        /* day in the year since January 1: [0 - 365]*/
               int tm_isdst;       /* daylight saving time: <0, 0, >0 */
 };

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

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

localtime和gmtime之间的区别是:localtime将日历时间转换成本地时间(考虑到本地时区和夏时制标志),而gmtime则将日历时间转换成国际标准时间。

函数mktime以本地时间的年月日等作为参数,将其转换成time_t值。

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

4 字符串时间

4.1 默认形式

asctime和ctime函数产生大家都熟悉的26字节的字符串,这与data(1)命令的系统默认输出形式类似。

如:Thu Oct 15 11:09:25 2015

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

asctime的参数是指向年/月/日等字符串的指针,而ctime的参数则是指向日历时间的指针。

4.2 可变形式

最后一个时间函数是strftime,它是非常复杂的类似于printf的时间值函数。

#include <time.h>
size_t strftime(char *buf, size_t max, const char *format,
                       const struct tm *tm);

最后一个参数是要格式化的时间值,由一个指向tm结构的指针指定。格式化结果存放在一个长度为max个字符的buf数组中,如果buf长度足以存放格式化结果及一个null终止符,则该函数返回在buf中存放的字符数(不包括null),否则该函数返回0.

格式:



5 实例

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


int main()
{   
    time_t cur_time;
    struct tm *tp;
    char date_time[64];


    memset(date_time, 0, sizeof(date_time));
                               
    /* get calendar time */
    cur_time = time(NULL);
                               
    /* get local time */
    tp = localtime(&cur_time);


    /* ctime */
    printf("ctime: %s\n", ctime(&cur_time));
    /* asctime */
    printf("asctime: %s\n", asctime(tp));


    /* strftime */
    strftime(date_time, sizeof(date_time), "%m-%d-%Y %I:%M%p\n", tp);
    printf("strftime: %s\n", date_time);
    
    return 0;
}

输出结果:

ctime: Thu Oct 15 14:31:49 2015
asctime: Thu Oct 15 14:31:49 2015
strftime: 10-15-2015 02:31PM

参考文章:

《c语言程序设计现代方法》/《UNIX环境高级编程》


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章