在 C 中比较两次

标签 c datetime compare

我如何比较 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/

相关文章:

C 数据类型创建

c++ - 在没有 OpenMP 的机器上忽略它

C++ - 具有下限/上限的循环数组?

iphone - 如何检查 NSNumber 中的空值

c - 单链表程序中不打印节点元素

mysql - 无法在数据库中添加日期

javascript - 为什么我不能从包含毫秒的字符串创建日期?

python - 如何在 python 中更改日期时间格式?

javascript - 正则表达式比较两个数字

比较C中的两个矩阵