一个 Swift 的 ISO 8601 持续时间解析器。灵感来自 Luxon 的 Duration 对象。
Codable
协议。Duration
创建 DateComponents
对象。Calendar
的扩展,可以使用 Duration
像使用 DateComponents
一样。从 ISO 8601 字符串创建持续时间。
let duration = Duration(fromISO: "P2Y4M12DT5H34M48S")
// Duration contains the parsed values
duration.year // 2
duration.month // 4
duration.day // 12
duration.hour // 5
duration.minute // 34
duration.second // 48
从持续时间获取 DateComponents
对象。
let dateComponents = duration.dateComponents
Swift Package Manager 是一个用于管理 Swift 代码分发的工具。它集成到了 swift
编译器中。
要在你的项目中使用 Duration
,只需将此包添加为依赖项
dependencies: [
.package(url: "https://github.com/longinius/swift-duration.git", .upToNextMajor(from: "1.0.0"))
]