time 、localtime、localtime_s、localtime_r、gettimeofday 的使用

1. time 函數
    
    原型: time_t time(time_t *calptr)
     得到自1970-1-1, 00:00:00以來經過的秒數,結果可以通過返回值,也可以通過參數得到,見實例
    頭文件 <time.h>
    返回值:
        成功:秒數
        失敗:-1
    例:
      time_t now;
      time(&now);
     // 等同於now = time(NULL)
      printf("now time is %d\n", now);

2.  localtime 用來獲取系統時間,精度爲秒

         將時間(秒)數值變換成本地時間,考慮到本地時區和夏令時標誌;
    原型: struct tm *localtime(const time_t * calptr);
    頭文件 <time.h>
返回值:
        成功: struct tm *結構體, 原型如下:
                struct tm {
                       int tm_sec;       /* 秒 – 取值區間爲[0,59] */
                       int tm_min;       /* 分 - 取值區間爲[0,59] */
                       int tm_hour;      /* 時 - 取值區間爲[0,23] */
                       int tm_mday;     /* 一個月中的日期 - 取值區間爲[1,31] */
                       int tm_mon;     /* 月份(從一月開始,0代表一月) - 取值區間爲[0,11] */
                       int tm_year;     /* 年份,其值等於實際年份減去1900 */
                       int tm_wday;    /* 星期 – 取值區間爲[0,6],其中0代表星期天,1代表星期一 */
                       int tm_yday;    /* 從每年1月1日開始的天數– 取值區間[0,365],其中0代表1月1日 */
                       int tm_isdst;    /* 夏令時標識符,夏令時tm_isdst爲正;不實行夏令時tm_isdst爲0 */    
                };
               注意:此結構體空間由內核自動分配, 而且不要去釋放它.
        失敗: NULL
 
    例:
        time_t now ;
        struct tm *tm_now ;
        time(&now) ;
        tm_now = localtime(&now) ;//把秒變換成年月日
        printf("now datetime: %d-%d-%d %d:%d:%d\n", tm_now->tm_year+1900, tm_now->tm_mon+1, tm_now->tm_mday, tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec) ;


3.  localtime_r 也是用來獲取系統時間,運行於linux平臺下

    原型爲struct tm *localtime_r(const time_t *timep, struct tm *result);

    #include <stdio.h>
    #include <time.h>
     
    int main()
    {
        time_t time_seconds = time(0);
        struct tm now_time;
        localtime_r(&time_seconds, &now_time);
     
        printf("%d-%d-%d %d:%d:%d\n", now_time.tm_year + 1900, now_time.tm_mon + 1,
            now_time.tm_mday, now_time.tm_hour, now_time.tm_min, now_time.tm_sec);
    }
    
    
localtime、localtime_r 二者區別:
    localtime對於多線程不安全,因爲 localtime在使用時,只需定義一個指針,申請空間的動作由函數自己完成,這樣在多線程的情況下,如果有另一個線程調用了這個函數,那麼指針指向的struct tm結構體的數據就會改變。

    在localtime_s與localtime_r調用時,定義的是struct tm的結構體,獲取到的時間已經保存在struct tm中,並不會受其他線程的影響。

 

4.  localtime_s 也是用來獲取系統時間,運行於windows平臺下,與localtime_r只有參數順序不一樣

    #include <iostream>
    #include <time.h>
     
    int main()
    {
        time_t time_seconds = time(0);
        struct tm now_time;
        localtime_s(&now_time,&time_seconds);
     
        printf("%d-%d-%d %d:%d:%d\n", now_time.tm_year + 1900, now_time.tm_mon + 1,
            now_time.tm_mday, now_time.tm_hour, now_time.tm_min, now_time.tm_sec);
    }


5.  gettimeofday 獲取 秒,微妙,時區等信息.

    #include <sys/time.h>

    int gettimeofday(struct timeval*tv, struct timezone *tz); //其參數tv是保存獲取時間結果的結構體,參數tz用於保存時區結果,timezone 參數若不使用則傳入NULL即可。

    struct timezone{

        int tz_minuteswest;/*格林威治時間往西方的時差*/

        int tz_dsttime;/*DST 時間的修正方式*/
    }
        
    struct timeval{

        long int tv_sec; // 秒數

        long int tv_usec; // 微秒數

    }
    
    例:
    它獲得的時間精確到微秒(1e-6 s)量級。在一段代碼前後分別使用gettimeofday可以計算代碼執行時間:
    struct timeval tv_begin, tv_end;

    gettimeofday(&tv_begin, NULL);

    foo();

    gettimeofday(&tv_end, NULL);

    函數執行成功後返回0,失敗後返回-1,錯誤代碼存於errno中

 

 

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