如何删除文件内7天之前的数据?
首先, 我们需要在每一条信息之前记住一个时间撮, 这个时间撮有两个作用:
#1. 用来记住这条信息的时间,
#2. 用来做标记, 以后面好更新数据;
首先我采用的是本地时间来作为该时间撮;
data.timestamp = time((time_t*)NULL);
time函数解释如下:
NAMEtime - get time in secondsSYNOPSIS#include <time.h>time_t time(time_t *t);DESCRIPTIONtime() 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 VALUEOn success, the value of time in seconds since the Epoch is returned. On error, ((time_t) -1) is returned, and errno is set appropriately.ERRORSEFAULT t points outside your accessible address space.CONFORMING TOSVr4, 4.3BSD, C89, C99, POSIX.1-2001. POSIX does not specify any error conditions.NOTESPOSIX.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 notleap 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 ALSOdate(1), gettimeofday(2), ctime(3), ftime(3), time(7)COLOPHONThis 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 foundat 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