简体   繁体   English

快速创建EKEvent,无需调整夏令时

[英]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: 我的应用程序中有一个向日历添加事件的功能,该过程涉及解析日期的字符串表示形式(使用夏令时调整,并为其设置EKEvent startDate,这是一个代码段:

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 一段时间以来,一切工作都按预期进行,但是自从英国夏令时过渡以来,我注意到日历上的活动要提前一个小时

Eg the event I set is "16-04-2018 14:00:00", but the calendar date is "16-04-2018 15 :00:00". 比如我设置的事件是“16-04-2018 14:00:00”,但日历日期是“16-04-2018 15:00:00”。

Is there a way to tell EKEvent to disable the daylight saving adjustment? 有没有办法告诉EKEvent禁用夏令时调整? As my string date is already with it. 由于我的字符串日期已经存在。

我发现了问题,显然我在构造时区为gmt + 00:00的DateFormatter对象。删除时区后,ios日历上的时间保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM