nextDate 如何考虑时区?

发布于 2025-01-21 04:04:36 字数 1153 浏览 0 评论 0原文

有以下代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝戈者 2025-01-28 04:04:36

您需要的是使用布达佩斯时区使用自定义日历。尝试这样:


extension DateInterval {
    func dates(matching components: DateComponents, using calendar: Calendar = .current) -> [Date] {
        var start = self.start
        var dates: [Date] = []
        while let date = calendar.nextDate(after: start, matching: components, matchingPolicy: .strict), date <= end {
            dates.append(date)
            start = date
        }
        return dates
    }
}

var budapestCalendar = Calendar(identifier: .iso8601)
budapestCalendar.timeZone = TimeZone(identifier: "Europe/Budapest")!

let start = DateComponents(calendar: budapestCalendar, year: 2022, month: 4, day: 25).date!
let end = DateComponents(calendar: budapestCalendar, year: 2022, month: 7, day: 2).date!
let dateInterval: DateInterval = .init(start: start, end: end)

let m2030 = dateInterval.dates(matching: .init(hour: 20, minute: 30, weekday: 2), using: budapestCalendar)

for date in m2030 {
    print(date)
}

这将打印:

2022-04-25 18:30:00 +0000
2022-05-02 18:30:00 +0000
2022-05-09 18:30:00 +0000
2022-05-16 18:30:00 +0000
2022-05-23 18:30:00 +0000
2022-05-30 18:30:00 +0000
2022-06-06 18:30:00 +0000
2022-06-13 18:30:00 +0000
2022-06-20 18:30:00 +0000
2022-06-27 18:30:00 +0000

What you need is to use a custom calendar using the Budapest timezone. Try like this:


extension DateInterval {
    func dates(matching components: DateComponents, using calendar: Calendar = .current) -> [Date] {
        var start = self.start
        var dates: [Date] = []
        while let date = calendar.nextDate(after: start, matching: components, matchingPolicy: .strict), date <= end {
            dates.append(date)
            start = date
        }
        return dates
    }
}

var budapestCalendar = Calendar(identifier: .iso8601)
budapestCalendar.timeZone = TimeZone(identifier: "Europe/Budapest")!

let start = DateComponents(calendar: budapestCalendar, year: 2022, month: 4, day: 25).date!
let end = DateComponents(calendar: budapestCalendar, year: 2022, month: 7, day: 2).date!
let dateInterval: DateInterval = .init(start: start, end: end)

let m2030 = dateInterval.dates(matching: .init(hour: 20, minute: 30, weekday: 2), using: budapestCalendar)

for date in m2030 {
    print(date)
}

This will print:

2022-04-25 18:30:00 +0000
2022-05-02 18:30:00 +0000
2022-05-09 18:30:00 +0000
2022-05-16 18:30:00 +0000
2022-05-23 18:30:00 +0000
2022-05-30 18:30:00 +0000
2022-06-06 18:30:00 +0000
2022-06-13 18:30:00 +0000
2022-06-20 18:30:00 +0000
2022-06-27 18:30:00 +0000

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文