Search code examples
macosstatlast-modified

How do I reliably retrieve a file's last modified time on OS X?


I'm experiencing a problem on OS X, where stat intermittently returns a struct whose st_mtime has a value of 0, for certain files.

Why does this happen, and is there any alternative function that will provide me with a reliable modified date for a file?


Solution

  • If you are using Objective-C, NSFileManager has a nice API named "attributesOfItemAtPath:error:" where one of the things passed back in the NSDictionary object is "fileModificationDate".

    You also can use CoreFoundation CFURL functions, like "CFURLCopyResourcePropertyForKey" which has something called "kCFURLAttributeModificationDateKey" which you can get the value of.