不知道你到底要问什么问题,是比较两个文件最后修改时间,还是计算一个文件最后修改时间到“现在”的秒数。下面给出比较两个文件时间的代码:
#include<stdio.h>
#include<stdlib.h>
#include<sys/stat.h>
int main(int argc,char *argv[])
{
if(argc!=3)
{
fprintf(stderr,"usage: %s <filepath><filepath>\n",argv[0])
exit(1)
}
struct stat buf1
struct stat buf2
if(lstat(argv[1],&buf1)<0 || lstat(argv[2],&buf2)<0)
{
perror("lstat error")
exit(2)
}
/* print time */
printf("%s: %ld %s: %ld\n",argv[1],buf1.st_mtime,argv[2],buf2.st_mtime)
/* print time difference */
printf("%ld\n",buf1.st_mtime-buf2.st_mtime)
return 0
}
#include <stdio.h>#include <time.h>
int
main ()
{
struct tm tm, tm2
char buf[255]
time_t t1, t2
strptime ("Thu Feb 5 13:12:18 EST 2009", "%a %b %d %H:%M:%S EST %Y", &tm)
strptime ("Fri Feb 6 13:11:18 EST 2009", "%a %b %d %H:%M:%S EST %Y", &tm2)
t1 = mktime(&tm)
t2 = mktime(&tm2)
printf("TIME: %ld, %ld, %ld\n", t2, t1, t2-t1)
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)