UTC-12 和 UTC+12 是否有时区缩写?

Are there time zone abbreviations for UTC-12 and UTC+12?

我正在解决一个问题,我想对 NSDates 进行计算,其中单个 NSDate 在不同时区给出不同的 dd/mm/yyyy 值。

为此,我目前使用纽约市 (EST) 和新西兰奥克兰,因为它们经常在不同的日期。

我希望能够使用国际日期变更线两侧的时区、UTC+12 和 UTC-12。似乎有一个标准的 UTC+12 缩写,ANAT,代表俄罗斯的 Anadyr。但是,TimeZone/NSTimeZone 的 iOS 实现似乎无法识别它。似乎也没有 UTC-12 的缩写(在阿拉斯加)。

有人知道是否有 iOS(或 Mac OS)可以识别的 UTC+12 和 UTC-12 缩写吗?

看来答案是否定的。

我写了一些代码来获取所有系统时区,按偏移量对它们进行排序,然后打印它们:

typealias timeZoneTuple = (abbreviation: String, name: String, offset: Int)

let timeZones = TimeZone.abbreviationDictionary
let mappedTimeZones: [timeZoneTuple] = timeZones
  .map {key, value in
    var offset = 0
    if let timeZone = TimeZone(abbreviation: key) {
      offset = timeZone.secondsFromGMT() / 3600
      
    }
    return (abbreviation: key, name: value, offset:offset)}
  .sorted {[=10=].offset < .offset}
mappedTimeZones.forEach {
  let abbreviation = [=10=].abbreviation.padding(toLength: 4, withPad: " ", startingAt: 0)
  let name = [=10=].name.padding(toLength: 20, withPad: " ", startingAt: 0)
  print("abbreviation = \(abbreviation), offset = \(name), val = \([=10=].offset)")}

以上代码的输出为:

abbreviation = HST , offset = Pacific/Honolulu    , val = -10
abbreviation = AKDT, offset = America/Juneau      , val = -9
abbreviation = AKST, offset = America/Juneau      , val = -9
abbreviation = PST , offset = America/Los_Angeles , val = -8
abbreviation = PDT , offset = America/Los_Angeles , val = -8
abbreviation = MDT , offset = America/Denver      , val = -7
abbreviation = MST , offset = America/Denver      , val = -7
abbreviation = CDT , offset = America/Chicago     , val = -6
abbreviation = CST , offset = America/Chicago     , val = -6
abbreviation = EDT , offset = America/New_York    , val = -5
abbreviation = PET , offset = America/Lima        , val = -5
abbreviation = EST , offset = America/New_York    , val = -5
abbreviation = COT , offset = America/Bogota      , val = -5
abbreviation = ADT , offset = America/Halifax     , val = -4
abbreviation = AST , offset = America/Halifax     , val = -4
abbreviation = CLT , offset = America/Santiago    , val = -3
abbreviation = CLST, offset = America/Santiago    , val = -3
abbreviation = ART , offset = America/Argentina/Bu, val = -3
abbreviation = BRST, offset = America/Sao_Paulo   , val = -2
abbreviation = BRT , offset = America/Sao_Paulo   , val = -2
abbreviation = GMT , offset = GMT                 , val = 0
abbreviation = WET , offset = Europe/Lisbon       , val = 0
abbreviation = BST , offset = Europe/London       , val = 0
abbreviation = WEST, offset = Europe/Lisbon       , val = 0
abbreviation = UTC , offset = UTC                 , val = 0
abbreviation = CEST, offset = Europe/Paris        , val = 1
abbreviation = WAT , offset = Africa/Lagos        , val = 1
abbreviation = CET , offset = Europe/Paris        , val = 1
abbreviation = CAT , offset = Africa/Harare       , val = 2
abbreviation = MSD , offset = Europe/Moscow       , val = 3
abbreviation = EAT , offset = Africa/Addis_Ababa  , val = 3
abbreviation = IRST, offset = Asia/Tehran         , val = 3
abbreviation = MSK , offset = Europe/Moscow       , val = 3
abbreviation = EET , offset = Europe/Istanbul     , val = 3
abbreviation = EEST, offset = Europe/Istanbul     , val = 3
abbreviation = GST , offset = Asia/Dubai          , val = 4
abbreviation = IST , offset = Asia/Calcutta       , val = 5
abbreviation = PKT , offset = Asia/Karachi        , val = 5
abbreviation = BDT , offset = Asia/Dhaka          , val = 6
abbreviation = WIT , offset = Asia/Jakarta        , val = 7
abbreviation = ICT , offset = Asia/Bangkok        , val = 7
abbreviation = SGT , offset = Asia/Singapore      , val = 8
abbreviation = HKT , offset = Asia/Hong_Kong      , val = 8
abbreviation = PHT , offset = Asia/Manila         , val = 8
abbreviation = KST , offset = Asia/Seoul          , val = 9
abbreviation = JST , offset = Asia/Tokyo          , val = 9
abbreviation = NZDT, offset = Pacific/Auckland    , val = 13
abbreviation = NZST, offset = Pacific/Auckland    , val = 13

所以看起来 UTC-12、UTC-11、UTC-1、UTC+10、UTC+11 和 UTC+12 都从 [=34= 中可用的“命名”时区中消失了].

编辑:

根据@MattJohnson 的评论,标识符似乎是获取可用时区列表的更好方法。修改我的代码以改用标识符:

struct timeZoneStruct: CustomStringConvertible {
  let identifier: String
  var offset: Int
  
  var description: String {
    let displayOffset = String(format: "%3d", offset)
    let displayIdentifier = (identifier + ",").padding(toLength: 30, withPad: " ", startingAt: 0)
    return "identifier = \(displayIdentifier) offset = \(displayOffset)"
  }
}
  
  
let timeZoneIDs = TimeZone.knownTimeZoneIdentifiers
let mappedTimeZones: [timeZoneStruct] = timeZoneIDs
  .map {identifier in
    var offset = 0
    if let timeZone = TimeZone(identifier: identifier) {
      offset = timeZone.secondsFromGMT() / 3600
    }
    return timeZoneStruct(identifier: identifier, offset: offset)}
  .sorted {[=12=].offset < .offset}
mappedTimeZones.forEach {
  print([=12=].description)
}

这会生成从 UTC-11 (Pacific/Pago_pago) 到 UTC+14 (Pacific/Apia)

的时区列表

(大多数时区有很多重复项,因此列表太长,无法包含在此处。)

所以似乎有定义的时区用于从 UTC-11 到 UTC+14 的偏移量。然而,UTC-12 没有时区,即使位于 Lat/Long: 0°12'N / 176°29'W 的贝克岛处于 UTC-12 时区。好奇。

这是获取所有时区及其各自缩写的最简单方法。 P.S 并非所有时区都有其适当的 3 字母缩写。

 let timezoneList = NSTimeZone.knownTimeZoneNames
    for i in 0...timezoneList.count - 1 {
    let locale = NSTimeZone.init(name: timezoneList[i])
    print("Region: \((locale?.name)!) Abbr: \((locale?.abbreviation)!)")
  }

还有总共 51 个 3 个字母的缩写:

  print(TimeZone.abbreviationDictionary.count)

您还可以探索 https://developer.apple.com/documentation/foundation/timezone 了解更多信息。