When you want to convert `Data` to some specific type, you need to know the data format of the Data.
When it is not a primitive type as in the linked article, things are not so easy.
And about the data format stored in extended attributes, there are very little information on the web.
Seems Apple would not like app programmers to access extended attributes explicitly, and they may be differ in versions of macOS.
As of "com.apple.lastuseddate#PS", I can get 16-byte Data, such as:
| let lastUseddDateData = try url.extendedAttribute(forName: "com.apple.lastuseddate#PS") |
| print(lastUseddDateData as NSData) |
-> <0430cb5c 00000000 4805492c 00000000>
It seems to be two 64-bit integers (little endian). I guess the first represents something like Unix time in seconds, and the other sub-seconds.
As far as I tested, the first seems to represent seconds from 1970-01-01 00:00:00 (UTC) (as well as Unix time), and the other for subseconds in nano-second.
| let (seconds, subseconds) = lastUseddDateData.withUnsafeBytes {bytes in |
| return (bytes.load(as: Int64.self), |
| bytes.load(fromByteOffset: 8, as: Int64.self)) |
| } |
| let NANO_SECONDS_IN_SECOND: TimeInterval = 1_000_000_000 |
| let lastUsedData = Date(timeIntervalSince1970: TimeInterval(seconds)+TimeInterval(subseconds)/NANO_SECONDS_IN_SECOND) |
| print(lastUsedData, lastUsedData.timeIntervalSinceReferenceDate) |
-> 2019-05-02 17:59:32 +0000 578512772.7429831
The result is nearly equivalent to the Date given by `NSMetadataItem(url: url)!.value(forAttribute: NSMetadataItemLastUsedDateKey) as! Date`, seems you have no need to access this extended attribute directly.
| let date = NSMetadataItem(url: url)!.value(forAttribute: NSMetadataItemLastUsedDateKey) as! Date |
| print(date, date.timeIntervalSinceReferenceDate) |
-> 2019-05-02 17:59:32 +0000 578512772.742983