c - 设置文件访问时间、修改时间
问题描述
我有一个已安装驱动器中的文件列表。我正在尝试设置访问和修改时间。
这是使用utime修改前的stat信息
Access: 2020-07-28 15:06:51.000000000 +0530
Modify: 2020-07-28 15:06:51.000000000 +0530
使用 utime 后,这里是统计信息。
Access: 2020-07-28 21:20:47.-469639744
Modify: 2020-07-28 21:20:47.-469740064
这是代码
#include <stdio.h>
#include <utime.h>
#include <time.h>
#include <string.h>
#include <errno.h>
int main(void) {
const char *filepath = "pathToFile/file";
struct utimbuf ubuf;
ubuf.actime = time(NULL) + (time_t)(6*60*60);
ubuf.modtime = time(NULL) + (time_t)(6*60*60);
errno = 0;
int ret = utime(filepath, &ubuf);
if (ret == -1) {
printf("Error is: %s\n", strerror(errno));
} else {
puts("No Error!");
}
return 0;
}
我如何在这里保留+0530的时区信息?
解决方案
文件时间戳中没有存储时区信息(至少在我使用的文件系统中没有)。它只stat
是以这种方式显示它。
相关代码来自stat.c
human_time()
:
if (localtime_rz (tz, &t.tv_sec, &tm))
nstrftime (str, sizeof str, "%Y-%m-%d %H:%M:%S.%N %z", &tm, tz, ns);
else
{
char secbuf[INT_BUFSIZE_BOUND (intmax_t)];
sprintf (str, "%s.%09d", timetostr (t.tv_sec, secbuf), ns);
}
在内部,所有 stat “知道”是struct timespec
格式文件的时间戳,没有任何时区信息。如果从文件时间戳到struct tm
in localtime_rz()
call 的秒转换成功,则时间戳以包含%z
时区信息的格式打印。如果调用失败,则"%s.%09d"
使用 then。不存储时间戳,它是在stat
显示有关文件的信息时从戳中推断(“猜测”)的。
推荐阅读
- javascript - 如何监听 getBoundingClientRect() 的变化?
- android - std::string 到 std::vector
在 Android 上:没有匹配的构造函数 - typescript - Typescript 模块已成功解析但找不到
- javascript - 如果innerHTML 包含x、y、z 等,则replaceWith -- 需要针对多个innerHTML 变体的解决方案
- python - Python将哈希转换为数据框
- ios - 设置 UIButton 图片的内容模式
- c - 印刷过多,以结果处理
- websphere-liberty - 使用默认 java 信任库 (cacerts) 打包自由应用程序的最佳方式
- javascript - 显示不带括号的对象数组,但元素(包括逗号)必须保留
- c# - WPF 中的 DatePicker 显示 01.01.0001 而不是今天