Swiftpack.co - kkla320/ISO8601DurationFormatter as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by kkla320.
kkla320/ISO8601DurationFormatter v2.0.0
A formatter for converting ISO8601 durations to DateComponents.
⭐️ 20
🕓 1 year ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/kkla320/ISO8601DurationFormatter.git", from: "v2.0.0")

ISO8601DurationFormatter

A formatter for converting ISO 8601 durations to DateComponents.

Installation

Swift Package Manager

Add the following to your Package.swift file's dependencies:

.package(url: "https://github.com/kkla320/ISO8601DurationFormatter.git", from: "2.0.0")

And then import wherever needed

import ISO8601DurationFormatter

Example

Using ISO8601DurationFormatter

Converting a string to DateComponents

let input = "PT40M"
let dateComponents = try formatter.dateComponents(from: input)
print(dateComponents.minute) // 40

Converting DateComponents to string

let dateComponents = DateComponents(year: 6,
                                    month: 2,
                                    day: 2,
                                    hour: 4,
                                    minute: 44,
                                    second: 22,
                                    weekOfYear: 2)
let iso8601DurationString = formatter.string(from: input)
print(iso8601DurationString) // P6Y2M2W2DT4H44M22S

Using extension methods

Converting a string to DateComponents

let dateComponents = try DateComponents(iso8601DurationString: "PT40M")
print(dateComponents.minute) // 40

You can also use negative durations as defined by ISO 8601-2:2019.

let dateComponents = try DateComponents(iso8601DurationString: "-PT40M")
print(dateComponents.minute) // -40

Be aware, that this is defined in an extension of the standard. Other libaries could not work with negative values.

Converting DateComponents to string

let dateComponents = DateComponents(year: 6,
                                    month: 2,
                                    day: 2,
                                    hour: 4,
                                    minute: 44,
                                    second: 22,
                                    weekOfYear: 2)

let ISO8601DurationString = dateComponents.toISO8601Duration()
print(ISO8601DurationString) // P6Y2M2W2DT4H44M22S

You can also configure the behaviour of toISO8601Duration with the emitZeroOrNilValues parameter

let dateComponents = DateComponents(year: 0,
                                    month: 0,
                                    day: 2,
                                    hour: 4,
                                    minute: 44,
                                    second: nil,
                                    weekOfYear: 2)

let ISO8601DurationString = dateComponents.toISO8601Duration(emitZeroOrNilValues: true)
print(ISO8601DurationString) // P2W2DT4H44M

Special thanks to Igor-Palaguta for implementing the most in his project YoutubeEngine.

GitHub

link
Stars: 20
Last commit: 28 weeks ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

2.0.0
1 year ago

Release 2.0.0

  • Added a new init for DateComponents @ScottSymes-ANZ
  • Changed ISO8601DurationFormatter.dateComponents(from string: String) to throw an error, if the input could not be parsed @ScottSymes-ANZ
  • Added ISO8601DurationFormatter.string(from dateComponents: DateComponents) to complete the implementation of Formatter @ScottSymes-ANZ
  • Added the possibility to parse negative durations according to ISO8601-2:2019 @ScottSymes-ANZ
  • Added the possibility to omit zero or nil properties from DateComponents, so they won't be included in the resulting string @ScottSymes-ANZ

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