C語言操作時間函數time.ctime,實現定時執行某個任務小例子


時間操作函數在實際項目開發中會經常用到,最近做項目也正好用到就正好順便整理一下。

時間概述

在這里插入圖片描述
由上圖可知:

  1. 通過系統調用函數time()可以從內核獲得一個類型為time_t的1個值,該值叫calendar時間,即從1970年1月1日的UTC時間從0時0分0妙算起到現在所經過的秒數。而該時間也用於紀念UNIX的誕生。
  2. 函數gmtime()、localtime()可以將calendar時間轉變成struct tm結構體類型變量中。通過該結構體成員可以很方便的得到當前的時間信息。
    我們也可以通過函數mktime將該類型結構體的變量轉變成calendar時間。
struct tm{
	int tm_sec;/*秒數*/
	int tm_min; /*分鍾*/
	int tm_hour;/*小時*/
	int tm_mday;/*日期*/
	int tm_mon; /*月份*/
	int tm_year; /*從1990年算起至今的年數*/
	int tm_wday; /*星期*/
	int tm_yday; /*從今年1月1日算起至今的天數*/
	int tm_isdst; /*日光節約時間的旗標*/
};
  1. asctime()和ctime()函數產生形式的26字節字符串,這與date命令的系統默認輸出形式類似:
    Tue Feb 10 18:27:38 2020/n/0.
  2. strftime()將一個struct tm結構格式化為一個字符串。

常用時間函數及舉例

1、time函數

頭文件:time.h
函數定義:time_t time (time_t *t)
說明:
	返回從1970年1月1日的UTC時間從0時0分0妙算起到現在所經過的秒數。

舉例如下:

#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;
}

輸出:
在這里插入圖片描述有興趣的同學可以計算下,從1970年1月1日0時0分0秒到現在經歷了多少秒。

附:time_t 一路追蹤發現就是從long類型經過不斷的typedef ,#define定義過來的。

2、ctime函數

定義:char *ctime(const time_t *timep);
說明:將參數所指的time_t結構中的信息轉換成真實世界的時間日期表示方法,然后將結果以字符串形式返回。
注意這個是本地時間。

舉例如下:

#include <stdio.h>
#include<time.h>
int main(void) {
	time_t timep;
	
	time(&timep);
	printf("%s\n",ctime(&timep));
	return 0;
}

輸出:
在這里插入圖片描述

3、gmtime函數

定義:struct tm *gmtime(const time_t *timep);
說明:將參數timep所指的time_t結構中的信息轉換成真實世界所使用的時間日期表示方法,然后將結果由結構tm返回。此函數返回的時間日期未經時區轉換,而是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函數

#include <time.h> 
定義:  
size_t strftime(char *s, size_t max, const char *format,const struct tm *tm);
說明:
類似於snprintf函數,我們可以根據format指向的格式字符串,將struct tm結構體中信息輸出到s指針指向的字符串中,最多為max個字節。當然s指針指向的地址需提前分配空間,比如字符數組或者malloc開辟的堆空間。
其中,格式化字符串各種日期和時間的詳細的確切表示方法有如下多種,我們可以根據需要來格式化各種各樣的含時間字符串。
    %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 時區名稱,如果不能得到時區名稱則返回空字符。
    %% 百分號
返回值:
成功的話返回格式化之后s字符串的字節數,不包括null終止字符,但是返回的字符串包括null字節終止字符。否則返回0,s字符串的內容是未定義的。值得注意的是,這是libc4.4.4以后版本開始的。對於一些的老的libc庫,比如4.4.1,如果給定的max較小的話,則返回max值。即返回字符串所能容納的最大字節數。

舉例如下:

  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 }

執行結果如下:
在這里插入圖片描述
輸出結果表示YYYYmmDDHHMMSS

5、 asctime函數

定義:
char *asctime(const struct tm *timeptr);
說明:
	將參數timeptr所指的struct tm結構中的信息轉換成真實時間所使用的時間日期表示方法,結果以字符串形態返回。與ctime()函數不同之處在於傳入的參數是不同的結構。
返回值:
	返回的也是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函數

struct tm *localhost(const time_t *timep);
取得當地目前的時間和日期

舉例如下:

#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函數

定義:time_t mktime(struct tm *timeptr);
說明:
	用來將參數timeptr所指的tm結構數據轉換成從1970年1月1日的UTC時間從0時0分0妙算起到現在所經過的秒數。

舉例如下:

#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函數

定義:
int gettimeofday(struct timeval *tv,struct timezone *tz);
說明:
	把目前的時間由tv所指的結構返回,當地時區信息則放到有tz所指的結構中,

結構體timeval 定義如下:

struct timeval{
	long tv_sec; /*秒*/
	long tv_usec; /*微秒*/
};

結構體timezone定義如下:

struct timezone{
	int tz_minuteswest; /*和greenwich時間差了多少分鍾*/
	int tz_dsttime; /*日光節約時間的狀態*/
}

舉例如下:

#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;
}

輸出:
在這里插入圖片描述

綜合實驗

現在我們利用這些時間函數,來實現一個定時執行某個任務得功能。

功能

  1. 程序運行時要記錄當前日志文件的最后修改時間;
  2. 每個10秒鍾就檢查下log文件是否被修改,如果沒有被修改就休眠10秒鍾;
  3. 如果log文件被修改了,就將當前的日志文件拷貝成備份文件,備份文件名字加上當前時間;
  4. 通過curl發送給ftp服務器;
  5. 刪除備份文件,重復步驟2。

程序流程圖如下:

在這里插入圖片描述

函數功能介紹

init()

首先記錄當前log文件時間,並記錄到全局變量last_mtime中。

check_file_change()
讀取文件最后修改時間,並和last_mtime進行比較,如果相同就返回0,不同就返回1.

file_name_add_time()
將當前的日志文件拷貝成備份文件,備份文件名字加上當前時間。

stat()​

得到對應文件的屬性信息,存放到struct stat結構體變量中。

運行截圖:

第一步:
在這里插入圖片描述
因為log文件沒有被修改過,所以程序不會上傳。

第二步:
手動輸入字符串 yikoulinux 到日志文件 t.log中。
在這里插入圖片描述第三步:
因為文件發生了改變,所以打印“file updated”,同時可以看到curl上傳文件的log信息。
在這里插入圖片描述以下是FTP服務器的根目錄,可以看到,上傳的日志文件:t-2020-7-26-1-19-45.log
在這里插入圖片描述

【補充】

  1. 配置信息,直接在代碼中寫死,通常應該從配置文件中讀取,為方便讀者閱讀,本代碼沒有增加該功能;
  2. FTP服務器搭建,本文沒有說明,相關文件比較多,大家可以自行搜索,一口君用的是File zilla;
    在這里插入圖片描述
  3. 通常這種需要長時間運行的程序,需要設置成守護進程,本文沒有添加相應功能,讀者可以自行搜索。如果強烈要求可以單開一篇詳細介紹。
  4. 代碼中time的管理函數,請讀者自行搜索相關文章。
  5. curl也提供了相關的函數庫curl.lib,如果要實現更靈活的功能可以使用對應的api。
  6. 之所以先把文件拷貝成備份文件,主要是考慮其他模塊隨時可能修改日志文件,起到一定保護作用。

代碼如下

代碼如下:

/***************************************************
           Copyright (C)  公眾號: 一口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;

/*用戶名密碼暫時寫死,實際應該保存在配置文件*/
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)
{
	//准備結構體
	ST status;
	
	//調用stat函數
	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)
{
	//准備結構體
	ST status;
	
	//調用stat函數
	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);	
	}
}

請關注公眾號「一口Linux」


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM