|
| 1 | +import Foundation |
| 2 | + |
| 3 | + |
| 4 | +/* |
| 5 | +partial-time https://tools.ietf.org/html/rfc3339 |
| 6 | + |
| 7 | + time-hour = 2DIGIT ; 00-23 |
| 8 | + time-minute = 2DIGIT ; 00-59 |
| 9 | + time-second = 2DIGIT ; 00-58, 00-59, 00-60 based on leap second |
| 10 | + ; rules |
| 11 | + time-secfrac = "." 1*DIGIT |
| 12 | + time-numoffset = ("+" / "-") time-hour ":" time-minute |
| 13 | + time-offset = "Z" / time-numoffset |
| 14 | + |
| 15 | + partial-time = time-hour ":" time-minute ":" time-second |
| 16 | + [time-secfrac] |
| 17 | +*/ |
| 18 | + |
| 19 | + |
| 20 | +var timeExpression: NSRegularExpression = { |
| 21 | + let time = #""" |
| 22 | + (?x) |
| 23 | + ^ |
| 24 | + (?<hour>(([01]\d)|(2[0-3]))) |
| 25 | + : |
| 26 | + (?<minute>([0-5]\d)) |
| 27 | + : |
| 28 | + (?<second>(([0-5]\d)|(60))) |
| 29 | + (?<secfrac>\.\d+)? |
| 30 | + (?<offset> |
| 31 | + ( |
| 32 | + Z |
| 33 | + | |
| 34 | + (?<numoffset> |
| 35 | + (?<numoffsetdirection>[+-]) |
| 36 | + (?<numoffsethour>([01]\d)|2[0-3]) |
| 37 | + : |
| 38 | + (?<numoffsetminute>[0-5]\d) |
| 39 | + ) |
| 40 | + ) |
| 41 | + ) |
| 42 | + $ |
| 43 | + """# |
| 44 | + return try! NSRegularExpression(pattern: time, options: [.caseInsensitive]) |
| 45 | +}() |
| 46 | + |
| 47 | + |
| 48 | +typealias TimeOffset = (hour: Int, minute: Int) |
| 49 | + |
| 50 | + |
| 51 | +enum Offset { |
| 52 | + case zulu |
| 53 | + case positive(TimeOffset) |
| 54 | + case negative(TimeOffset) |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | +struct Time { |
| 59 | + var hour: Int |
| 60 | + var minute: Int |
| 61 | + var second: Int |
| 62 | + var offset: Offset |
| 63 | + |
| 64 | + init(hour: Int, minute: Int, second: Int, offset: Offset) { |
| 65 | + self.hour = hour |
| 66 | + self.minute = minute |
| 67 | + self.second = second |
| 68 | + self.offset = offset |
| 69 | + } |
| 70 | + |
| 71 | + init?(value: String) { |
| 72 | + guard let match = timeExpression.firstMatch(in: value, range: NSMakeRange(0, value.utf16.count)) else { |
| 73 | + return nil |
| 74 | + } |
| 75 | + |
| 76 | + let hourRange = Range(match.range(withName: "hour"), in: value)! |
| 77 | + hour = Int(value[hourRange])! |
| 78 | + |
| 79 | + let minuteRange = Range(match.range(withName: "minute"), in: value)! |
| 80 | + minute = Int(value[minuteRange])! |
| 81 | + |
| 82 | + let secondRange = Range(match.range(withName: "second"), in: value)! |
| 83 | + second = Int(value[secondRange])! |
| 84 | + |
| 85 | + if Range(match.range(withName: "numoffset"), in: value) != nil { |
| 86 | + let direction = value[Range(match.range(withName: "numoffsetdirection"), in: value)!] |
| 87 | + |
| 88 | + let offsetHourRange = Range(match.range(withName: "numoffsethour"), in: value)! |
| 89 | + let offsetHour = Int(value[offsetHourRange])! |
| 90 | + |
| 91 | + let offsetMinuteRange = Range(match.range(withName: "numoffsetminute"), in: value)! |
| 92 | + let offsetMinute = Int(value[offsetMinuteRange])! |
| 93 | + |
| 94 | + if direction == "+" { |
| 95 | + offset = .positive((hour: offsetHour, minute: offsetMinute)) |
| 96 | + } else if direction == "-" { |
| 97 | + offset = .negative((hour: offsetHour, minute: offsetMinute)) |
| 98 | + } else { |
| 99 | + fatalError("ProgramaticError: Incorrect regular expression for time parsing invalid direction") |
| 100 | + } |
| 101 | + } else { |
| 102 | + offset = .zulu |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // returns the time converted to UTC (without num offset) |
| 107 | + var zulu: Time { |
| 108 | + switch offset { |
| 109 | + case .zulu: |
| 110 | + return self |
| 111 | + case let .positive(offset): |
| 112 | + return Time(hour: hour - offset.hour, minute: minute - offset.minute, second: second, offset: .zulu) |
| 113 | + case let .negative(offset): |
| 114 | + return Time(hour: hour + offset.hour, minute: minute + offset.minute, second: second, offset: .zulu) |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | + |
| 120 | +func isValidTime(_ value: String) -> Bool { |
| 121 | + guard let time = Time(value: value) else { |
| 122 | + return false |
| 123 | + } |
| 124 | + |
| 125 | + let zuluTime = time.zulu |
| 126 | + if zuluTime.second == 60 && (zuluTime.hour != 23 || zuluTime.minute != 59) { |
| 127 | + return false |
| 128 | + } |
| 129 | + |
| 130 | + return true |
| 131 | +} |
| 132 | + |
| 133 | + |
| 134 | +func validateTime(_ context: Context, _ value: String) -> AnySequence<ValidationError> { |
| 135 | + if isValidTime(value) { |
| 136 | + return AnySequence(EmptyCollection()) |
| 137 | + } |
| 138 | + |
| 139 | + return AnySequence([ |
| 140 | + ValidationError( |
| 141 | + "'\(value)' is not a valid RFC 3339 formatted time.", |
| 142 | + instanceLocation: context.instanceLocation, |
| 143 | + keywordLocation: context.keywordLocation |
| 144 | + ) |
| 145 | + ]) |
| 146 | +} |
0 commit comments