将 DAAppearanceTime 转换为日期

Convert DAAppearanceTime to Date

如何将 DAAppearance 来自 Disk Arbitration 的时间转换为有效的时间戳?

我尝试了以下方法:

  if let appearanceTime = diskinfo["DAAppearanceTime"] as? NSNumber{
                            print(appearanceTime)
                            let date = NSDate(timeIntervalSince1970: TimeInterval(appearanceTime))
                            print(date)                             
                        }

我从函数返回正确的 DAAppearanceTime,但转换后的年份错误:

511348742.912949

1986-03-1609:19:02+0000

"DAAppearanceTime" 密钥没有正式记录,但 DiskArbitration 框架是开源的。

DAInternal.c:

 const CFStringRef kDADiskDescriptionAppearanceTimeKey  = CFSTR( "DAAppearanceTime"  );

DADisk.c:

/*
 * Create the disk description -- appearance time.
 */

time = CFAbsoluteTimeGetCurrent( );

object = CFNumberCreate( allocator, kCFNumberDoubleType, &time );
if ( object == NULL )  goto DADiskCreateFromIOMediaErr;

CFDictionarySetValue( disk->_description, kDADiskDescriptionAppearanceTimeKey, object );
CFRelease( object );

所以那个键的值就是CFAbsoluteTimeGetCurrent()returns,也就是

Absolute time is measured in seconds relative to the absolute reference date of Jan 1 2001 00:00:00 GMT.

你把它转换成 Date 这样的:

if let time = diskinfo["DAAppearanceTime"] as? Double {
    let date = Date(timeIntervalSinceReferenceDate: time)
    print(date)
}

对于值 511348742.912949 这导致 日期 2017-03-16 09:19:02 +0000.