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

时间:2017-02-12 14:21:00

标签: ios cocoa timezone nstimezone

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

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

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

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

2 个答案:

答案 0 :(得分:0)

看起来答案是肯定的。

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

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 {$0.offset < $1.offset}
mappedTimeZones.forEach {
  let abbreviation = $0.abbreviation.padding(toLength: 4, withPad: " ", startingAt: 0)
  let name = $0.name.padding(toLength: 20, withPad: " ", startingAt: 0)
  print("abbreviation = \(abbreviation), offset = \(name), val = \($0.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都缺少Cocoa中可用的“命名”时区。

编辑:

根据@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 {$0.offset < $1.offset}
mappedTimeZones.forEach {
  print($0.description)
}

产生一系列时区列表,范围从UTC-11(太平洋/ Pago_pago)到UTC + 14(太平洋/阿皮亚)

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

因此似乎有从UTC-11到UTC + 14的偏移定义的时区。但是,没有UTC-12的时区,即使Baker Island在Lat / Long:0°12'N / 176°29'W,也是UTC-12。好奇。

答案 1 :(得分:0)

这是获取所有时区及其缩写的最简单方法。 P.S并非所有时区都有正确的三字母缩写。

 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,以了解更多信息。