Convert DAAppearanceTime to Date

218 Views Asked by At

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

1

There are 1 best solutions below

0
Martin R On BEST ANSWER

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.