iosswiftcalendardstekevent

Swift creating EKEvent without daylight saving adjustment


I have in my app a feature to add an event to calendar, the process involves parsing a string representation of the date (with daylight saving adjustment, and setting the EKEvent startDate to it, here is a snippet:

let eventStore : EKEventStore = EKEventStore()
eventStore.requestAccess(to: .event) { (granted, error) in
    if (granted) && (error == nil) {
        let event:EKEvent = EKEvent(eventStore: eventStore)

        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "dd-MM-yyyy HH:mm"
        // Just for demonstration
        let myStartingDate = "16-04-2018 14:00:00"

        if let eventDate = dateFormatter.date(from: myStartingDate) {
            event.title = "Test event"

            event.startDate = eventDate

            event.endDate = eventDate.addingTimeInterval(TimeInterval(120 + 20 * 60))
            event.notes = "This is a note"
            event.location = "some location"
            event.calendar = eventStore.defaultCalendarForNewEvents
            do {
                try eventStore.save(event, span: .thisEvent)

                // Success, now open the calendar.
                self.openCalendarOn(date: eventDate)

            } catch let error as NSError {
                print("failed to save event with error : \(error)")
            }
        }
    }
}

Everything was working as expected for some time, but since the UK daylight saving transition, I've noticed that the event on the calendar is one hour ahead

E.g the event I set is "16-04-2018 14:00:00", but the calendar date is "16-04-2018 15:00:00".

Is there a way to tell EKEvent to disable the daylight saving adjustment? As my string date is already with it.


Solution

  • I found the problem, apparently I was constructing the DateFormatter object with timezone of gmt+00:00, Once I removed the timezone, the time remained the same on the ios calendar.