gpt4 book ai didi

swift - 将 DAAppearanceTime 转换为日期

转载 作者:搜寻专家 更新时间:2023-11-01 07:14:39 24 4
gpt4 key购买 nike

如何将磁盘仲裁中的DAAppearance时间转换为有效的时间戳?

我尝试了以下方法:

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

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

511348742.912949

1986-03-16 09: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()返回,这就是

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

关于swift - 将 DAAppearanceTime 转换为日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42830609/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com