【问题标题】:Does iOS keep file modification dates updated?iOS 是否会更新文件修改日期?
【发布时间】:2012-05-24 06:29:03
【问题描述】:
我正在尝试确定我的应用临时文件目录中的文件是否(以及何时)被修改,但修改日期(文件属性中的NSFileModificationDate)似乎总是与创建日期匹配(NSFileCreationDate )。我有一些模糊的回忆,iOS 中的文件系统不会更新修改日期,但我没有发现任何地方都有记录。
任何人都可以确认(并可能指向文档)iOS 不会更新修改日期吗?
此外,是否有其他方法可以确定文件是否已更改,而不是自己对内容进行哈希处理并跟踪结果?
【问题讨论】:
标签:
iphone
ios
filesystems
file-attributes
【解决方案1】:
我认为这仍然有效。取自Get directory contents in date modified order:
+ (NSDate*) getModificationDateForFileAtPath:(NSString*)path {
struct tm* date; // create a time structure
struct stat attrib; // create a file attribute structure
stat([path UTF8String], &attrib); // get the attributes of afile.txt
date = gmtime(&(attrib.st_mtime)); // Get the last modified time and put it into the time structure
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setSecond: date->tm_sec];
[comps setMinute: date->tm_min];
[comps setHour: date->tm_hour];
[comps setDay: date->tm_mday];
[comps setMonth: date->tm_mon + 1];
[comps setYear: date->tm_year + 1900];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *modificationDate = [[cal dateFromComponents:comps] addTimeInterval:[[NSTimeZone systemTimeZone] secondsFromGMT]];
[comps release];
return modificationDate;
}