nextDate 如何考虑时区?
有以下代码:
let start = DateComponents(calendar: .current, year: 2022, month: 4, day: 25).date!
let end = DateComponents(calendar: .current, year: 2022, month: 7, day: 2).date!
let dateInterval: DateInterval = .init(start: start, end: end)
let m2030 = dateInterval.dates(matching: .init(calendar: .current, timeZone: TimeZone(identifier: "Europe/Budapest"), hour: 20, minute: 30, weekday: 2))
extension DateInterval {
func dates(matching components: DateComponents) -> [Date] {
var start = self.start
var dates: [Date] = []
while let date = Calendar.current.nextDate(after: start, matching: components, matchingPolicy: .strict), date <= end {
dates.append(date)
start = date
}
return dates
}
}
我在控制台中看到了这一点:
(lldb) po m2030
▿ 10 elements
▿ 0 : 2022-04-25 13:30:00 +0000
- timeIntervalSinceReferenceDate : 672586200.0
为什么控制台说:13:30:00 +0000
在布达佩斯中是15:30
,尽管我的请求是显示的时间布达佩斯时间20:30
?
我现在在国外,没关系吗?在柬埔寨。 :) +5小时到达布达佩斯。
Have following code:
let start = DateComponents(calendar: .current, year: 2022, month: 4, day: 25).date!
let end = DateComponents(calendar: .current, year: 2022, month: 7, day: 2).date!
let dateInterval: DateInterval = .init(start: start, end: end)
let m2030 = dateInterval.dates(matching: .init(calendar: .current, timeZone: TimeZone(identifier: "Europe/Budapest"), hour: 20, minute: 30, weekday: 2))
extension DateInterval {
func dates(matching components: DateComponents) -> [Date] {
var start = self.start
var dates: [Date] = []
while let date = Calendar.current.nextDate(after: start, matching: components, matchingPolicy: .strict), date <= end {
dates.append(date)
start = date
}
return dates
}
}
And I see this in console:
(lldb) po m2030
▿ 10 elements
▿ 0 : 2022-04-25 13:30:00 +0000
- timeIntervalSinceReferenceDate : 672586200.0
Why console says: 13:30:00 +0000
which in Budapest is 15:30
, though my request was show times Budapest time at 20:30
?
I am now abroad, does it matter? In Cambodia. :) +5 hour to Budapest.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要的是使用布达佩斯时区使用自定义日历。尝试这样:
这将打印:
What you need is to use a custom calendar using the Budapest timezone. Try like this:
This will print: