- (??? ),hello我是祐言
- 博客主頁:C語言基礎(chǔ),Linux基礎(chǔ),軟件配置領(lǐng)域博主??
- 快上??,一起學(xué)習(xí)!
- 送給讀者的一句雞湯??:
- 集中起來的意志可以擊穿頑石!
- 作者水平很有限,如果發(fā)現(xiàn)錯誤,可在評論區(qū)指正,感謝??
????????編程中經(jīng)常用到時間表達(dá)及轉(zhuǎn)換的函數(shù),它們都定義在 time.h 庫函數(shù)中,在此做一下總結(jié),以方便后續(xù)查看使用。
一、幾個時間概念:
1.Coordinated Universal Time(UTC)
????????協(xié)調(diào)世界時,又稱世界標(biāo)準(zhǔn)時間,也即格林威治標(biāo)準(zhǔn)時間(Greenwich Mean Time,GMT),中國內(nèi)地的時間與UTC得時差為+8,也即UTC+8,美國為UTC-5。
2.Calendar Time
????????日歷時間,是用"從一個標(biāo)準(zhǔn)時間點(diǎn)到此時的時間經(jīng)過的秒數(shù)"來表示的時間。標(biāo)準(zhǔn)時間點(diǎn)對不同編譯器可能會不同,但對一個編譯系統(tǒng)來說,標(biāo)準(zhǔn)時間是不變的。一般是表示距離UTC時間 1970-01-01 00:00:00的秒數(shù)。
3.epoch
????????時間點(diǎn)。在標(biāo)準(zhǔn)c/c++中是一個整數(shù),用此時的時間和標(biāo)準(zhǔn)時間點(diǎn)相差的秒數(shù)(即日歷時間)來表示。
4.clock tick
????????時鐘計時單元(而不叫做時鐘滴答次數(shù)),一個時鐘計時單元的時間長短是由cpu控制的,一個clock tick不是cpu的一個時鐘周期,而是c/c++的一個基本計時單位。
二、time.h 的定義
????????time.h 頭文件聲明了四個變量類型、兩個宏和各種操作日期和時間的函數(shù)。
其中4個變量:
size_t | 是無符號整數(shù)類型,它是 sizeof 關(guān)鍵字的結(jié)果。 |
---|---|
clock_t | 這是一個適合存儲處理器時間的類型,類型為unsigned long |
time_t | 這是一個適合存儲日歷時間類型。 |
struct tm | 這是一個用來保存時間和日期的結(jié)構(gòu)。 |
?????????tm 結(jié)構(gòu)體定義如下:
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 到 11(注意) \*/*
int tm_year; ? ? */\* 自 1900 年起的年數(shù) ? \*/*
int tm_wday; ? ? */\* 一周中的第幾天,范圍從 0 到 6 \*/*
int tm_yday; ? ? */\* 一年中的第幾天,范圍從 0 到 365 \*/*
int tm_isdst; ? */\* 夏令時 ? ? ? \*/*
};
兩個宏:
NULL | 這個宏是一個空指針常量的值。 |
---|---|
CLOCKS_PER_SEC | 這個宏表示每秒的處理器時鐘個數(shù)。用于將clock()函數(shù)的結(jié)果轉(zhuǎn)化為以秒為單位的量,這個量的具體值是與操作系統(tǒng)相關(guān)的,通常為1000。 |
三、time庫函數(shù)
1.clock函數(shù)
函數(shù)原型: clock_t clock(void)
函數(shù)返回:返回clock函數(shù)執(zhí)行起(一般為程序的開頭),處理器時鐘所使用的時間。
函數(shù)功能:用來計算程序或程序的某一段的執(zhí)行時間。
實例:
#include<stdio.h>
#include<time.h>
?
int main()
{
?clock_t start_t,finish_t;
?double total_t = 0;
?int i = 0;
?start_t = clock();
?for(;i<100000;++i)
{
? ?*//do someting;*
}
?finish_t = clock();
?total_t = (double)(finish_t - start_t) / CLOCKS_PER_SEC;*//將時間轉(zhuǎn)換為秒*
?
?printf("CPU 占用的總時間:%f**\n**", total_t);
?return 0;
}
2.time函數(shù)
函數(shù)原型: time_t time(time_t *timer)
參數(shù)說明: timer=NULL時得到當(dāng)前日歷時間(從1970-01-01 00:00:00到現(xiàn)在的秒數(shù)),timer=時間數(shù)值時,用于設(shè)置日歷時間,time_t是一個unsigned long類型。如果 timer不為空,則返回值也存儲在變量 timer中。
函數(shù)功能: 得到當(dāng)前日歷時間或者設(shè)置日歷時間
函數(shù)返回: 當(dāng)前日歷時間
實例:
#include <stdio.h>
#include <time.h>
?
int main ()
{
time_t seconds;
?
seconds = time(NULL);
printf("自 1970-01-01 起的小時數(shù) = %ld**\n**", seconds/3600);
?
return(0);
}
3.asctime函數(shù)
函數(shù)原型: char* asctime(struct tm * ptr)
函數(shù)功能:將結(jié)構(gòu)struct tm * ptr所表示的時間以字符串表示
函數(shù)返回: 返回的時間字符串格式為:星期,月,日,小時:分:秒,年
參數(shù)說明: 結(jié)構(gòu)指針ptr應(yīng)通過函數(shù)localtime()或gmtime()得到
實例:
#include <stdio.h>
#include <time.h>
?
int main()
{
?struct tm t;*//更多情況下是通過localtime函數(shù)及gmtime函數(shù)獲得tm結(jié)構(gòu)*
?
?t.tm_sec ? = 10;
?t.tm_min ? = 10;
?t.tm_hour ?= 6;
?t.tm_mday ?= 25;
?t.tm_mon ? = 2;
?t.tm_year ?= 89;
?t.tm_wday ?= 6;
?
?printf("%s**\n**",asctime(&t));
?
?return(0);
}
4.localtime函數(shù)
函數(shù)原型: struct tm *localtime(const time_t *timer)
函數(shù)功能: 使用 timer 的值來填充 tm 結(jié)構(gòu)。timer 的值被分解為 tm 結(jié)構(gòu),并用本地時區(qū)表示。
函數(shù)返回: 以tm結(jié)構(gòu)表達(dá)的時間
實例:
#include <stdio.h>
#include <time.h>
?
int main ()
{
?time_t timer;
?struct tm *Now;
?
?time( &timer );
?Now = localtime( &timer );
?printf("當(dāng)前的本地時間和日期:%s", asctime(Now));
?
?return(0);
}
5.ctime函數(shù)
函數(shù)原型: char *ctime(const time_t * timer)
函數(shù)功能: 將日歷時間參數(shù)timer轉(zhuǎn)換為一個表示本地當(dāng)前時間的字符串
函數(shù)返回: 返回字符串格式:星期,月,日,小時:分:秒,年
參數(shù)說明: timer參數(shù)應(yīng)由函數(shù)time獲得,其等價于 astime( localtime(timer) )
實例:
#include <stdio.h>
#include <time.h>
?
int main ()
{
?time_t curtime;
?time(&curtime);
?printf("當(dāng)前時間 = %s", ctime(&curtime));
?
?return(0);
}
6.difftime函數(shù)
函數(shù)原型: double difftime(time_t time2, time_t time1)
函數(shù)功能: 得到兩次機(jī)器時間差,單位為秒
函數(shù)返回: 時間差,單位為秒
參數(shù)說明: time1,time2分別表示兩個不同的機(jī)器時間,該參數(shù)應(yīng)使用time函數(shù)獲得
實例:
#include <time.h> ?
#include <stdio.h> ?
int main()
{
?time_t first,second;
?time(&first);
?sleep(2000);
?time(&second);
?printf("The difference is: %f seconds",difftime(second,first));
?
?return 0;
}
7.gmtime函數(shù)
函數(shù)原型: struct tm *gmtime(time_t *timer)
函數(shù)功能: 得到以結(jié)構(gòu)tm表示的時間信息,并用格林威治標(biāo)準(zhǔn)時間表示
函數(shù)返回: 以結(jié)構(gòu)tm表示的時間信息指針
參數(shù)說明: timer用函數(shù)time()得到的時間信息
實例:
#include <stdio.h>
#include <time.h>
?
#define BST (+1)
#define CCT (+8)
?
int main ()
{
?
?time_t rawtime;
?struct tm *info;
?
?time(&rawtime);
?*/\* 獲取 GMT 時間 \*/*
?info = gmtime(&rawtime );
?
?printf("當(dāng)前的世界時鐘:**\n**");
?printf("倫敦:%2d:%02d**\n**", (info->tm_hour+BST)%24, info->tm_min);
?printf("中國:%2d:%02d**\n**", (info->tm_hour+CCT)%24, info->tm_min);
?
?return(0);
}
8.mktime函數(shù)
函數(shù)原型:time_t mktime(struct tm *timeptr)
函數(shù)功能:把 timeptr 所指向的結(jié)構(gòu)轉(zhuǎn)換為一個依據(jù)本地時區(qū)的 time_t 值
函數(shù)返回:該函數(shù)返回一個 time_t 值,該值對應(yīng)于以參數(shù)傳遞的日歷時間。如果發(fā)生錯誤,則返回 -1 值。
實例:
#include <stdio.h>
#include <time.h>
?
int main ()
{
?int ret;
?struct tm info;
?char buffer[80];
?
?info.tm_year = 2001 - 1900;
?info.tm_mon = 7 - 1;
?info.tm_mday = 4;
?info.tm_hour = 0;
?info.tm_min = 0;
?info.tm_sec = 1;
?info.tm_isdst = -1;
?
?ret = mktime(&info);
?if( ret == -1 )
{
? ?printf("錯誤:不能使用 mktime 轉(zhuǎn)換時間。**\n**");
}
?else
{
? strftime(buffer, sizeof(buffer), "%c", &info );
? print(buffer);
}
?
?return(0);
}
9.strftime函數(shù)
函數(shù)原型: size_t strftime( char *strDest, size_t maxsize, const char *format, const struct tm *timeptr);
函數(shù)功能: 根據(jù)format指向字符串中格式命令把timeptr中保存的時間信息放在strDest指向的字符串中,最多向 strDest中存放maxsize個字符。
參數(shù)說明: 轉(zhuǎn)化結(jié)果存在s中,最多maxsize個字符寫到s中
函數(shù)返回: 該函數(shù)返回向strDest指向的字符串中放置的字符數(shù)(不包括'\0'),如果字符數(shù)多于maxsize,函數(shù)返回0。
實例:
/*format如下:它們是區(qū)分大小寫的。
?%a 星期幾的簡寫
?%A 星期幾的全稱
?%b 月分的簡寫
?%B 月份的全稱
?%c 標(biāo)準(zhǔn)的日期的時間串
?%C 年份的后兩位數(shù)字
?%d 十進(jìn)制表示的每月的第幾天
?%D 月/天/年
?%e 在兩字符域中,十進(jìn)制表示的每月的第幾天
?%F 年-月-日
?%g 年份的后兩位數(shù)字,使用基于周的年
?%G 年分,使用基于周的年
?%h 簡寫的月份名
?%H 24小時制的小時
?%I 12小時制的小時
?%j 十進(jìn)制表示的每年的第幾天
?%m 十進(jìn)制表示的月份
?%M 十時制表示的分鐘數(shù)
?%n 新行符
?%p 本地的AM或PM的等價顯示
?%r 12小時的時間
?%R 顯示小時和分鐘:hh:mm
?%S 十進(jìn)制的秒數(shù)
?%t 水平制表符
?%T 顯示時分秒:hh:mm:ss
?%u 每周的第幾天,星期一為第一天 (值從0到6,星期一為0)
?%U 第年的第幾周,把星期日做為第一天(值從0到53)
?%V 每年的第幾周,使用基于周的年
?%w 十進(jìn)制表示的星期幾(值從0到6,星期天為0)
?%W 每年的第幾周,把星期一做為第一天(值從0到53)
?%x 標(biāo)準(zhǔn)的日期串
?%X 標(biāo)準(zhǔn)的時間串
?%y 不帶世紀(jì)的十進(jìn)制年份(值從0到99)
?%Y 帶世紀(jì)部分的十進(jìn)制年份
?%z,%Z 時區(qū)名稱,如果不能得到時區(qū)名稱則返回空字符。
?%% 百分號
*/
?
#include <stdio.h>
#include <time.h>
?
int main ()
{
?time_t rawtime;
?struct tm *info;
?char buffer[80];
?
?time( &rawtime );
?
?info = localtime( &rawtime );
?
?strftime(buffer,80,"%Y%m%e_%H%M%S", info);*//以年月日_時分秒的形式表示當(dāng)前時間*
?printf("%s**\n**", buffer );
?
?return(0);
}
????????最后再補(bǔ)充一個比較綜合一點(diǎn)的例子:求給定時間和現(xiàn)在的系統(tǒng)時間的差。
#include <stdio.h>
#include <time.h>
void formatTime(int seconds, int *hours, int *minutes, int *seconds_remain) {
seconds %= 3600;
*hours = seconds / 3600;
*minutes = seconds / 60;
*seconds_remain = seconds % 60;
}
int main()
{
int ret;
struct tm info;
char buffer[80];
//給定時間2023年7月25日10點(diǎn)54分01秒
info.tm_year = 2023 - 1900;
info.tm_mon = 7 - 1;
info.tm_mday = 25;
info.tm_hour = 10;
info.tm_min = 54;
info.tm_sec = 1;
info.tm_isdst = -1;
ret = mktime(&info);
if (ret == -1)
{
printf("錯誤:不能使用 mktime 轉(zhuǎn)換時間。**\n**");
}
else
{
strftime(buffer, sizeof(buffer), "%c", &info);
printf("指定時間:%s\n", buffer);
}
time_t currentTime;
time(¤tTime);
char *timeStr = ctime(¤tTime); // 將time_t時間值轉(zhuǎn)換為字符串格式
printf("當(dāng)前系統(tǒng)時間:%s\n", timeStr);
// 計算時間差值
double diff = difftime(currentTime, ret);
int hours, minutes, seconds_remain;
formatTime((int)diff, &hours, &minutes, &seconds_remain);
printf("時間差值為:%d小時 %d分鐘 %d秒\n", hours, minutes, seconds_remain);
return 0;
}
????????更多C語言和Linux系統(tǒng)相關(guān)文章,關(guān)注專欄:
? ?手撕C語言文章來源:http://www.zghlxwxcb.cn/news/detail-607358.html
? ? ? ? ? ? 玩轉(zhuǎn)linux文章來源地址http://www.zghlxwxcb.cn/news/detail-607358.html
??寫在最后
- 今天的分享就到這啦~
- 覺得博主寫的還不錯的煩勞?
一鍵三連喔
~ - ??感謝關(guān)注??
到了這里,關(guān)于關(guān)于系統(tǒng)時間的一些time庫函數(shù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!