c语言实现定时显示字符,C语言操作时间函数,实现定时执行某个任务小程序

时间概述

297891d239175559115aae3b474cce4b.png由上图可知:通过系统调用函数time()可以从内核获得一个类型为time_t的1个值,该值叫calendar时间,即从1970年1月1日的UTC时间从0时0分0妙算起到现在所经过的秒数。而该时间也用于纪念UNIX的诞生。

函数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; /*日光节约时间的旗标*/

};asctime()和ctime()函数产生形式的26字节字符串,这与date命令的系统默认输出形式类似:Tue Feb 10 18:27:38 2020/n/0.

strftime()将一个struct tm结构格式化为一个字符串。

常用时间函数及举例

1、time函数头文件:time.h

函数定义:time_t time (time_t *t)

说明:

返回从1970年1月1日的UTC时间从0时0分0妙算起到现在所经过的秒数。

举例如下:#include

#include

int main(){

time_t timep;

long seconds = time(&timep);

printf("%ld\n",seconds);

printf("%ld\n",timep);

return 0;

}

输出:

c5c46784d35a4e6c69860477a3c39dba.png有兴趣的同学可以计算下,从1970年1月1日0时0分0秒到现在经历了多少秒。

附:time_t 一路追踪发现就是从long类型经过不断的typedef ,#define定义过来的。

2、ctime函数定义:char *ctime(const time_t *timep);

说明:将参数所指的time_t结构中的信息转换成真实世界的时间日期表示方法,然后将结果以字符串形式返回。

注意这个是本地时间。

举例如下:#include 

#include

int main(void){

time_t timep;

time(&timep);

printf("%s\n",ctime(&timep));

return 0;

}

输出:

c4a023497ae4e6c782dffddac9871a79.png

3、gmtime函数定义:struct tm *gmtime(const time_t *timep);

说明:将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。此函数返回的时间日期未经时区转换,而是UTC时间。

举例如下:#include 

#include

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;

}

输出:

f824b2f29b4db500ba73e86603307a2c.png

4、 strftime函数#include 

定义:

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 

2 #include 

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 }

执行结果如下:

59a801acf0ba8f6aa4d19babae259da2.png输出结果表示YYYYmmDDHHMMSS

5、 asctime函数定义:

char *asctime(const struct tm *timeptr);

说明:

将参数timeptr所指的struct tm结构中的信息转换成真实时间所使用的时间日期表示方法,结果以字符串形态返回。与ctime()函数不同之处在于传入的参数是不同的结构。

返回值:

返回的也是UTC时间。

举例如下:#include 

#include 

#include

int main(void){

time_t timep;

time(&timep);

printf("%s\n",asctime(gmtime(&timep)));

return EXIT_SUCCESS;

}

输出:

1edf15bbbf471592c0bb4e014b4b83e8.png

6、 localhost函数struct tm *localhost(const time_t *timep);

取得当地目前的时间和日期

举例如下:#include 

#include 

#include

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;

}

输出:

3cc879411246cb42e47cc9a41750d7a4.png

7、mktime函数定义:time_t mktime(struct tm *timeptr);

说明:

用来将参数timeptr所指的tm结构数据转换成从1970年1月1日的UTC时间从0时0分0妙算起到现在所经过的秒数。

举例如下:#include 

#include 

#include

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;

}

输出:

df77c2ddde9b4d2e885a991f1e140bc9.png

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 

#include 

#include

#include

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;

}

输出:

4c99b4bcb10968c23af5551f82b12b70.png

综合实验

现在我们利用这些时间函数,来实现一个定时执行某个任务得功能。

功能程序运行时要记录当前日志文件的最后修改时间;

每个10秒钟就检查下log文件是否被修改,如果没有被修改就休眠10秒钟;

如果log文件被修改了,就将当前的日志文件拷贝成备份文件,备份文件名字加上当前时间;

通过curl发送给ftp服务器;

删除备份文件,重复步骤2。

程序流程图如下:

a7fa044c23279ff6429efa202dabbd5c.png在这里插入图片描述

函数功能介绍

init()

首先记录当前log文件时间,并记录到全局变量last_mtime中。

check_file_change()读取文件最后修改时间,并和last_mtime进行比较,如果相同就返回0,不同就返回1.

file_name_add_time()将当前的日志文件拷贝成备份文件,备份文件名字加上当前时间。

stat()

得到对应文件的属性信息,存放到struct stat结构体变量中。

运行截图:

第一步:

bb16a1432618c8c1eb6a449070c28abc.png因为log文件没有被修改过,所以程序不会上传。

第二步:手动输入字符串 yikoulinux 到日志文件 t.log中。

cbae17b6e24c5f4a9af9b1e671334881.png第三步:因为文件发生了改变,所以打印“file updated”,同时可以看到curl上传文件的log信息。

32ccd311d95bf0f378fa275878c01063.png以下是FTP服务器的根目录,可以看到,上传的日志文件:t-2020-7-26-1-19-45.log。

f0423145c0d8ef35614c4b4339023fa8.png

【补充】配置信息,直接在代码中写死,通常应该从配置文件中读取,为方便读者阅读,本代码没有增加该功能;

FTP服务器搭建,本文没有说明,相关文件比较多,大家可以自行搜索,一口君用的是File zilla;

bc48c40b67a7b7074acefda3fdabd28e.png

通常这种需要长时间运行的程序,需要设置成守护进程,本文没有添加相应功能,读者可以自行搜索。如果强烈要求可以单开一篇详细介绍。

代码中time的管理函数,请读者自行搜索相关文章。

curl也提供了相关的函数库curl.lib,如果要实现更灵活的功能可以使用对应的api。

之所以先把文件拷贝成备份文件,主要是考虑其他模块随时可能修改日志文件,起到一定保护作用。

代码如下

代码如下:/***************************************************

Copyright (C)  公众号: 一口linux

***************************************************/

#include 

#include 

#include 

#include 

#include 

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

链接:https://blog.youkuaiyun.com/daocaokafei/article/details/108610289

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值