某些日期的 DateFormatter.localizedString 已损坏 [英] DateFormatter.localizedString is broken for some of the dates
问题描述
这是我在选择器更改时所做的:
延期日期{var fromCurrentToUTC: 日期 {返回添加时间间隔(-TimeInterval(TimeZone.current.secondsFromGMT()))}}var title = "--"如果让日期 = datePickerView.date?.fromCurrentToUTC {title = DateFormatter.localizedString(from: date, dateStyle: .medium, timeStyle: .none)}打印(" - - -")打印(datePickerView.date!)打印(标题)打印(时区.当前)打印(datePickerView.date!.timeIntervalSince1970)dateTimeSegmentControl.setTitle(title, forSegmentAt: 0)
这就是它查找日期的方式:
假设.11 月 6 日之前的日期一切正常,11 月 6 日之后一切都将关闭.为什么?
更新:
我使用的每个时区的关键日期都不同.例如:
华沙 (+0200) 日期是 10 月 30 日芝加哥 (-0500) 日期是 11 月 6 日
有序打印:
<前>-----2017-11-04 00:00:00 +00002017 年 11 月 4 日美国/纽约(当前)1509753600.0-----2017-11-05 00:00:00 +00002017 年 11 月 5 日美国/纽约(当前)1509840000.0-----2017-11-06 00:00:00 +00002017 年 11 月 5 日美国/纽约(当前)1509926400.0-----2017-11-07 00:00:00 +00002017 年 11 月 6 日美国/纽约(当前)1510012800.0在你的函数中
延期日期{var fromCurrentToUTC: 日期 {返回添加时间间隔(-TimeInterval(TimeZone.current.secondsFromGMT()))}}
减去当前日期的 GMT 偏移量,而不是由要调整的日期的 GMT 偏移量.因此你得到如果要调整的日期在 DST 期间而当前日期不在,则结果错误,反之亦然.
这可以通过使用来修复
延期日期{var fromCurrentToUTC: 日期 {return addedTimeInterval(-TimeInterval(TimeZone.current.secondsFromGMT(for: self)))}}
相反.但是,更好的解决方案是设置时区将日期格式化程序设置为 UTC,而不是调整日期:
if let date = datePickerView.date {让 fmt = DateFormatter()fmt.dateStyle = .mediumfmt.timeStyle = .nonefmt.timeZone = TimeZone(secondsFromGMT: 0)让 title = fmt.string(from: date)打印(标题)}
This is what I do when picker changes:
extension Date {
var fromCurrentToUTC: Date {
return addingTimeInterval(-TimeInterval(TimeZone.current.secondsFromGMT()))
}
}
var title = "--"
if let date = datePickerView.date?.fromCurrentToUTC {
title = DateFormatter.localizedString(from: date, dateStyle: .medium, timeStyle: .none)
}
print("-----")
print(datePickerView.date!)
print(title)
print(TimeZone.current)
print(datePickerView.date!.timeIntervalSince1970)
dateTimeSegmentControl.setTitle(title, forSegmentAt: 0)
And this is how it looks for the dates:
Assuming. Everything is fine for the dates before 6th November, and everything is off after 6th November. Why?
update:
That critical date is different for every time zone I use. For example:
Warsaw (+0200) the date is 30 October Chicago (-0500) the date is 6th November
The ordered prints:
----- 2017-11-04 00:00:00 +0000 4 Nov 2017 America/New_York (current) 1509753600.0 ----- 2017-11-05 00:00:00 +0000 5 Nov 2017 America/New_York (current) 1509840000.0 ----- 2017-11-06 00:00:00 +0000 5 Nov 2017 America/New_York (current) 1509926400.0 ----- 2017-11-07 00:00:00 +0000 6 Nov 2017 America/New_York (current) 1510012800.0
In your function
extension Date {
var fromCurrentToUTC: Date {
return addingTimeInterval(-TimeInterval(TimeZone.current.secondsFromGMT()))
}
}
the GMT offset of the current date is subtracted, not by the GMT offset of the date to be adjusted. Therefore you get a wrong result if the date to be adjusted is in a DST period and the current date is not, or vice versa.
That can be fixed by using
extension Date {
var fromCurrentToUTC: Date {
return addingTimeInterval(-TimeInterval(TimeZone.current.secondsFromGMT(for: self)))
}
}
instead. However, a better solution would be to set the timezone of the date formatter to UTC, instead of adjusting the date:
if let date = datePickerView.date {
let fmt = DateFormatter()
fmt.dateStyle = .medium
fmt.timeStyle = .none
fmt.timeZone = TimeZone(secondsFromGMT: 0)
let title = fmt.string(from: date)
print(title)
}
这篇关于某些日期的 DateFormatter.localizedString 已损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!