我有一个C程序,我使用递归函数遍历目录结构。它需要一个
time_t
递归dir函数来自
How to recursively list directories in C on Linux?
void traversedir(time_t cutoff, const char* name, int indent)
{
if (indent > 9)
return;
DIR* dir;
struct dirent* entry;
if (!(dir = opendir(name)))
return;
struct tm *t = gmtime(&cutoff);
printf("%d-%d-%d %d:%d:%d (%lld)\n", (1900 + t->tm_year), (t->tm_mon + 1), t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, (long long) cutoff);
sleep(1); // only for debugging
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_DIR) {
char path[1024];
if (entry->d_name[0] == '.' && !isValidNumber(entry->d_name))
continue;
snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
...
traversedir(cutoff, path, indent + 2);
}
}
closedir(dir);
}
void imageCleanup()
{
time_t cutoff = (time(NULL) - (86400 * 10));
do {
printf("Cutoff: %lld\n", (long long) cutoff);
traversedir(cutoff, "/path/to/dir", 0);
sleep(2);
cutoff += 3600;
} while (!available_storage() && cutoff < time(NULL));
}
Cutoff: 1534245930
2018-8-14 11:25:30 (1534245930)
2018-8-24 11:25:31 (1534245930)
2018-8-14 11:25:30 (1534245930)
2018-8-24 11:25:33 (1534245930)
2018-8-14 11:25:30 (1534245930)
2018-8-14 11:25:35 (1534245930)
2018-8-14 11:25:30 (1534245930)
2018-8-14 11:25:30 (1534245930)
2018-8-24 11:25:38 (1534245930)
2018-8-14 11:25:30 (1534245930)
2018-8-14 11:25:30 (1534245930)
2018-8-14 11:25:30 (1534245930)
2018-8-14 11:25:30 (1534245930)
2018-8-24 11:25:43 (1534245930)
2018-8-14 11:25:30 (1534245930)
2018-8-14 11:25:30 (1534245930)
2018-8-14 11:25:30 (1534245930)
2018-8-14 11:25:30 (1534245930)
2018-8-14 11:25:30 (1534245930)
2018-8-14 11:25:30 (1534245930)
2018-8-14 11:25:30 (1534245930)
2018-8-14 11:25:30 (1534245930)
2018-8-24 11:25:52 (1534245930)
2018-8-14 11:25:30 (1534245930)
2018-8-24 11:25:54 (1534245930)
2018-8-14 11:25:55 (1534245930)
2018-8-24 11:25:56 (1534245930)
2018-8-14 11:25:30 (1534245930)
2018-8-24 11:25:58 (1534245930)
时间
从中转换结构的位置在括号中。它是始终如一的
1534245930
或2018年8月14日上午11:25:30,即程序执行时的负十天。
你可以看到转换非常不一致。执行时,秒数会跟随实际时间几次。日子也变了几次。
有人知道或知道是什么导致的吗?我试着做一个简单的循环来转换一个静态的
时间