TimeZone.current on iOS

last updated on 7 December 2024

Where does a value for TimeZone.current (https://developer.apple.com/documentation/foundation/timezone/2293341-current) come from in Swift? I spent some time on checking that this weekend. A list of the time zones changes at least several times a year, it’s not constant. There is this article https://zachholman.com/talk/utc-is-enough-for-everyone-right that explains the “why” for that in a very nice, fun and detailed way. I really recommend reading that article! It’s quite long but super interesting 😀 The time zone list changed on the 4th September 2024 last time (https://www.iana.org/time-zones) so just a bit over 3 months ago. So maybe TimeZone.current makes some network request to check a current version of that database? What if a user’s device isn’t connected to the Internet? What’s a default value then?

Let’s have a look at the Foundation framework’s source code here https://github.com/swiftlang/swift-corelibs-foundation and more specifically saying an NSTimeZone.swift file. So there is this line

_timeZone.abbreviation(for: aDate)

there which – if we follow a chain of calling the functions – calls a C function called __InitTZStrings() from a CFTimeZone.c file later on. And now, what does __InitTZStrings() do?

static void __InitTZStrings(void) {
    __tzZoneInfo = CFSTR(TZDIR);
    __tzDir = TZDIR "zone.tab";
}

It creates a path to a file with the time zone list! That’s it, we found it!! 💃 So there is a constant value for a directory path called TZDIR and its value is the following (you can find it in the same file, CFTimeZone.c):

#define TZDIR	"/usr/share/zoneinfo/"

and then a zone.tab filename is just appended to that path so the answers to the questions that I put at the beginning of this blog post are:

  1. TimeZone.current doesn’t make any network requests to get an updated list of the time zones.
  2. Apple keeps that value in a /usr/share/zoneinfo/zone.tab file on iOS.
  3. Given point 2 – if you don’t update a version of the iOS system on your iPhone – you may have an outdated time zone list on your phone.

We may just try to answer the very last question in this post: what is a .tab format?

The .tab format on iOS refers to the tab-separated values in a text file, represented by the UTTypeTabSeparatedText (https://developer.apple.com/documentation/uniformtypeidentifiers/uttypetabseparatedtext) identifier in Apple’s frameworks. That type of a file contains data where fields are separated by the tab characters (\t). That format is widely supported across the iOS, iPadOS and macOS apps for handling structured data and it’s often used for the spreadsheet exports or data transfers.