@@ -88,17 +88,15 @@ func remindersStore() throws -> EKEventStore {
8888}
8989/// A due date: `YYYY-MM-DD` is date-only; a date-time is ISO 8601, and one without an offset is this
9090/// Mac's local time, as typing it into the app would be.
91- func dueComponents ( _ text : String ) throws -> DateComponents {
91+ func dueComponents ( _ text : String , _ field : String = " Due " ) throws -> DateComponents {
9292 var calendar = Calendar ( identifier : . gregorian )
9393 if text . range ( of : #"^\d{4}-\d{2}-\d{2}$"# , options : . regularExpression ) != nil {
9494 let parts = text . split ( separator : " - " ) . compactMap { Int ( $0 ) }
9595 let components = DateComponents ( calendar : calendar , year : parts [ 0 ] , month : parts [ 1 ] , day : parts [ 2 ] )
96- guard components . isValidDate else { throw ComputerError ( " invalid_arguments " , " Due date is not a real date." , dispatchState : " not_started " ) }
96+ guard components . isValidDate else { throw invalidCommand ( " \( field ) is not a real date." ) }
9797 return DateComponents ( year : parts [ 0 ] , month : parts [ 1 ] , day : parts [ 2 ] )
9898 }
99- guard let date = isoDateTime ( text ) else {
100- throw ComputerError ( " invalid_arguments " , " Due must be YYYY-MM-DD or an ISO 8601 date-time. " , dispatchState : " not_started " )
101- }
99+ guard let date = isoDateTime ( text ) else { throw invalidCommand ( " \( field ) must be YYYY-MM-DD or a real ISO 8601 date-time. " ) }
102100 calendar . timeZone = . current
103101 return calendar . dateComponents ( [ . year , . month , . day , . hour , . minute , . second , . timeZone ] , from : date )
104102}
@@ -134,29 +132,42 @@ func localGregorian() -> Calendar {
134132 var calendar = Calendar ( identifier : . gregorian ) ; calendar . timeZone = . current
135133 return calendar
136134}
137- /// An ISO 8601 date-time; without an offset it is this Mac's local time.
135+ let isoDateTimePattern = try! NSRegularExpression ( pattern : #"^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(?::(\d{2})(?:\.\d{1,9})?)?(?:(Z)|([+-])(\d{2}):(\d{2}))?$"# )
136+ /// An ISO 8601 date-time; without an offset it is this Mac's local time. Fractions of a second do not
137+ /// matter to a calendar item, so they are dropped. Every field must be in range: `ISO8601DateFormatter`
138+ /// rolls Feb 30 into March and accepts any offset, so a zoned time is checked part by part.
138139func isoDateTime ( _ text : String ) -> Date ? {
139- for options : ISO8601DateFormatter . Options in [ [ . withInternetDateTime ] , [ . withInternetDateTime , . withFractionalSeconds ] ] {
140- let zoned = ISO8601DateFormatter ( ) ; zoned . formatOptions = options
141- if let date = zoned . date ( from : text ) { return date }
140+ guard let match = isoDateTimePattern . firstMatch ( in : text , range : NSRange ( text . startIndex ... , in : text ) ) else { return nil }
141+ func part ( _ index : Int ) -> String ? { Range ( match . range ( at : index ) , in : text ) . map { String ( text [ $0 ] ) } }
142+ func number ( _ index : Int ) -> Int ? { part ( index ) . flatMap { Int ( $0 ) } }
143+ var zone : TimeZone ?
144+ if part ( 7 ) != nil { zone = TimeZone ( secondsFromGMT : 0 ) }
145+ if let sign = part ( 8 ) , let hours = number ( 9 ) , let minutes = number ( 10 ) {
146+ guard hours <= 14 , minutes < 60 else { return nil }
147+ zone = TimeZone ( secondsFromGMT : ( sign == " - " ? - 1 : 1 ) * ( hours * 3600 + minutes * 60 ) )
148+ }
149+ guard let zone else {
150+ // A local time keeps the formatter, which resolves a repeated hour at a daylight-saving change.
151+ let whole = String ( text . prefix ( 19 ) )
152+ let local = DateFormatter ( ) ; local . locale = Locale ( identifier : " en_US_POSIX " ) ; local . timeZone = . current
153+ local . dateFormat = whole . count == 16 ? " yyyy-MM-dd'T'HH:mm " : " yyyy-MM-dd'T'HH:mm:ss "
154+ local . isLenient = false
155+ return local . date ( from : whole )
142156 }
143- // Fractions of a second do not matter to a calendar item, so a local time drops them.
144- guard text . range ( of : #"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2}(\.\d{1,9})?)?$"# , options : . regularExpression ) != nil else { return nil }
145- let whole = String ( text . prefix ( 19 ) )
146- let local = DateFormatter ( ) ; local . locale = Locale ( identifier : " en_US_POSIX " ) ; local . timeZone = . current
147- local . dateFormat = whole . count == 16 ? " yyyy-MM-dd'T'HH:mm " : " yyyy-MM-dd'T'HH:mm:ss "
148- local . isLenient = false
149- return local . date ( from : whole )
157+ var calendar = Calendar ( identifier : . gregorian ) ; calendar . timeZone = zone
158+ let components = DateComponents ( year : number ( 1 ) , month : number ( 2 ) , day : number ( 3 ) , hour : number ( 4 ) , minute : number ( 5 ) , second : number ( 6 ) ?? 0 )
159+ guard components . isValidDate ( in : calendar ) else { return nil }
160+ return calendar . date ( from : components )
150161}
151162/// An event boundary: an ISO 8601 date-time (local without an offset), or `YYYY-MM-DD` for all-day events.
152163func eventDate ( _ text : String ? , _ field : String , allDay : Bool ) throws -> Date {
153164 let text = try commandText ( text , field , max : 64 )
154165 if allDay {
155- let parts = try dueComponents ( text )
166+ let parts = try dueComponents ( text , field )
156167 guard parts . hour == nil , let date = localGregorian ( ) . date ( from : parts ) else { throw invalidCommand ( " All-day \( field ) must be YYYY-MM-DD. " ) }
157168 return date
158169 }
159- guard let date = isoDateTime ( text ) else { throw invalidCommand ( " \( field ) must be an ISO 8601 date-time. " ) }
170+ guard let date = isoDateTime ( text ) else { throw invalidCommand ( " \( field ) must be a real ISO 8601 date-time. " ) }
160171 return date
161172}
162173func runAppCommand ( _ command : AppCommand ) throws -> [ String : Any ] {
@@ -168,11 +179,10 @@ func runAppCommand(_ command: AppCommand) throws -> [String: Any] {
168179 }
169180 return [ " kind " : " command " , " command " : command . name , " items " : lists , " truncated " : allLists . count > 200 ]
170181 case " create_reminder " :
171- let title = ( command . title ?? " " ) . trimmingCharacters ( in : . whitespacesAndNewlines )
172- guard ! title . isEmpty , title . count <= 1024 else { throw ComputerError ( " invalid_arguments " , " A reminder needs a title of at most 1024 characters. " , dispatchState : " not_started " ) }
173- guard ( command . notes ? . count ?? 0 ) <= 8000 else { throw ComputerError ( " invalid_arguments " , " Notes are limited to 8000 characters. " , dispatchState : " not_started " ) }
174- guard [ 0 , 1 , 5 , 9 ] . contains ( command . priority ?? 0 ) else { throw ComputerError ( " invalid_arguments " , " Priority must be 0 (none), 1 (high), 5 (medium) or 9 (low). " , dispatchState : " not_started " ) }
175- let due = try command . due . map ( dueComponents )
182+ let title = try commandText ( command . title , " Title " )
183+ let notes = try optionalText ( command . notes , " Notes " , max : 8000 )
184+ guard [ 0 , 1 , 5 , 9 ] . contains ( command . priority ?? 0 ) else { throw invalidCommand ( " Priority must be 0 (none), 1 (high), 5 (medium) or 9 (low). " ) }
185+ let due = try command . due . map { try dueComponents ( $0 ) }
176186 let store = try remindersStore ( )
177187 let list : EKCalendar
178188 if let listId = command . listId {
@@ -186,7 +196,7 @@ func runAppCommand(_ command: AppCommand) throws -> [String: Any] {
186196 }
187197 guard list . allowsContentModifications else { throw ComputerError ( " list_read_only " , " This reminder list is read-only. " , dispatchState : " not_started " ) }
188198 let reminder = EKReminder ( eventStore : store )
189- reminder . calendar = list ; reminder . title = title ; reminder . notes = command . notes
199+ reminder . calendar = list ; reminder . title = title ; reminder . notes = notes
190200 reminder . priority = command . priority ?? 0 ; reminder . dueDateComponents = due
191201 try checkCancellation ( )
192202 inputDispatched = true
0 commit comments