iOS是否保持文件修改日期更新?

时间:2012-05-16 16:35:08

标签: iphone ios filesystems file-attributes

我正在尝试确定是否(以及何时)我的应用的临时文件目录中的文件已被修改,但修改日期(文件属性中的NSFileModificationDate)似乎始终与创建日期匹配({ {1}})。我有一些模糊的回忆,iOS中的文件系统不会更新修改日期,但我没有在任何地方找到记录。

任何人都可以确认(并且可能指向文档)iOS不会更新修改日期吗?

此外,还有其他一些方法可以确定文件是否已经改变了内容的散列并自己跟踪结果吗?

1 个答案:

答案 0 :(得分:2)

我认为这仍然有效。取自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;
}