https://github.com/linusu/parseisoduration
Parse an ISO 8601 duration to a TimeInterval
https://github.com/linusu/parseisoduration
hacktoberfest iso8601 iso8601-duration parse swift
Last synced: 6 months ago
JSON representation
Parse an ISO 8601 duration to a TimeInterval
- Host: GitHub
- URL: https://github.com/linusu/parseisoduration
- Owner: LinusU
- Created: 2020-12-04T16:05:05.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-12-07T12:46:05.000Z (almost 5 years ago)
- Last Synced: 2025-02-07T11:52:14.379Z (8 months ago)
- Topics: hacktoberfest, iso8601, iso8601-duration, parse, swift
- Language: Swift
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
Awesome Lists containing this project
README
# Swift: Parse ISO Duration
Parse an ISO 8601 duration to a `TimeInterval`
## Installation
Install using Swift Package Manager:
```swift
dependencies: [
.package(url: "https://github.com/LinusU/ParseISODuration", from: "1.0.0"),
]
```## Usage
```swift
import ParseISODurationtry! TimeInterval(fromISODuration: "PT8S") // 8 * 1000
try! TimeInterval(fromISODuration: "PT10M") // 10 * 60 * 1000
try! TimeInterval(fromISODuration: "PT20H") // 20 * 60 * 60 * 1000
try! TimeInterval(fromISODuration: "PT6M4S") // 6 * 60 * 1000 + 4 * 1000try! TimeInterval(fromISODuration: "P10Y10M10D") // Throws ParseISODurationError.ambiguousDuration
try! TimeInterval(fromISODuration: "Hello world") // Throws ParseISODurationError.invalidDuration
```## Year and month
If years or months is specified and more than 0 the library will throw `ParseISODurationError.ambiguousDuration` since its meaning can't be converted to milliseconds.
## Related
- Node.js version: [LinusU/node-parse-iso-duration](https://github.com/LinusU/node-parse-iso-duration)
## License
MIT