我如何比较 C 中的时间? 我的程序正在获取 2 个文件的最后修改时间,然后比较该时间以查看哪个时间是最新的。 有没有比较时间的功能,或者你必须自己创建一个?这是我的获取时间函数:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
void getFileCreationTime(char *path) {
struct stat attr;
stat(path, &attr);
printf("Last modified time: %s", ctime(&attr.st_mtime));
}
最佳答案
使用 time.h
中的 difftime(time1, time0)
获取两个时间之间的差异。这将计算 time1 - time0
并返回表示秒数差异的 double
。如果为正,则 time1
晚于 time0
;如果为负,则 time0
较晚;如果为 0,则它们相同。
关于在 C 中比较两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31633943/