简体   繁体   English

这个 Swift TimeZone 是另一个时区假设中日期的缩写有什么问题?

[英]What is wrong with this Swift TimeZone for abbreviation for a date in another timezone assumption?

In an Xcode playground with the following Swift code, I get the commented output.在具有以下 Swift 代码的 Xcode 操场中,我得到了评论的 output。 Given a MST TimeZone object and a date that would be in the MDT TimeZone for this MST TimeZone, I do not receive the expected MDT TimeZone abbreviation output.给定 MST 时区 object 和该 MDT 时区中的日期,我没有收到预期的 MDT 时区缩写 output。 What am I not understanding about this function, and how do I get the desired output of figuring out which of the two possible timezones a given date is in given the current example MST timezone?我对这个 function 有什么不了解,以及如何获得所需的 output 来确定给定日期在给定当前示例 MST 时区的两个可能时区中的哪一个?

// Daylight saving time (MDT) 2021 in Colorado began at 2:00 AM on Sunday, March 14
let mstDate = Date(timeIntervalSinceReferenceDate: 636327068) // "Mar 1, 2021 at 2:31 PM"
let mdtDate = Date(timeIntervalSinceReferenceDate: 637709468) // "Mar 17, 2021 at 3:31 PM"
let mstTimeZone = TimeZone(abbreviation: "MST")!
let mdtTimeZone = TimeZone(abbreviation: "MDT")!

mstTimeZone.abbreviation(for: mstDate) // expected MST, got MST
mstTimeZone.abbreviation(for: mdtDate) // expected MDT, got *****MST*****?

mdtTimeZone.abbreviation(for: mstDate) // expected MST, got MST
mdtTimeZone.abbreviation(for: mdtDate) // expected MDT, got MDT

Do not rely on timezone abbreviations.不要依赖时区缩写。 MST is being interpreted as "Mountain Standard Time" but it is being interpreted as Phoenix, Arizona ("America/Phoenix") instead of Denver, Colorado ("America/Denver"). MST 被解释为“山地标准时间”,但它被解释为亚利桑那州凤凰城(“美国/凤凰城”),而不是科罗拉多州丹佛市(“美国/丹佛”)。 You should always use timezone identifiers instead of abbreviations which are ambiguous.您应该始终使用时区标识符,而不是模棱两可的缩写。 The timezone identifier doesn't change based on a date.时区标识符不会根据日期而改变。 What you need is to check if it is daylight saving or not for the desired date/timezone and get a timezone localized name based on it:您需要检查所需日期/时区是否为夏令时,并根据它获取时区本地化名称:

extension TimeZone {
    static let denverCO = Self(identifier: "America/Denver")!
    func localizedName(for date: Date) -> String { localizedName(for: isDaylightSavingTime(for: date) ? .daylightSaving : .standard, locale: .current) ?? "" }
    func localizedNameShort(for date: Date) -> String { localizedName(for: isDaylightSavingTime(for: date) ? .shortDaylightSaving : .shortStandard, locale: .current) ?? "" }
}

let mst = Date(timeIntervalSinceReferenceDate: 636327068) // "Mar 1, 2021 at 2:31 PM"
let mdt = Date(timeIntervalSinceReferenceDate: 637709468) // "Mar 17, 2021 at 3:31 PM"

TimeZone.denverCO.localizedName(for: mst)  // "Mountain Standard Time"
TimeZone.denverCO.localizedName(for: mdt)  // "Mountain Daylight Time"

TimeZone.denverCO.localizedNameShort(for: mst)  // "MST"
TimeZone.denverCO.localizedNameShort(for: mdt)  // "MDT"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM