iOS Development - Associating a city with a NSTimezone Offline

365 views Asked by At

Hope your all doing well.

So I am in the process of developing a timezone iOS application. The application requires that we have a list of high population cities, and that when a user selects one, it determines the Olson Timezone ID necessary for constructing a NSTimeZone object (which we then use for NSDate conversions).

I've been able to obtain a list of cities and their corresponding lat and lng, which I planned to feed into Geoname's Timezone service endpoint, in order to determine the aforementioned Olson timezone ID for each city.

The problem is that the amount of requests I am going to need to make is huge, and I'm wondering if there isn't a more simple solution to my problem. Any suggestions/ideas would be greatly appreciated.

Thanks everyone!

2

There are 2 answers

1
rmaddy On

You should build the city/timezone mapping just once (in development) and then supply the pre-built mapping with the app. Then the app never needs to do any Internet lookup of any kind.

You could also put the file on your own server and have the app check for updates once in a while.

1
Nebjezus On

For those who come across the same issue, here is how I ended up solving the problem.

I downloaded a tab delimitated list from Geonames.org that contained a list of cities, which also contained the Olson Timezone ID for each city as well. There are a few different lists you can download from Geoname's export dump link (seen below), but I went with the cities5000.zip list so as to have a fairly comprehensive list of cities without being too bloated.

You can find the download links and information regarding the lists at the link below: http://download.geonames.org/export/dump/readme.txt

Because the list contained a lot of unnecessary information (such as lat and lng coordinates), and because I needed the data to be in a .plist format so as to be easily used in the iOS application, I wrote a simple Python script that would extract the city names and timezones from the list, sort them alphabetically based on the city names, and then convert to the .plist format.

For those interested, the python script is as follows:

import json
import httplib
import os

cities = ()
rows = []
# Open up the tab delim list from Geonames.org
with open("cities15000.txt") as file:
    lines = file.readlines()

# Reading each line in the list
for line in lines:
    comps = line.split('\t')
    city = comps[1].strip()
    timezone = comps[17].strip()

    # Make sure there are no duplicates
    if not city in cities:  
        cities = cities + (city,)
        row = {'city':city,'timezone':timezone}
        rows = rows + [row,]

# Sort the rows based on the city name  
def cmp(a,b):
    if a['city'] > b['city']:
        return 1
    elif a['city'] == b['city']:
        return 0
    else:
        return -1
rows.sort(cmp)


# Convert the array to json and then to plist
jsonString = json.dumps(rows)

with open("cities.json", "w") as jsonFile:
    jsonFile.write(jsonString)
os.system('plutil -convert xml1 cities.json -o cities.plist')