@@ -136,13 +136,17 @@ func localGregorian() -> Calendar {
136136}
137137/// An ISO 8601 date-time; without an offset it is this Mac's local time.
138138func isoDateTime ( _ text : String ) -> Date ? {
139- let zoned = ISO8601DateFormatter ( ) ; zoned . formatOptions = [ . withInternetDateTime ]
140- if let date = zoned . date ( from : text ) { return date }
141- guard text . range ( of : #"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2})?$"# , options : . regularExpression ) != nil else { return nil }
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 }
142+ }
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 ) )
142146 let local = DateFormatter ( ) ; local . locale = Locale ( identifier : " en_US_POSIX " ) ; local . timeZone = . current
143- local . dateFormat = text . count == 16 ? " yyyy-MM-dd'T'HH:mm " : " yyyy-MM-dd'T'HH:mm:ss "
147+ local . dateFormat = whole . count == 16 ? " yyyy-MM-dd'T'HH:mm " : " yyyy-MM-dd'T'HH:mm:ss "
144148 local . isLenient = false
145- return local . date ( from : text )
149+ return local . date ( from : whole )
146150}
147151/// An event boundary: an ISO 8601 date-time (local without an offset), or `YYYY-MM-DD` for all-day events.
148152func eventDate ( _ text : String ? , _ field : String , allDay : Bool ) throws -> Date {
@@ -272,7 +276,8 @@ func runAppCommand(_ command: AppCommand) throws -> [String: Any] {
272276 inputDispatched = true
273277 try store . execute ( request )
274278 let display = [ given , family ] . compactMap { $0 } . joined ( separator : " " )
275- return [ " kind " : " command " , " command " : command . name , " created " : [ " id " : contact . identifier , " title " : display . isEmpty ? organization ?? " " : display , " list " : group ? . name ?? " Contacts " ] ]
279+ let title = String ( ( display . isEmpty ? organization ?? " " : display ) . prefix ( 1024 ) )
280+ return [ " kind " : " command " , " command " : command . name , " created " : [ " id " : contact . identifier , " title " : title , " list " : String ( ( group ? . name ?? " Contacts " ) . prefix ( 1024 ) ) ] ]
276281 default :
277282 throw ComputerError ( " unknown_command " , " Unknown app command. " , dispatchState : " not_started " )
278283 }
0 commit comments