简体   繁体   中英

Convert DAAppearanceTime to Date

How can the DAAppearance Time from the Disk​Arbitration be converted to a valid Timestamp?

I tried the following:

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

I get the correct DAAppearanceTime back from the function but the wrong Year after the conversion:

511348742.912949

1986-03-16 09:19:02 +0000

The "DAAppearanceTime" key is not officially documented, but the DiskArbitration framework is open source.

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 );

So the value of that key is what CFAbsoluteTimeGetCurrent() returns, and that is the

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

You convert it to a Date like this:

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

For the value 511348742.912949 this results in the date 2017-03-16 09:19:02 +0000 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM