首页 > 解决方案 > 如何从 NSTimeZone 获取 GNU Lib C TZ 格式输出?

问题描述

我需要将远程时钟的时区信息设置为 iOS 设备上的时区信息。

远程时钟仅支持以下GNU lib C TZ 格式std offset dst [offset],start[/time],end[/time]

例如:EST+5EDT,M3.2.0/2,M11.1.0/2

所以我需要在 Swift 中从NSTimeZone.local时区生成一个类似于上面的字符串。似乎无法访问当前时区规则,因为它们会在IANA TZ 数据库中生成输出。

如果没有在应用程序中缓存 TZ 数据库的本地副本的可怕想法,是否可以做到这一点?

更新:

即使通过其他编程语言,我也找不到任何有用的东西。我能找到的最好的方法本质上是在 linux 中解析 tzfile 并制作我自己的包含信息的 NSDictionary。

标签: iosswiftobjective-cdatetimenstimezone

解决方案


这是一次有趣的探索,主要是因为将数据拟合成正确的格式非常复杂。问题组件:

  • 我们需要适用于给定时区的“当前”TZ 数据库规则。这是一个有点加载的概念,因为:

    1. Darwin 平台实际上并不直接将 TZ 数据库用于大多数应用程序,而是使用 ICU 的时区数据库,它有不同的格式并且更复杂。即使您生成这种格式的字符串,它也不一定描述设备上的实际时间行为

    2. 虽然可以在 iOS 上动态读取和解析 TZ 数据库,但不能保证 TZ 数据库本身以此处所需的格式存储信息。rfc8536,管理时区信息格式的 RFC 对您想要的格式进行了以下说明:

      版本 3 TZif 文件中的 TZ 字符串可以使用以下对 POSIX TZ 字符串的扩展。这些扩展是使用 [POSIX] 的“基本定义”卷的第 8.3 节的术语来描述的。

      示例:<-03>3<-02>,M3.5.0/-2,M10.5.0/-1
      示例:EST5EDT,0/0,J365/25

      在深入研究 iOS TZ 数据库时,我发现一些数据库条目确实以这种格式在文件末尾提供了规则,但它们似乎是少数。您可以动态解析这些,但这可能不值得

    因此,我们需要使用 API 来生成这种格式的字符串。

  • 为了生成在给定日期至少大致正确的“规则”,您需要了解有关该日期前后 DST 转换的信息。这是一个非常棘手的话题,因为 DST 规则一直在变化,而且并不总是像您希望的那样有意义。至少:

    • 北半球的许多时区从春季开始到秋季结束 DST
    • 南半球的许多时区从秋季开始到春季结束 DST
    • 某些时区不遵守 DST(全年采用标准时间)
    • 某些时区不遵守 DST,全年处于白天

    因为规则非常复杂,所以这个答案的其余部分假设您可以生成一个代表特定时间日期的“足够好”的答案,并且愿意在将来某个时间更正时向您的时钟发送更多字符串是需要的。例如,为了描述“现在”,我们将假设根据上一个 DST 转换(如果有)和下一个 DST 转换(如果有)生成规则“足够好”,但这可能不适用于所有情况在许多时区

  • Foundation 以/TimeZone的形式提供 DST 转换信息。然而,令人沮丧的是,没有办法获得有关先前DST 转换的信息,因此我们需要纠正这一点:TimeZone.nextDaylightSavingTimeTransitionTimeZone.nextDaylightSavingTimeTransition(after:)

    • Foundation 的本地化支持(包括日历和时区)直接基于ICU 库,该库在所有 Apple 平台内部提供。ICU确实提供了一种获取有关先前 DST 转换的信息的方法,但 Foundation 只是不将其作为 API 提供,因此我们需要自己公开它

    • ICU 是 Apple 平台上的半私有库。该库保证存在,Xcode 会为您libicucore.tbd提供链接 in <Project> > <Target> > Build Phases > Link Binary with Libraries,但实际的标头和符号不会直接暴露给应用程序。您可以成功链接到libicucore,但您需要在导入 Swift 的 Obj-C 标头中前向声明我们需要的功能

    • 在 Swift 项目的某个地方,我们需要公开以下 ICU 功能:

      #include <stdint.h>
      
      typedef void * _Nonnull UCalendar;
      typedef double UDate;
      typedef int8_t UBool;
      typedef uint16_t UChar;
      
      typedef enum UTimeZoneTransitionType {
          UCAL_TZ_TRANSITION_NEXT,
          UCAL_TZ_TRANSITION_NEXT_INCLUSIVE,
          UCAL_TZ_TRANSITION_PREVIOUS,
          UCAL_TZ_TRANSITION_PREVIOUS_INCLUSIVE,
      } UTimeZoneTransitionType;
      
      typedef enum UCalendarType {
          UCAL_TRADITIONAL,
          UCAL_DEFAULT,
          UCAL_GREGORIAN,
      } UCalendarType;
      
      typedef enum UErrorCode {
          U_ZERO_ERROR = 0,
      } UErrorCode;
      
      UCalendar * _Nullable ucal_open(const UChar *zoneID, int32_t len, const char *locale, UCalendarType type, UErrorCode *status);
      void ucal_setMillis(const UCalendar * _Nonnull cal, UDate date, UErrorCode * _Nonnull status);
      UBool ucal_getTimeZoneTransitionDate(const UCalendar * _Nonnull cal, UTimeZoneTransitionType type, UDate * _Nonnull transition, UErrorCode * _Nonnull status);
      

      这些都是前向声明/常量,因此无需担心实现(因为我们通过链接来获得它libicucore)。

    • UTimeZoneTransitionType您可以在—中看到值,TimeZone.nextDaylightSavingTimeTransition只需ucal_getTimeZoneTransitionDate使用 的值调用UCAL_TZ_TRANSITION_NEXT,因此我们可以通过使用 调用该方法来提供大致相同的功能UCAL_TZ_TRANSITION_PREVIOUS

      extension TimeZone {
          func previousDaylightSavingTimeTransition(before: Date) -> Date? {
              // We _must_ pass a status variable for `ucal_open` to write into, but the actual initial
              // value doesn't matter.
              var status = U_ZERO_ERROR
      
              // `ucal_open` requires the time zone identifier be passed in as UTF-16 code points.
              // `String.utf16` doesn't offer a contiguous buffer for us to pass directly into `ucal_open`
              // so we have to create our own by copying the values into an `Array`, then
              let timeZoneIdentifier = Array(identifier.utf16)
              guard let calendar = Locale.current.identifier.withCString({ localeIdentifier in
                  ucal_open(timeZoneIdentifier, // implicit conversion of Array to a pointer, but convenient!
                            Int32(timeZoneIdentifier.count),
                            localeIdentifier,
                            UCAL_GREGORIAN,
                            &status)
              }) else {
                  // Figure out some error handling here -- we failed to find a "calendar" for this time
                  // zone; i.e., there's no time zone date for this time zone.
                  //
                  // With more enum cases copied from `UErrorCode` you may find a good way to report an
                  // error here if needed. `u_errorName` turns a `UErrorCode` into a string.
                  return nil
              }
      
              // `UCalendar` functions operate on the calendar's current timestamp, so we have to apply
              // `date` to it. `UDate`s are the number of milliseconds which have passed since January 1,
              // 1970, while `Date` offers its time interval in seconds.
              ucal_setMillis(calendar, before.timeIntervalSince1970 * 1000.0, &status)
      
              var result: UDate = 0
              guard ucal_getTimeZoneTransitionDate(calendar, UCAL_TZ_TRANSITION_PREVIOUS, &result, &status) != 0 else {
                  // Figure out some error handling here -- same as above (check status).
                  return nil
              }
      
              // Same transition but in reverse.
              return Date(timeIntervalSince1970: result / 1000.0)
          }
      }
      

因此,有了所有这些,我们可以填写一个粗略的方法来生成您需要的格式的字符串:

extension TimeZone {
    struct Transition {
        let abbreviation: String
        let offsetFromGMT: Int
        let date: Date
        let components: DateComponents

        init(for timeZone: TimeZone, on date: Date, using referenceCalendar: Calendar) {
            abbreviation = timeZone.abbreviation(for: date) ?? ""
            offsetFromGMT = timeZone.secondsFromGMT(for: date)
            self.date = date
            components = referenceCalendar.dateComponents([.month, .weekOfMonth, .weekdayOrdinal, .hour, .minute, .second], from: date)
        }
    }

    func approximateTZEntryRule(on date: Date = Date(), using calendar: Calendar? = nil) -> String? {
        var referenceCalendar = calendar ?? Calendar(identifier: .gregorian)
        referenceCalendar.timeZone = self

        guard let year = referenceCalendar.dateInterval(of: .year, for: date) else {
            return nil
        }

        // If no prior DST transition has ever occurred, we're likely in a time zone which is either
        // standard or daylight year-round. We'll cap the definition here to the very start of the
        // year.
        let previousDSTTransition = Transition(for: self, on: previousDaylightSavingTimeTransition(before: date) ?? year.start, using: referenceCalendar)

        // Same with the following DST transition -- if no following DST transition will ever come,
        // we'll cap it to the end of the year.
        let nextDSTTransition = Transition(for: self, on: nextDaylightSavingTimeTransition(after: date) ?? year.end, using: referenceCalendar)

        let standardToDaylightTransition: Transition
        let daylightToStandardTransition: Transition
        if isDaylightSavingTime(for: date) {
            standardToDaylightTransition = previousDSTTransition
            daylightToStandardTransition = nextDSTTransition
        } else {
            standardToDaylightTransition = nextDSTTransition
            daylightToStandardTransition = previousDSTTransition
        }

        let standardAbbreviation = daylightToStandardTransition.abbreviation
        let standardOffset = formatOffset(daylightToStandardTransition.offsetFromGMT)
        let daylightAbbreviation = standardToDaylightTransition.abbreviation
        let startDate = formatDate(components: standardToDaylightTransition.components)
        let endDate = formatDate(components: daylightToStandardTransition.components)
        return "\(standardAbbreviation)\(standardOffset)\(daylightAbbreviation),\(startDate),\(endDate)"
    }

