pjlib系列之時間time

Linux下的幾種時間

1、精確到秒級

time_t本質上是一個long型

time_t time(time_t *t);

//此函數會返回從公元1970年1月1日的UTC時間從0時0分0秒算起到現在所經過的秒數。如果t 並非空指針的話,此函數也會將返回值存到t指針所指的內存。

struct tm {
   int tm_sec;         /* seconds */
   int tm_min;         /* minutes */
   int tm_hour;        /* hours */
   int tm_mday;        /* day of the month */
   int tm_mon;         /* month */
   int tm_year;        /* year */
   int tm_wday;        /* day of the week */
   int tm_yday;        /* day in the year */
   int tm_isdst;       /* daylight saving time */
};
           
//int tm_sec 代表目前秒數,正常範圍爲0-59,但允許至61秒
//int tm_min 代表目前分數,範圍0-59
//int tm_hour 從午夜算起的時數,範圍爲0-23
//int tm_mday 目前月份的日數,範圍01-31
//int tm_mon 代表目前月份,從一月算起,範圍從0-11
//int tm_year 從1900 年算起至今的年數
//int tm_wday 一星期的日數,從星期一算起,範圍爲0-6
//int tm_yday 從今年1月1日算起至今的天數,範圍爲0-365
//int tm_isdst 日光節約時間的旗標
    struct tm *localtime(const time_t *timep);
    struct tm *localtime_r(const time_t *timep, struct tm *result);

/*該函數將有time函數獲取的值timep轉換真實世界所使用的時間日期表示方法,然後將結果由結構tm返回*/
/**需要注意的是localtime函數可以將時間轉換本地時間,但是localtime函數不是線程安全的。
多線程應用裏面,應該用localtime_r函數替代localtime函數,因爲localtime_r是線程安全的**/

time_t mktime(struct tm *tm);       
/**將時間結構體struct tm的值轉化爲經過的秒數**/

localtime將time_t轉換成tm,mktime相反,把tm轉換成time_t

2、精確到微妙級

int gettimeofday(struct timeval *tv, struct timezone *tz);  
struct timeval {
   time_t      tv_sec;     /* seconds (秒)*/
   suseconds_t tv_usec;    /* microseconds(微秒) */
};
struct timezone {
   int tz_minuteswest;     /* minutes west of Greenwich */
   int tz_dsttime;         /* type of DST correction */
};

gettimeofday函數獲取當前時間(從1970-01-01 00:00:00到現在,精確到微妙)存於tv結構體中,相應的時區信息則存於tz結構體中,需要注意的是tz是依賴於系統,不同的系統可能存在獲取不到的可能,因此通常設置爲NULL

3、精確到納秒級

int clock_gettime(clockid_t clk_id, struct timespect *tp);  

struct timespec  
{  
      time_t tv_sec; /* seconds */  
      long tv_nsec; /* nanoseconds */  
};  

    參數說明:  

    clockid_t clk_id 用於指定計時時鐘的類型,有以下4種:  

        CLOCK_REALTIME:系統實時時間,隨系統實時時間改變而改變,即從UTC1970-1-1 0:0:0開始計時,中間時刻如果系統時間被用戶該成其他,則對應的時間相應改變  

        CLOCK_MONOTONIC:從系統啓動這一刻起開始計時,不受系統時間被用戶改變的影響  

        CLOCK_PROCESS_CPUTIME_ID:本進程到當前代碼系統CPU花費的時間  

        CLOCK_THREAD_CPUTIME_ID:本線程到當前代碼系統CPU花費的時間  

    struct timespect *tp用來存儲當前的時間  

pjlib的時間

/**
 * Representation of time value in this library.
 * This type can be used to represent either an interval or a specific time
 * or date. 
 */
typedef struct pj_time_val
{
    /** The seconds part of the time. */
    long    sec;

    /** The miliseconds fraction of the time. */
    long    msec;

} pj_time_val;

void 	pj_time_val_normalize (pj_time_val *t)
#define 	PJ_TIME_VAL_MSEC(t)
#define 	PJ_TIME_VAL_EQ(t1, t2)
#define 	PJ_TIME_VAL_GT(t1, t2)
#define 	PJ_TIME_VAL_GTE(t1, t2)
#define 	PJ_TIME_VAL_LT(t1, t2)
#define 	PJ_TIME_VAL_LTE(t1, t2)
#define 	PJ_TIME_VAL_ADD(t1, t2)
#define 	PJ_TIME_VAL_SUB(t1, t2)

pj_status_t 	pj_gettimeofday (pj_time_val *tv)

pj_time_val跟Linux的timeval基本一致,只不過前者是毫秒級,後者是微妙級,我們程序中一般到毫秒級就夠了。PJ_TIME_XX幾個宏用來判斷兩個pj_time_val的大小,加減等。pj_gettimeofday用來獲取當前時間,其本質是調用系統的gettimeofday。

pj_time_val和幾個宏定義在types.h,pj_gettimeofday定義在os_time_unix.c。

/**
 * This structure represent the parsed representation of time.
 * It is acquired by calling #pj_time_decode().
 */
typedef struct pj_parsed_time
{
    /** This represents day of week where value zero means Sunday */
    int wday;

    /* This represents day of the year, 0-365, where zero means
     *  1st of January.
     */
    /*int yday; */

    /** This represents day of month: 1-31 */
    int day;

    /** This represents month, with the value is 0 - 11 (zero is January) */
    int mon;

    /** This represent the actual year (unlike in ANSI libc where
     *  the value must be added by 1900).
     */
    int year;

    /** This represents the second part, with the value is 0-59 */
    int sec;

    /** This represents the minute part, with the value is: 0-59 */
    int min;

    /** This represents the hour part, with the value is 0-23 */
    int hour;

    /** This represents the milisecond part, with the value is 0-999 */
    int msec;

} pj_parsed_time;

pjlib提供的另一個結構體pj_parsed_time類似tm,並提供了一些轉換函數。pj_parsed_time定義在types.h,幾個函數定義在os_time_common.c

pj_status_t 	pj_time_decode (const pj_time_val *tv, pj_parsed_time *pt)
pj_status_t 	pj_time_encode (const pj_parsed_time *pt, pj_time_val *tv)
pj_status_t 	pj_time_local_to_gmt (pj_time_val *tv)
pj_status_t 	pj_time_gmt_to_local (pj_time_val *tv)

 

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