如何删除文件内7天之前的数据?

如何删除文件内7天之前的数据?


首先, 我们需要在每一条信息之前记住一个时间撮, 这个时间撮有两个作用:

#1. 用来记住这条信息的时间,

#2. 用来做标记, 以后面好更新数据;


首先我采用的是本地时间来作为该时间撮;

data.timestamp = time((time_t*)NULL); 

time函数解释如下:

NAME
       time - get time in seconds

SYNOPSIS
       #include <time.h>

       time_t time(time_t *t);

DESCRIPTION
       time() returns the time as the number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).

       If t is non-NULL, the return value is also stored in the memory pointed to by t.

RETURN VALUE
       On success, the value of time in seconds since the Epoch is returned.  On error, ((time_t) -1) is returned, and errno is set appropriately.

ERRORS
       EFAULT t points outside your accessible address space.

CONFORMING TO
       SVr4, 4.3BSD, C89, C99, POSIX.1-2001.  POSIX does not specify any error conditions.

NOTES
       POSIX.1  defines  seconds since the Epoch using a formula that approximates the number of seconds between a specified time and the Epoch.  This for‐
       mula takes account of the facts that all years that are evenly divisible by 4 are leap years, but years that are evenly divisible  by  100  are  not
       leap years unless they are also evenly divisible by 400, in which case they are leap years.  This value is not the same as the actual number of sec‐
       onds between the time and the Epoch, because of leap seconds and because system clocks are not required to be synchronized to a standard  reference.
       The intention is that the interpretation of seconds since the Epoch values be consistent; see POSIX.1-2008 Rationale A.4.15 for further rationale.

SEE ALSO
       date(1), gettimeofday(2), ctime(3), ftime(3), time(7)

COLOPHON
       This  page is part of release 3.54 of the Linux man-pages project.  A description of the project, and information about reporting bugs, can be found
       at http://www.kernel.org/doc/man-pages/.


通过写文件的形式将相关消息及时间记录下来:

ret =  fprintf(stream, "%hu %u %hhu %u %u %u %hu %u %u %u %hu\n", 
				data.listNum, data.timestamp, data.type。

如果需要显示当时时间可采用:

	printf("The calendar time is %s\n", <strong>ctime(&timeStamp)</strong>);

增加判断条件来筛除7天之前的数据:

                        #define MAX_TIME_DURATION    (60*60*24*6) 
                        tt = time((time_t *)NULL);
			if (tt - data.timestamp > MAX_TIME_DURATION) // to add the judgement case. data.timestamp 
			{
				d_printf(D_DEBUG, NULL, "timestamp() - data.timestamp > MAX_TIME_DURATION\n");
			}

Best Wishes!

Yannick Tong



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值