将要通过网络发送/接收的日期(绝对时间)转换为 Swift 中的数据?

Convert a Date (absolute time) to be sent/received across the network as Data in Swift?(将要通过网络发送/接收的日期(绝对时间)转换为 Swift 中的数据?)
本文介绍了将要通过网络发送/接收的日期(绝对时间)转换为 Swift 中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在寻找一种 Swifty 方式来生成时间戳.

I am looking for a Swifty way to generate a timestamp.

我的 macOS 应用程序会记录一些数据并在数据创建时间上打上标记.然后,数据将通过网络发送(作为 Data)在 iPad 上重建.

My macOS app logs some data and stamps it with the time the data was created. The data will then be sent across the network (as Data) to be reconstructed on an iPad.

是否有任何 Swift 类可以生成时间戳?国安日期?NSTimeIntervalSince1970?CFAbsoluteTimeGetCurrent()

Is there any Swift class that will work to generate the timestamp? NSDate? NSTimeIntervalSince1970? CFAbsoluteTimeGetCurrent()

要求是:

  1. 将时间戳存储在尽可能少的字节中(pref. Int)
  2. 与真实的地球时间有些相似(我宁愿不生成我的自己的时间格式)
  3. 毫秒精度
  4. 快速构建
  5. iOS 9+、macOS 10.10+

推荐答案

您可以发送您的 Date 并将其转换为 Data(8 字节浮点)并返回日期如下:

You can send your Date converting it to Data (8-bytes floating point) and back to Date as follow:

extension Numeric {
    var data: Data {
        var source = self
        return .init(bytes: &source, count: MemoryLayout<Self>.size)
    }
    init<D: DataProtocol>(_ data: D) {
        var value: Self = .zero
        let size = withUnsafeMutableBytes(of: &value, { data.copyBytes(to: $0)} )
        assert(size == MemoryLayout.size(ofValue: value))
        self = value
    }
}

<小时>

extension UInt64 {
    var bitPattern: Double { .init(bitPattern: self) }
}

<小时>

extension Date {
    var data: Data { timeIntervalSinceReferenceDate.bitPattern.littleEndian.data }
    init<D: DataProtocol>(data: D) {
        self.init(timeIntervalSinceReferenceDate: data.timeIntervalSinceReferenceDate)
    }
}

<小时>

extension DataProtocol {
    func value<N: Numeric>() -> N { .init(self) }
    var uint64: UInt64 { value() }
    var timeIntervalSinceReferenceDate: TimeInterval { uint64.littleEndian.bitPattern }
    var date: Date { .init(data: self) }
}

<小时>

游乐场测试


Playground Testing

let date = Date()            // "Nov 15, 2019 at 12:13 PM"
let data = date.data         // 8 bytes
print(Array(data))           // "[25, 232, 158, 22, 124, 191, 193, 65]
"
let loadedDate = data.date   // "Nov 15, 2019 at 12:13 PM"
print(date == loadedDate)    // "true"

这篇关于将要通过网络发送/接收的日期(绝对时间)转换为 Swift 中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Can`t copy file from bundle to documents directory in iOS(无法将文件从捆绑包复制到 iOS 中的文档目录)
How to copy a quot;Dictionaryquot; in Swift?(如何复制“字典在斯威夫特?)
Swift - How to mutate a struct object when iterating over it(Swift - 迭代结构对象时如何对其进行变异)
How to copy text to clipboard/pasteboard with Swift(如何使用 Swift 将文本复制到剪贴板/粘贴板)
Swift Cannot invoke #39;find#39; with an argument list of type #39;([Score], Score)#39; where Score is a struct(Swift 无法使用类型为“([Score],Score)的参数列表调用“find,其中 Score 是一个结构)
How to load image asynchronously with Swift using UIImageViewExtension and preventing duplicate images or wrong Images loaded to cells(如何使用 UIImageViewExtension 与 Swift 异步加载图像并防止重复图像或错误图像加载到单元格) - IT屋-程序员软