網站首頁 編程語言 正文
概述
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
相關推薦
- 2023-01-23 oracle數據排序后獲取前幾行數據的寫法(rownum、fetch方式)_oracle
- 2023-04-09 C語言中的分支循環其嵌套語句_C 語言
- 2022-10-16 python正則表達式re.group()用法_python
- 2022-04-03 Nginx構建Tomcat集群的操作方法_nginx
- 2022-09-10 詳解Go?中的時間處理_Golang
- 2023-02-09 Linux命令行循環執行shell命令_linux shell
- 2022-12-09 C++筆記-設置cout輸出數據的寬度和填充方式_C 語言
- 2022-11-19 Python+random模塊實現隨機抽樣_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支