這段時間一直在處理服務器產生的日誌,需要經常獲取時間和格式化輸出,所以對C/C++的時間操作總結和歸納一下。在網上找了很多的blog,大多是寫函數的具體用法,時間存儲結構的含義,並不是不這種介紹方式不好,只是我很難快速從中提取出主要的內容。
時間函數及時間結構之間的關係圖
時間的存儲結構一般有四種,time_t、struct tm、struct timeval、struct timespec。下圖給出了時間存儲結構的精度,及其與時間函數的關係圖,
時間結構及其獲取函數
time_t
time_t 類型表示從1970-01-01 00:00:00時開始到現在秒數,精確度:秒(second)。
time_t 類型定義在頭文件 /usr/include/sys/time.h 中:
#define _TIME_T
typedef long time_t;
#endif
time_t 類型變量獲取函數:time_t time(time_t* lpt):
#include<time.h>
time_t ts;
ts = time(0); // 方法一
ts = time(NULL); // 方法二
ts = time((size_t)NULL); // 方法三
time(&ts); // 方法四
struct tm
struct tm 結構精度爲:秒(second),結構定義如下:
struct tm
{
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; /* 一星期的日數,從星期一算起,範圍爲0-6 */
int tm_wday; /* 從1900 年算起至今的年數 */
int tm_yday; /* 從今年1月1日算起至今的天數,範圍爲0-365 */
int tm_isdst; /* 日光節約時間的旗標 */
};
struct tm類型變量並不能直接獲取,通過解析time_t得到:
#include<time.h>
struct tm *localtime(const time_t *timep); /* 當前時區時間 */
struct tm *gmtime(const time_t *timep); /* GMT時間 */
struct timeb
struct timeb 結構的精確度:毫秒(10E-3秒)。結構定義如下:
struct timeb{
time_t time; /* 爲1970-01-01至今的秒數*/
unsigned short millitm; /* 千分之一秒即毫秒 */
short timezonel; /* 爲目前時區和Greenwich相差的時間,單位爲分鐘 */
short dstflag; /* 爲日光節約時間的修正狀態,如果爲非0代表啓用日光節約時間修正 */
};
struct timeb類型變量的獲取:
#include <sys/timeb.h>
int ftime(struct timeb* tp); // 成功返回0;失敗返回-1
struct timeval
struct timeval 結構精確度爲:微秒(10E-6);結構定義如下:
struct timeval
{
long tv_sec;/* 爲1970-01-01至今的秒數*/
long tv_usec;/* 前一秒結束到現在的毫秒數*/
}
struct timeval類型變量的獲取:
#include <sys/time.h>
int gettimeofday(struct timeval* tv,struct timezone* tz); /* 成功返回0,失敗返回-1,錯誤代碼存於errno */
/* 該函數會提取系統當前時間,把時間分爲秒和微秒分別填充到結構struct timeval中;把當地的時區信息填充到結構struct timezone中,參數tz用於保存時區結果(若不使用則傳入NULL即可)。 */
clock_t
clock_t 類型表示進程佔用CPU的時間(微秒數),精度爲:微秒(10E-6)。
clock_t 定義在頭文件/usr/include/sys/types.h中:
#ifndef _CLOCK_T
#define _CLOCK_T
typedef long clock_t;
#endif
clock_t 類型變量的獲取:
#include <time.h>
clock_t clock(void); /* 以微秒的方式返回進程佔用CPU的時間 */
struct timespec
struct timespec結構精確度爲:納秒(10E-9秒);它是POSIX.4標準定義的一個時間結構;結構定義如下:
struct timespec
{
time_t tv_sec; /* 爲1970-01-01至今的秒數*/
long tv_nsec; /* 前一秒結束到現在的納秒數*/
};
struct timespec結構類型變量的獲取:
<這部內容未確認,待考證>
int clock_gettime(clockid_t, struct timespec *)
/* 獲取特定時鐘的時間,常用如下4種時鐘:
CLOCK_REALTIME 系統當前時間,從1970年1.1日算起
CLOCK_MONOTONIC 系統的啓動時間,不能被設置
CLOCK_PROCESS_CPUTIME_ID 本進程運行時間
CLOCK_THREAD_CPUTIME_ID 本線程運行時間
*/
typedef struct timespec timespec_t; // </usr/include/sys/time_impl.h 中定義>
extern void gethrestime(timespec_t*);
extern void gethrestime_lasttick(timespec_t*);
時間格式轉換
ctime()和asctime()
ctime()和asctime()將不同結構的時間轉換成真實世界所使用的(當前時區)時間日期表示方法,然後將結果以字符串形態返回。
ctime()將time_t類型的時間轉換成字符串;
asctime()將struct tm類型的時間轉換成字符串。
轉換成的時間格式都爲”Wed Jun 30 21 :49 :08 1993/n“
strftime()/strptime()
C/C++提供了一個自定義時間格式的函數strftime(),和一個將自定義時間格式轉化爲struct tm結構的函數strptime():
#include<time.h>
size_t strftime(
char *strDest, /* 存放格式化後的字符串緩存 */
size_t maxsize, /* 最多可以輸出的字符數 */
const char *format, /* 時間輸出格式 */
const struct tm *timeptr /* 待轉換的時間 */)
char *strptime(
const char *buf, /* 待轉換的格式化字符串 */
const char *format, /* 字符串的時間格式 */
struct tm *timeptr /* 轉換後的時間 */)
可使用的格式化字符串:
%a 星期幾的簡寫
%A 星期幾的全稱
%b 月分的簡寫
%B 月份的全稱
%c 標準的日期的時間串
%C 年份的後兩位數字
%d 十進制表示的每月的第幾天
%D 月/天/年
%e 在兩字符域中,十進制表示的每月的第幾天
%F 年-月-日
%g 年份的後兩位數字,使用基於周的年
%G 年分,使用基於周的年
%h 簡寫的月份名
%H 24小時制的小時
%I 12小時制的小時
%j 十進制表示的每年的第幾天
%m 十進制表示的月份
%M 十時製表示的分鐘數
%n 新行符
%p 本地的AM或PM的等價顯示
%r 12小時的時間
%R 顯示小時和分鐘:hh:mm
%S 十進制的秒數
%t 水平製表符
%T 顯示時分秒:hh:mm:ss
%u 每週的第幾天,星期一爲第一天 (值從0到6,星期一爲0)
%U 第年的第幾周,把星期日做爲第一天(值從0到53)
%V 每年的第幾周,使用基於周的年
%w 十進制表示的星期幾(值從0到6,星期天爲0)
%W 每年的第幾周,把星期一做爲第一天(值從0到53)
%x 標準的日期串
%X 標準的時間串
%y 不帶世紀的十進制年份(值從0到99)
%Y 帶世紀部分的十進制年份
%z,%Z 時區名稱,如果不能得到時區名稱則返回空字符。
%% 百分號
時間獲取函數的效率對比
<待加入代碼和效果進行完善>