ISO8601Duration 1.3.2

ISO8601Duration 1.3.2

Maciej Gad 维护。



Duration Build Status

Swift 使用 Codable 实现的 ISO 8601 持续时间。

本微框架应有助于处理使用 ISO 8601 标准编写的持续时间。它可以将字符串“P3Y6M4DT12H30M5S”解析为 TimeInterval。有关 ISO 8601 标准的更多信息,可以在维基百科中阅读https://en.wikipedia.org/wiki/ISO_8601#Durations

用法

用法非常简单,您可以使用 Duration(string: String)

import ISO8601Duration

let duration = Duration(string: "P3Y6M4DT12H30M5S")
print(duration.timeInterval) //110615405.0

或者您可以像这样从 JSON 中进行解码

{
  "duration": "P3Y6M4DT12H30M5S"
}

然后您可以创建自己的结构

import ISO8601Duration

struct Box: Codable {
  let duration: Duration
}

以标准方式添加加载数据

let data =  Data(bytes: "{\"duration\":\"P3Y6M4DT12H30M5S\"}".utf8) //load you data
let decoder = JSONDecoder()
let box = try decoder.decode(Box.self, from: data)
print(box.duration.timeInterval) //110615405.0

安装

使用 CocoaPods

将其添加到您的 Podfile 中

pod 'ISO8601Duration'

然后调用

pod install

并导入

import ISO8601Duration