$ docker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension String { | |
/// Converts an ISO 8601 formatted `String` into `NSDateComponents`. | |
/// | |
/// - Note: Does not accept fractional input (e.g.: P3.5Y), must be integers (e.g.: P3Y6M). | |
/// - SeeAlso: https://en.wikipedia.org/wiki/ISO_8601#Durations | |
/// - Returns: If valid ISO 8601, an `NSDateComponents` representation, otherwise `nil`. | |
func ISO8601DateComponents() -> NSDateComponents? { | |
// Regex adapted from Moment.js https://github.com/moment/moment/blame/develop/src/lib/duration/create.js#L16 | |
let pattern = "^P(?:(\\d*)Y)?(?:(\\d*)M)?(?:(\\d*)W)?(?:(\\d*)D)?(?:T(?:(\\d*)H)?(?:(\\d*)M)?(?:(\\d*)S)?)?$" | |
let nsstringRepresentation = self as NSString |