    /* These formatting functions can be way better. You'll also want to actually cache the
       DateComponentsFormatter somewhere.
     */

    func formatOffset(_ dateComponents: DateComponents) -> String {
        let formatter = DateComponentsFormatter()
        formatter.allowedUnits = [.hour, .minute, .second]
        formatter.zeroFormattingBehavior = .dropTrailing
        return formatter.string(from: dateComponents) ?? ""
    }

    func formatOffset(_ seconds: Int) -> String {
        return formatOffset(DateComponents(second: seconds))
    }

    func formatDate(components: DateComponents) -> String {
        let month = components.month ?? 0
        let week = components.weekOfMonth ?? 0
        let day = components.weekdayOrdinal ?? 0
        let offset = formatOffset(DateComponents(hour: components.hour, minute: components.minute, second: components.second))
        return "M\(month).\(week).\(day)/\(offset)"
    }
}

请注意,这里有很多需要改进的地方,尤其是在清晰度和性能方面。(格式化程序是出了名的昂贵,所以你肯定想缓存它们。)这目前也只产生扩展形式的日期,"Mm.w.d"而不是儒略日,但可以用螺栓固定。该代码还假设将无限规则限制在当前日历年“足够好”,因为这是 GNU C 库文档似乎暗示的,例如始终处于标准/夏令时的时区。(这也不能识别众所周知的时区,如 GMT/UTC,这可能足以写成“GMT”。)

我没有针对各个时区对这段代码进行广泛的测试,上面的代码应该被认为是额外迭代的基础。对于我的时区America/New_York,这会产生"EST-5EDT,M3.3.2/3,M11.2.1/1",乍一看对我来说似乎是正确的,但许多其他边缘情况可能值得探索:

  • 年初/年末的边界条件
  • 给出与DST 转换完全匹配TRANSITION_PREVIOUS的日期(考虑vs. TRANSITION_PREVIOUS_INCLUSIVE
  • 始终为标准/日光的时区
  • 非标准日光/时区偏移

这还有很多,总的来说,我建议尝试找到一种在此设备上设置时间的替代方法(最好使用命名的时区),但这至少可以帮助您入门。


推荐阅读