Skip to content

LinusU/ParseISODuration

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Swift: Parse ISO Duration

Parse an ISO 8601 duration to a TimeInterval

Installation

Install using Swift Package Manager:

    dependencies: [
        .package(url: "https://github.com/LinusU/ParseISODuration", from: "1.0.0"),
    ]

Usage

import ParseISODuration

try! 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 * 1000

try! 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

License

MIT