|
https://m.toutiao.com/is/JcccKk6/ 時間操作函數(shù)在實際項目開發(fā)中會經(jīng)常用到,最近做項目也正好用到就正好順便整理一下。 時間概述由上圖可知:
struct tm{ int tm_sec;/*秒數(shù)*/ int tm_min; /*分鐘*/ int tm_hour;/*小時*/ int tm_mday;/*日期*/ int tm_mon; /*月份*/ int tm_year; /*從1990年算起至今的年數(shù)*/ int tm_wday; /*星期*/ int tm_yday; /*從今年1月1日算起至今的天數(shù)*/ int tm_isdst; /*日光節(jié)約時間的旗標(biāo)*/};
常用時間函數(shù)及舉例1、time函數(shù)頭文件:time.h函數(shù)定義:time_t time (time_t *t)說明: 返回從1970年1月1日的UTC時間從0時0分0妙算起到現(xiàn)在所經(jīng)過的秒數(shù)。 舉例如下: #include<stdio.h>#include<time.h>int main(){ time_t timep; long seconds = time(&timep); printf('%ld\n',seconds); printf('%ld\n',timep); return 0;}輸出: 有興趣的同學(xué)可以計算下,從1970年1月1日0時0分0秒到現(xiàn)在經(jīng)歷了多少秒。 附:time_t 一路追蹤發(fā)現(xiàn)就是從long類型經(jīng)過不斷的typedef ,#define定義過來的。 2、ctime函數(shù)定義:char *ctime(const time_t *timep);說明:將參數(shù)所指的time_t結(jié)構(gòu)中的信息轉(zhuǎn)換成真實世界的時間日期表示方法,然后將結(jié)果以字符串形式返回。注意這個是本地時間。 舉例如下: #include <stdio.h>#include<time.h>int main(void) { time_t timep; time(&timep); printf('%s\n',ctime(&timep)); return 0;}輸出: 3、gmtime函數(shù)定義:struct tm *gmtime(const time_t *timep);說明:將參數(shù)timep所指的time_t結(jié)構(gòu)中的信息轉(zhuǎn)換成真實世界所使用的時間日期表示方法,然后將結(jié)果由結(jié)構(gòu)tm返回。此函數(shù)返回的時間日期未經(jīng)時區(qū)轉(zhuǎn)換,而是UTC時間。 舉例如下: #include <stdio.h>#include<time.h> int main(void) { char *wday[] = {'Sun','Mon','Tue','Wed','Thu','Fri','Sat'}; time_t timep; struct tm *p; time(&timep); p = gmtime(&timep); printf('%d/%d/%d ',(1900+p->tm_year),(1+p->tm_mon),p->tm_mday); printf('%s %d:%d:%d\n',wday[p->tm_wday],p->tm_hour,p->tm_min,p->tm_sec); return 0;}輸出: 4、 strftime函數(shù)#include <time.h> 定義: size_t strftime(char *s, size_t max, const char *format,const struct tm *tm);說明:類似于snprintf函數(shù),我們可以根據(jù)format指向的格式字符串,將struct tm結(jié)構(gòu)體中信息輸出到s指針指向的字符串中,最多為max個字節(jié)。當(dāng)然s指針指向的地址需提前分配空間,比如字符數(shù)組或者malloc開辟的堆空間。其中,格式化字符串各種日期和時間的詳細(xì)的確切表示方法有如下多種,我們可以根據(jù)需要來格式化各種各樣的含時間字符串。 %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ì)部分的十制年份 %z,%Z 時區(qū)名稱,如果不能得到時區(qū)名稱則返回空字符。 %% 百分號返回值:成功的話返回格式化之后s字符串的字節(jié)數(shù),不包括null終止字符,但是返回的字符串包括null字節(jié)終止字符。否則返回0,s字符串的內(nèi)容是未定義的。值得注意的是,這是libc4.4.4以后版本開始的。對于一些的老的libc庫,比如4.4.1,如果給定的max較小的話,則返回max值。即返回字符串所能容納的最大字節(jié)數(shù)。 舉例如下: 1 #include <stdio.h> 2 #include <time.h> 3 4 #define BUFLEN 255 5 int main(int argc, char **argv) 6 { 7 time_t t = time( 0 ); 8 char tmpBuf[BUFLEN]; 9 10 strftime(tmpBuf, BUFLEN, '%Y%m%d%H%M%S', localtime(&t)); //format date a 11 printf('%s\n',tmpBuf); 12 return 0; 13 }執(zhí)行結(jié)果如下: 輸出結(jié)果表示YYYYmmDDHHMMSS 5、 asctime函數(shù)定義:char *asctime(const struct tm *timeptr);說明: 將參數(shù)timeptr所指的struct tm結(jié)構(gòu)中的信息轉(zhuǎn)換成真實時間所使用的時間日期表示方法,結(jié)果以字符串形態(tài)返回。與ctime()函數(shù)不同之處在于傳入的參數(shù)是不同的結(jié)構(gòu)。返回值: 返回的也是UTC時間。 舉例如下: #include <stdio.h>#include <stdlib.h>#include<time.h>int main(void) { time_t timep; time(&timep); printf('%s\n',asctime(gmtime(&timep))); return EXIT_SUCCESS;}輸出: 6、 localhost函數(shù)struct tm *localhost(const time_t *timep);取得當(dāng)?shù)啬壳暗臅r間和日期 舉例如下: #include <stdio.h>#include <stdlib.h>#include<time.h> int main(void) { char *wday[] = {'Sun','Mon','Tue','Wed','Thu','Fri','Sat'}; time_t timep; struct tm *p; time(&timep); p = localtime(&timep); printf('%d/%d/%d ',(1900+p->tm_year),(1+p->tm_mon),p->tm_mday); printf('%s %d:%d:%d\n',wday[p->tm_wday],p->tm_hour,p->tm_min,p->tm_sec); return EXIT_SUCCESS;}輸出: 7、mktime函數(shù)定義:time_t mktime(struct tm *timeptr);說明: 用來將參數(shù)timeptr所指的tm結(jié)構(gòu)數(shù)據(jù)轉(zhuǎn)換成從1970年1月1日的UTC時間從0時0分0妙算起到現(xiàn)在所經(jīng)過的秒數(shù)。 舉例如下: #include <stdio.h>#include <stdlib.h>#include<time.h> int main(void) { time_t timep; struct tm *p; time(&timep); printf('time():%ld\n',timep); p = localtime(&timep); timep = mktime(p); printf('time()->localtime()->mktime():%ld\n',timep); return EXIT_SUCCESS;}輸出: 8、 gettimeofday函數(shù)定義:int gettimeofday(struct timeval *tv,struct timezone *tz);說明: 把目前的時間由tv所指的結(jié)構(gòu)返回,當(dāng)?shù)貢r區(qū)信息則放到有tz所指的結(jié)構(gòu)中, 結(jié)構(gòu)體timeval 定義如下: struct timeval{ long tv_sec; /*秒*/ long tv_usec; /*微秒*/};結(jié)構(gòu)體timezone定義如下: struct timezone{ int tz_minuteswest; /*和greenwich時間差了多少分鐘*/ int tz_dsttime; /*日光節(jié)約時間的狀態(tài)*/}舉例如下: #include <stdio.h>#include <stdlib.h>#include<time.h>#include<sys/time.h> int main(void) {struct timeval tv;struct timezone tz;gettimeofday(&tv,&tz);printf('tv_sec :%d\n',tv.tv_sec);printf('tv_usec: %d\n',tv.tv_usec);printf('tz_minuteswest:%d\n',tz.tz_minuteswest);printf('tz_dsttime:%d\n',tz.tz_dsttime);return EXIT_SUCCESS;}輸出: 綜合實驗現(xiàn)在我們利用這些時間函數(shù),來實現(xiàn)一個定時執(zhí)行某個任務(wù)得功能。 功能
程序流程圖如下:在這里插入圖片描述 函數(shù)功能介紹init() 首先記錄當(dāng)前l(fā)og文件時間,并記錄到全局變量last_mtime中。 check_file_change() 讀取文件最后修改時間,并和last_mtime進(jìn)行比較,如果相同就返回0,不同就返回1. file_name_add_time() 將當(dāng)前的日志文件拷貝成備份文件,備份文件名字加上當(dāng)前時間。 stat() 得到對應(yīng)文件的屬性信息,存放到struct stat結(jié)構(gòu)體變量中。 運行截圖:第一步: ![]() 因為log文件沒有被修改過,所以程序不會上傳。 第二步: 手動輸入字符串 yikoulinux 到日志文件 t.log中。 ![]() 第三步: 因為文件發(fā)生了改變,所以打印“file updated”,同時可以看到curl上傳文件的log信息。 ![]() 以下是FTP服務(wù)器的根目錄,可以看到,上傳的日志文件:t-2020-7-26-1-19-45.log。 ![]() 【補(bǔ)充】
代碼如下 代碼如下:/*************************************************** linux ***************************************************/#include <sys/stat.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <time.h> typedef struct stat ST;unsigned long last_mtime;/*用戶名密碼暫時寫死,實際應(yīng)該保存在配置文件*/char name[32]='user';char pass[32] ='123456';char ip[32] ='192.168.43.117';char filename[32]='t.log';char dstfile[256] ={0};int init(void){ //準(zhǔn)備結(jié)構(gòu)體 ST status; //調(diào)用stat函數(shù) int res = stat(filename,&status); if(-1 == res) { perror('error:open file fail\n'); return 0; } last_mtime = status.st_mtime; printf('init time:%s \n',ctime(&last_mtime)); return 1;} int check_file_change(void){ //準(zhǔn)備結(jié)構(gòu)體 ST status; //調(diào)用stat函數(shù) int res = stat(filename,&status); if(-1 == res) { perror('error:open file fail\n'); return 0; }// printf('old:%s new:%s',ctime(&last_mtime),ctime(&status.st_mtime)); if(last_mtime == status.st_mtime) { printf('file not change\n'); return 0; }else{ printf('file updated\n'); last_mtime = status.st_mtime; return 1; }}void file_name_add_time(void){ ST status; time_t t; struct tm *tblock; char cmd[1024]={0}; t = time(NULL); tblock = localtime(&t); sprintf(dstfile,'t-%d-%d-%d-%d-%d-%d.log', tblock->tm_year+1900, tblock->tm_mon, tblock->tm_mday, tblock->tm_hour, tblock->tm_min, tblock->tm_sec); sprintf(cmd,'cp %s %s',filename,dstfile);// printf('cdm=%s\n',cmd); system(cmd);}int main(void){ char cmd[1024]={0}; init(); while(1) { if(check_file_change() == 1) { file_name_add_time(); sprintf(cmd,'curl -u %s:%s ftp://%s/ -T %s',name,pass,ip,dstfile); // printf('cdm=%s\n',cmd); system(cmd); unlink(dstfile); } sleep(10); }}
|
|
|