2017-03-16 89 views
1

如何將DAAppearance時間從磁盤仲裁轉換爲有效的時間戳?將DAAppearanceTime轉換爲日期

我試過如下:

511348742.912949

1986年3月16日09:

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

我得到正確的DAAppearanceTime回從功能,但轉換後的錯誤年:19:02 +0000

+0

您正在獲取時間戳「511348742.912949」的正確日期,您可以在此處進行確認http://www.onlineconversion.com/unix_time.htm –

+0

這意味着DiskArbitration提供了錯誤的數字嗎?時間是正確的,但我的USB記憶棒自1986年以來沒有連接 – nja

回答

1

"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()回報,那就是

絕對時間是在相對於秒計絕對參考日期爲2001年1月1日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