Swiftpack.co - leonx98/Swift-ISO8601-DurationParser as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by leonx98.
leonx98/Swift-ISO8601-DurationParser 0.9.0
Swift ISO8601 Parser
⭐️ 18
🕓 1 week ago
.package(url: "https://github.com/leonx98/Swift-ISO8601-DurationParser.git", from: "0.9.0")

Swift/ISO8601-Duration Parser

Swift 5 extension which converts ISO 8601 duration strings into a DateComponents object.

Examples

PT12H = 12 hours
P3D = 3 days
P3DT12H = 3 days, 12 hours
P3Y6M4DT12H30M5S = 3 years, 6 months, 4 days, 12 hours, 30 minutes and 5 seconds
P10W = 70 days
For more information look here: https://en.wikipedia.org/wiki/ISO_8601#Durations

Usage

let durationString = "P3Y6M4DT12H30M5S"
let components = DateComponents.durationFrom8601String(durationString)
components?.year // 3
components?.month // 6
components?.day // 4
components?.hour // 12
components?.minute // 30
components?.second // 5

let durationStringInWeeks = "P8W"
let componentsForWeeks = DateComponents.durationFrom8601String(durationStringInWeeks)
componentsForWeeks?.day //56

Installation

SPM

You can use SPM. Add the following to your Package.swift.

.package(url: "https://github.com/leonx98/Swift-ISO8601-DurationParser.git", .upToNextMajor(from: "0.9.0")),

Donation

If you like my open source libraries, you can sponsor it! ☺️

paypal

License

Swift/ISO8601-Duration Parser is available under the MIT license. See the LICENSE file for more info.

Acknowledgments

Objective-C version from @kevinrandrup

GitHub

link
Stars: 18
Last commit: 1 week ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

Add SwiftPM Support
1 year ago
  • Add SPM support
  • Make resulting DateComponents optional
  • Remove external parameter name

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics