UIDatePicker不尊重夏令时

时间:2018-04-04 12:01:07

标签: ios swift nscalendar nsdatecomponents

我正在尝试从UIDatePicker获取小时和分钟值,以便在我的服务器中使用,但UIDatePicker的值是一小时。我当地的时区目前使用夏令时,所以我假设UIDatePicker不尊重它。

以下是我用来将UIDatePicker提供的日期转换为小时和分钟值的代码:

struct MyTimeSettings {

    var time: Date!

    init(from json: [String : Any]) {
        var calendar = Calendar.current
        calendar.timeZone = TimeZone(abbreviation: "UTC")!

        self.time = calendar.date(from: DateComponents(timeZone: TimeZone(abbreviation: "UTC")!, year: 0, month: 0, day: 0, hour: (json["Hour"] as! Int), minute: (json["Minute"] as! Int)))
    }

    init() {}

    func getJSON() -> [String : Any] {
        var calendar = Calendar.current
        calendar.timeZone = TimeZone(abbreviation: "UTC")!
        let components = calendar.dateComponents(in: TimeZone(abbreviation: "UTC")!, from: self.time)

        return [
            "Hour" : components.hour!,
            "Minute" : components.minute!
        ]
    }

    static func ==(lhs: MyTimeSettings, rhs: MyTimeSettings) -> Bool {
        return (lhs.time == rhs.time)
    }
}

为了测试它,我给它一个Date,其评估时间为美国东部时间晚上10:15:

var settings = MyTimeSettings()
let date = Date(timeIntervalSince1970: 1522894500)
settings.time = date

print(settings.getJSON()) // ["Hour": 2, "Minute": 15]

它也适用于输入JSON:

let json = ["Hour": 2, "Minute": 15]
print(MyTimeSettings(from: json).getJSON()) // ["Hour": 2, "Minute": 15]

我的问题是,从UIDatePicker获取日期时,相同的代码正常工作。我正在使用Eureka库在表格视图单元格中显示UIDatePicker

<<< TimePickerRow() { row in
    row.cell.datePicker.timeZone = TimeZone.current
    row.cell.datePicker.locale = Locale.current
    row.value = self.myTimeSettings.time
}.onChange({ (row) in
    self.myTimeSettings.time = row.value
})

然后我将日期选择器上的时间设置为晚上10:15(美国东部时间),但我没有得到UTC时间凌晨2:15:

print(myTimeSettings.getJSON()) // ["Hour": 3, "Minute": 12]

为什么日期选择器返回错误的时间?

2 个答案:

答案 0 :(得分:1)

根据Apple's Documentation,您需要调用此方法:

  

<强>声明

     

func daylightSavingTimeOffset(for date: Date = default) -> TimeInterval

     

参数

     

<强>日期

     

用于计算的日期。默认值是当前值   日期。

答案 1 :(得分:0)

感谢Jake's answer,我可以通过将daylightSavingTimeOffset函数添加到MyTimeSettings来获得正确的UTC时间:

init(from json: [String : Any]) {
    var calendar = Calendar.current
    calendar.timeZone = TimeZone(abbreviation: "UTC")!

    self.enabled = json["Enabled"] as! Bool
    self.daysToNotify = (json["Interval"] as! [String]).map { SHNotificationIntervalType(rawValue: $0)! }
    self.time = calendar.date(from: DateComponents(timeZone: TimeZone(abbreviation: "UTC")!, year: 0, month: 0, day: 0, hour: (json["Hour"] as! Int), minute: (json["Minute"] as! Int)))
    if TimeZone.current.isDaylightSavingTime() {
        let offset = TimeZone.current.daylightSavingTimeOffset()
        self.time = self.time + offset
    }
}

init() {}

func getJSON() -> [String : Any] {
    var calendar = Calendar.current
    calendar.timeZone = TimeZone(abbreviation: "UTC")!

    var components: DateComponents
    if TimeZone.current.isDaylightSavingTime() {
        let offset = TimeZone.current.daylightSavingTimeOffset()
        components = calendar.dateComponents(in: TimeZone(abbreviation: "UTC")!, from: self.time - offset)
    } else {
        components = calendar.dateComponents(in: TimeZone(abbreviation: "UTC")!, from: self.time)
    }

    return [
        "Enabled" : self.enabled!,
        "Interval" : (self.daysToNotify.map { $0.rawValue }),
        "Hour" : components.hour!,
        "Minute" : components.minute!
    ]
}