Caleb
Caleb

Reputation: 125007

Does iOS keep file modification dates updated?

I'm trying to determine whether (and when) a file in my app's temp file directory has been modified, but the modification date (NSFileModificationDate in the file attributes) seems to always match the creation date (NSFileCreationDate). I have some vague recollection that the file system in iOS doesn't update modification dates, but I haven't found that documented anywhere.

Can anyone confirm (and maybe point to documentation) that iOS doesn't update modification dates?

Also, is there some other method for determining whether a file has changed short of hashing the contents and keeping track of the result myself?

Upvotes: 3

Views: 1222

Answers (1)

Dylan Gattey
Dylan Gattey

Reputation: 1724

I think this still works. Taken from 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;
}

Upvotes: 2

Related Questions