As part of a recent project I’ve been spending more time then I’d like with time zones. In my case a variety of dates and time zone details are provided by a remote service. Having spent the last few years solving this type of problem with moment.js I was excited to see that NSTimeZone contained many of the same features.
The time zone information is provided in the IANA time zone database format. I was really existed to see that you can create a new NSTimeZone using an IANA time zone name. Below is an example using the Kuala Lumpur time zone.
let timeZone = NSTimeZone(name: "Asia/Kuala_Lumpur") //What is the abbreviation timeZone?.abbreviation //Get the description with everything timeZone?.description
So my first experiment was to parse the IANA time zones into a Swift String Array so that I could write a few Playground tests. For this blog post we will show a few of the time zone names, instead of the full 417 item array.
let IANAtimeZones = ["Europe/Andorra","Asia/Dubai","Asia/Kabul",...]
* You can see the full array here
Next I found that the NSTimeZone method knownTimeZoneNames provides a list of all time zones iOS supports out of the box. This gives me an easy way for me to later compare the IANA time zone database with those supported by iOS. I use the below to get a list of those time zones known by iOS.
let iOStimeZones = NSTimeZone.knownTimeZoneNames()
To make writing any comparison testing alittle easier we create a method called tzExists. This will allow us to compare a provided time zone name against all of those known by iOS. In your production code you will want to cache the knownTimeZoneNames array for performance reasons.
func tzExists(name : String) -> Bool { return NSTimeZone.knownTimeZoneNames().contains(name) }
Now that we have everything setup a simple loop can be used to compare our IANA time zones with those known to iOS.
for tz in IANAtimeZones { if(!tzExists(tz)) { print(tz + " not available") } }
Oddly enough there are differences. It could be I have an incomplete version of the IANA time zone database as iOS has 10 more known time zones then my IANA test harness. Even with these differences the straight forward usage of NSTimeZone solves my problem perfectly.
Additional Resources:
- Playground : A playground which shows the above objects, tests, and methods is available here.
- Gist : The playground is available as a gist here.
Image may be NSFW.
Clik here to view.

Clik here to view.
