I'm writing some unit tests for some file sync code I'm writing, and I'm wondering if it's possible (likely via FileManager
but who knows) to change a file's creation / modification date to something earlier than its current date. Or even if I can specify the creation date when I create a new file.
It would be nice to be able to do this programmatically inside my tests if possible, but otherwise I'm interested in if it can be done at all.
I suppose as long as it works on the iOS Simulator, that's fine.
You can change the modification date by running touch -t <date> <file>
, where <date>
is the new date and <file>
is the file you want to modify. See the man page for touch
for the date format, but it's basically like YYYYMMDDhhmm.SS
(year, month, day, hour, minute, second).
Example:
$ touch -t 197501020304.05 foo
$ ls -lT foo
-rw-r--r-- 1 userid staff 0 Jan 2 03:04:05 1975 foo
From the documentation, it looks like you can also set the modification date using NSFileManager
's setAttributes(_:ofItemAtPath:)
method, which lets you set attributes for a given file, including creationDate
and modificationDate
. However, note that:
As in the POSIX standard, the app either must own the file or directory or must be running as superuser for attribute changes to take effect.