How to extract the folder or the file access time?

I want to extract the folder or the file access time, is similar the access time is the same in the Microsoft platform file attribute, but the Mac OS platform cannot extract to the correct this attribute, I attempt use the NSURLContentAccessDateKey method. I attempt use KK the method, was still unable to obtain the wish result, asks fellow masters to direct.

Replies

This is working for me. Consider this sequence…

First I created a file containing some text.

$ cat > tmp.txt
hello cruel world
^D

stat
shows that all the times are pretty close.
$ stat tmp.txt
16777220 84144560 -rw-r--r-- 1 quinn staff 0 18 "Dec  5 08:48:03 2016" "Dec  5 08:48:03 2016" "Dec  5 08:48:03 2016" "Dec  5 08:47:59 2016" 4096 8 0 tmp.txt

I then waited a while and accessed the file again.

$ cat tmp.txt
hello cruel world

Re-doing the

stat
shows that the access time has bumped up to 08:48:30.
16777220 84144560 -rw-r--r-- 1 quinn staff 0 18 "Dec  5 08:48:30 2016" "Dec  5 08:48:03 2016" "Dec  5 08:48:03 2016" "Dec  5 08:47:59 2016" 4096 8 0 tmp.txt

Now I ran this code:

BOOL        success;
NSURL *    url;
NSDate *    accessTime;

url = [NSURL fileURLWithPath:@"/Users/quinn/tmp.txt"];
success = [url getResourceValue:&accessTime forKey:NSURLContentAccessDateKey error:NULL];
if ( ! success ) {
    NSLog(@"failure");
} else {
    NSLog(@"%@", accessTime);
}

which printed:

2016-12-05 08:50:52.358087 AccessTime[11589:720579] 2016-12-05 08:48:30 +0000

And lo!, the access time is 08:48:30, which is exactly what

stat
showed.

IMPORTANT The times match up exactly in this case because I’m on GMT. If you’re in some other time zone, be aware that

stat
prints local time while calling
NSLog
on an NSDate prints GMT.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks for your detailed answer. I attempt this method once more, already succeeded solves, should be because I left the error too carelessly.