@@ -129,6 +129,11 @@ func contactStore() throws -> CNContactStore {
129129 }
130130 return CNContactStore ( )
131131}
132+ /// The Gregorian calendar in this Mac's time zone, so `YYYY-MM-DD` means the same day whatever calendar the user prefers.
133+ func localGregorian ( ) -> Calendar {
134+ var calendar = Calendar ( identifier : . gregorian ) ; calendar . timeZone = . current
135+ return calendar
136+ }
132137/// An ISO 8601 date-time; without an offset it is this Mac's local time.
133138func isoDateTime ( _ text : String ) -> Date ? {
134139 let zoned = ISO8601DateFormatter ( ) ; zoned . formatOptions = [ . withInternetDateTime ]
@@ -144,7 +149,7 @@ func eventDate(_ text: String?, _ field: String, allDay: Bool) throws -> Date {
144149 let text = try commandText ( text , field , max : 64 )
145150 if allDay {
146151 let parts = try dueComponents ( text )
147- guard parts . hour == nil , let date = Calendar . current . date ( from : parts ) else { throw invalidCommand ( " All-day \( field ) must be YYYY-MM-DD. " ) }
152+ guard parts . hour == nil , let date = localGregorian ( ) . date ( from : parts ) else { throw invalidCommand ( " All-day \( field ) must be YYYY-MM-DD. " ) }
148153 return date
149154 }
150155 guard let date = isoDateTime ( text ) else { throw invalidCommand ( " \( field ) must be an ISO 8601 date-time. " ) }
@@ -178,6 +183,7 @@ func runAppCommand(_ command: AppCommand) throws -> [String: Any] {
178183 let reminder = EKReminder ( eventStore : store )
179184 reminder . calendar = list ; reminder . title = title ; reminder . notes = command . notes
180185 reminder . priority = command . priority ?? 0 ; reminder . dueDateComponents = due
186+ try checkCancellation ( )
181187 inputDispatched = true
182188 try store . save ( reminder , commit : true )
183189 return [ " kind " : " command " , " command " : command . name , " created " : [ " id " : reminder . calendarItemIdentifier , " title " : title , " list " : list . title ] ]
@@ -192,7 +198,10 @@ func runAppCommand(_ command: AppCommand) throws -> [String: Any] {
192198 let start = try eventDate ( command . start , " Start " , allDay : allDay )
193199 let end = try eventDate ( command . end , " End " , allDay : allDay )
194200 guard allDay ? end >= start : end > start else { throw invalidCommand ( " End must be after start. " ) }
195- guard end . timeIntervalSince ( start ) <= 31 * 86_400 else { throw invalidCommand ( " An event can last at most 31 days. " ) }
201+ // Calendar days, so a daylight-saving change inside the event does not shorten the limit.
202+ guard let limit = localGregorian ( ) . date ( byAdding : . day , value : 31 , to : start ) , end <= limit else {
203+ throw invalidCommand ( " An event can last at most 31 days. " )
204+ }
196205 let location = try optionalText ( command . location , " Location " )
197206 let notes = try optionalText ( command . notes , " Notes " , max : 8000 )
198207 if let minutes = command . alertMinutesBefore , ! ( 0 ... 40_320 ) . contains ( minutes ) { throw invalidCommand ( " alertMinutesBefore must be 0 to 40320. " ) }
@@ -212,6 +221,7 @@ func runAppCommand(_ command: AppCommand) throws -> [String: Any] {
212221 event . calendar = calendar ; event . title = title ; event . isAllDay = allDay
213222 event . startDate = start ; event . endDate = end ; event . location = location ; event . notes = notes
214223 if let minutes = command . alertMinutesBefore { event . addAlarm ( EKAlarm ( relativeOffset : TimeInterval ( - minutes * 60 ) ) ) }
224+ try checkCancellation ( )
215225 inputDispatched = true
216226 try store . save ( event , span : . thisEvent , commit : true )
217227 return [ " kind " : " command " , " command " : command . name , " created " : [ " id " : event . eventIdentifier ?? event . calendarItemIdentifier , " title " : title , " list " : calendar . title ] ]
@@ -229,6 +239,7 @@ func runAppCommand(_ command: AppCommand) throws -> [String: Any] {
229239 }
230240 let group = CNMutableGroup ( ) ; group . name = name
231241 let request = CNSaveRequest ( ) ; request . add ( group , toContainerWithIdentifier : nil )
242+ try checkCancellation ( )
232243 inputDispatched = true
233244 try store . execute ( request )
234245 return [ " kind " : " command " , " command " : command . name , " created " : [ " id " : group . identifier , " title " : name , " list " : " Contacts " ] ]
@@ -253,8 +264,11 @@ func runAppCommand(_ command: AppCommand) throws -> [String: Any] {
253264 contact . organizationName = organization ?? " " ; contact . jobTitle = jobTitle ?? " "
254265 contact . emailAddresses = emails . map { CNLabeledValue ( label : CNLabelWork , value : $0 as NSString ) }
255266 contact . phoneNumbers = phones . map { CNLabeledValue ( label : CNLabelPhoneNumberMain , value : CNPhoneNumber ( stringValue : $0 ) ) }
256- let request = CNSaveRequest ( ) ; request . add ( contact , toContainerWithIdentifier : nil )
267+ // A group's members must live in the group's own account, so the contact is saved there.
268+ let container = try group . flatMap { try store . containers ( matching : CNContainer . predicateForContainerOfGroup ( withIdentifier : $0 . identifier ) ) . first ? . identifier }
269+ let request = CNSaveRequest ( ) ; request . add ( contact , toContainerWithIdentifier : container )
257270 if let group { request . addMember ( contact , to : group ) }
271+ try checkCancellation ( )
258272 inputDispatched = true
259273 try store . execute ( request )
260274 let display = [ given , family ] . compactMap { $0 } . joined ( separator : " " )
0 commit comments