我在挂载的驱动器中有一个文件列表。我正在尝试设置访问权限和修改时间。
这是使用utime进行修改之前的状态信息
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的时区信息?
发布于 2020-07-28 18:06:22
在文件时间戳中没有存储时区信息(至少在我使用的文件系统中没有)。这只是stat正在以这种方式显示它。
stat.c中的相关代码
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格式的文件的时间戳,没有任何时区信息。如果在localtime_rz()调用中从文件时间戳到struct tm的秒数转换成功,则时间戳将以包含%z时区信息的格式打印。如果调用失败,则使用"%s.%09d"。时间戳不会存储,它是在stat显示有关文件的信息时从时间戳中推断(“猜测”)的。
https://stackoverflow.com/questions/63131598
复制相似问题