Error[8]: Undefined offset: 8, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

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

您可以

Date
将其转换为
Data
(8字节浮点数)并将其发送回
Date
如下:

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: 
extension UInt64 {    var bitPattern: Double { .init(bitPattern: self) }}
)} ) assert(size == MemoryLayout.size(ofValue: value)) self = value }}

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

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

游乐场测试

[+++]


)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 165, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
将日期(绝对时间)转换为通过Swift在网络上作为数据发送接收的日期?_随笔_内存溢出

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

将日期(绝对时间)转换为通过Swift在网络上作为数据发送接收的日期?,第1张

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

您可以

Date
将其转换为
Data
(8字节浮点数)并将其发送回
Date
如下:

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: 
extension UInt64 {    var bitPattern: Double { .init(bitPattern: self) }}
)} ) assert(size == MemoryLayout.size(ofValue: value)) self = value }}

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

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

游乐场测试



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5642203.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存