获得真实的"最后一次打开"日期?

时间:2012-12-17 12:55:10

标签: macos cocoa file-io

以下代码正常运行。预计会在最后一个发布日期返回应用,但是......

// e.g. entryPath = @"/Applications/Evernote.app"

struct stat output;
int ret = stat([entryPath fileSystemRepresentation], &output);
if (ret == 0)
{
    struct timespec accessTime = output.st_atimespec;
    NSDate * date1 = [[NSDate dateWithTimeIntervalSince1970:accessTime.tv_sec]
            dateWithCalendarFormat:nil timeZone:[NSTimeZone localTimeZone]];

    // and the 10.6+ version: (the result was always the same for me)
    NSDate * date2 = [[[[NSURL fileURLWithPath:entryPath]
            resourceValuesForKeys:[NSArray arrayWithObject:NSURLContentAccessDateKey]
                            error:nil]
            objectForKey:NSURLContentAccessDateKey]
            dateWithCalendarFormat:nil timeZone:[NSTimeZone localTimeZone]];
}

它返回非常奇怪的日期,通常在凌晨3:00-3:30,今天或昨天,当我实际上睡觉时。但之后,我在Finder中单击“获取信息”,它显示真实的最后发布日期(通常小于date1date2,但也可以相等)。如何以编程方式获得它? Finder get info: Last opened date

0 个答案:

没有答案