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環境高級編程》


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