日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C/C++實現獲取系統時間的示例代碼_C 語言

作者:iBlackAngel ? 更新時間: 2023-01-20 編程語言

概述

C 標準庫提供了 time() 函數與 localtime() 函數可以獲取到當前系統的日歷時間,但 time() 函數精度只能到秒級,如果需要更高精度的系統時間需要使用 gettimeofday() 函數,精度達到微秒級。

#include <sys/time.h>

int gettimeofday(struct timeval *tv, struct timezone *tz);

tv 參數是一個 struct timeval 結構體(同樣是在 <sys/time.h> 頭文件中定義):

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

時區結構體 struct timezone 的使用已過時,tz 參數通常應指定為 NULL。

函數 localtime() 把 timep 指向的日歷時間轉換為表示本地時間的細分時間。

#include <time.h>

???????struct tm *localtime(const time_t *timep);

localtime() 返回一個指向 struct tm 對象的指針,它保存了一個日歷時間的各組成部分,日歷時間也被稱為細分時間(Broken-down time)。該結構體定義在 <time.h> 頭文件中:

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 */
};

tm 結構體的成員:

tm_sec —— 分鐘后的秒數,通常在 59 秒的范圍內,但最多可以達到 60 秒,以允許閏秒。

tm_min —— 小時后的分鐘數,范圍為 0 到 59。

tm_hour —— 午夜過后的小時數,范圍為 0 到 23。

tm_mday —— 一個月的某一天,范圍為 1 到 31。

tm_mon —— 自 1 月以來的月份數,范圍為 0 到 11(顯示月份的時候需要加 1)

tm_year —— 自 1900 年以來的年數(顯示年份的時候需要加上 1900)

tm_wday —— 自周日(星期日)以來的天數,范圍為 0 到 6。

tm_yday —— 自 1 月 1 日以來的天數,范圍為 0 到 365。

tm_isdst —— 指示夏時制在所述時間是否有效的標志。如果夏令時有效,則該值為正值;如果夏令時無效,則為零;如果信息不可用,則為負值。

示例

#include <stdio.h>          // included for 'printf()'
#include <sys/time.h>       // included for 'gettimeofday()'
#include <time.h>           // included for 'localtime()'

int main(int argc, char const *argv[]) {
    struct timeval tv;
    gettimeofday(&tv, NULL);

    time_t sec = tv.tv_sec;
    suseconds_t usec = tv.tv_usec;

    struct tm *lt = localtime(&sec);

    printf("%d-%02d-%02d %02d:%02d:%02d.%03d\n", 
            lt->tm_year + 1900,
            lt->tm_mon + 1,
            lt->tm_mday,
            lt->tm_hour,
            lt->tm_min,
            lt->tm_sec,
            (int)(usec / 1000));
    return 0;
}

使用 gettimeofday 獲取到保存在 timeval 結構體的時間之后,通過 localtime 函數將 tv_sec 轉換成 struct tm 結構體,在關鍵的 tm_year, tm_mon 進行特殊處理計算出當前到秒的日歷時間,然后通過將 tv_usec 微秒數除以 1000 得到毫秒數。

在命令行使用 gcc 編譯:

gcc -o main main.c

結果為帶毫秒數的當前日歷時間:

$ ./main
2022-12-15 11:03:56.847

易用性封裝

如果需要直接在代碼中獲取當前帶毫秒數的日歷時間,可以參考以下封裝接口:

使用 C++11 標準的 thread_local 創建一個線程安全的全局變量,然后將運算結果存儲在全局變量中,最后返回對象的指針,這樣既能保證調用函數的易用性,同時也能兼顧運算性能,這種寫法可以非常簡單地應用到大部分應用中:

thread_local char __timebuf[64] = {0x00};

const char *curtime() {
    struct timeval tv;
    gettimeofday(&tv, NULL);
    struct tm *lt = localtime(&tv.tv_sec);
    snprintf(__timebuf, sizeof(__timebuf) - 1,
            "%d-%02d-%02d %02d:%02d:%02d.%03d", 
            lt->tm_year + 1900,
            lt->tm_mon + 1,
            lt->tm_mday,
            lt->tm_hour,
            lt->tm_min,
            lt->tm_sec,
            (int)(tv.tv_usec / 1000));    
    return __timebuf;
}

原文鏈接:https://blog.csdn.net/bluebird_shao/article/details/128328631

欄目分類
最近更新