UIDatePicker 不遵守夏令时
UIDatePicker does not respect Daylight Savings Time
我正在尝试从 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 PM EDT:
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 库在 table 视图单元格中显示 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 PM (EDT),但我没有得到 2:15 AM UTC:
print(myTimeSettings.getJSON()) // ["Hour": 3, "Minute": 12]
为什么日期选择器返回错误的时间?
根据Apple's Documentation,您需要调用此方法:
Declaration
func daylightSavingTimeOffset(for date: Date = default) -> TimeInterval
Parameters
date
The date to use for the calculation. The default value is the current
date.
感谢 ,我能够通过将 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: [=10=])! }
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 { [=10=].rawValue }),
"Hour" : components.hour!,
"Minute" : components.minute!
]
}
我正在尝试从 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 PM EDT:
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 库在 table 视图单元格中显示 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 PM (EDT),但我没有得到 2:15 AM UTC:
print(myTimeSettings.getJSON()) // ["Hour": 3, "Minute": 12]
为什么日期选择器返回错误的时间?
根据Apple's Documentation,您需要调用此方法:
Declaration
func daylightSavingTimeOffset(for date: Date = default) -> TimeInterval
Parameters
date
The date to use for the calculation. The default value is the current date.
感谢 daylightSavingTimeOffset
函数添加到 MyTimeSettings
:
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: [=10=])! }
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 { [=10=].rawValue }),
"Hour" : components.hour!,
"Minute" : components.minute!
]
}