timezones

.paul.

Well-known member
Joined
May 22, 2007
Messages
212
Programming Experience
1-3
i've got a text file that lists 596 cities (example)

Ankara, 2, Ankara - Turkey
Antananarivo, 3, Antananarivo - Madagascar
Asuncion, -3, Asuncion - Paraguay
Auckland, 13, Auckland - New Zealand
Augusta, -5, Augusta - Maine - U.S.A.
Austin, -6, Austin - Texas - U.S.A.
Atlanta, -5, Atlanta - Georgia - U.S.A.
Athens, 2, Athens - Greece
Baghdad, 3, Baghdad - Iraq
Bangkok, 7, Bangkok - Thailand
Bamako, 0, Bamako - Mali
Banjul, 0, Banjul - Gambia
Barcelona, 1, Barcelona - Spain
Baton Rouge, -6, Baton Rouge - Louisiana - U.S.A.
Beijing, 8, Beijing - China
Beirut, 2, Beirut - Lebanon
Belgrade, 1, Belgrade - Serbia
Belmopan, -6, Belmopan - Belize

it lists city name, current UTC offset, then (again) city name, state (if applicable), and country.

how can i match these cities up to the appropriate timezone?
 
Below you will find a vb.net console application that will read your text file and list the timezone for each city. I used the military timezones as an example, you can change or add them to your liking. (There are actually many different timezones for each UTC.) The program assumes that each line in your file contains three fields separated by commas and that the second field is a integer. (There are actually some timezones that are 1/2 hour changes. This program doesn't take them in to account). This program is very rough, but I hope it helps to get you started.
VB.NET:
[COLOR="Blue"]Imports System.IO

Module TimeZone

    Sub Main()
[COLOR="SeaGreen"]        'this line lets the program know where the file is located
        'you would need to change this to your file location and name.
[/COLOR]        Dim f As FileInfo = New FileInfo("C:\users\tony\documents\cities.txt")
[COLOR="SeaGreen"]        'this line opens the file 
[/COLOR]        Dim sr As StreamReader = f.OpenText()
 [COLOR="SeaGreen"]       'set up a loop to read each line of the file until the end
[/COLOR]        Do While Not sr.EndOfStream
            Dim line As String
            Dim fields As String()
            Dim city As String
            Dim utc As Integer
            Dim country As String
            Dim TimeZone As String
[COLOR="SeaGreen"]            'we read the line
[/COLOR]            line = sr.ReadLine
[COLOR="SeaGreen"]            'we split the line into the three parts divided by the comma
[/COLOR]            fields = line.Split(",")
[COLOR="SeaGreen"]            'we get the city from the first part of the line
[/COLOR]            city = fields(0)
[COLOR="SeaGreen"]            'we get the UTC and convert it to an integer from the second part of the line[/COLOR]
            utc = Convert.ToInt32(fields(1).Trim)
[COLOR="SeaGreen"]            'we get the last part of the line (city, state and country)
[/COLOR]            country = fields(2)
 [COLOR="SeaGreen"]           'we use the utc to see what timezone we are in
[/COLOR]            Select Case utc
                Case 0
                    TimeZone = "Zulu Time Zone"
                Case -12
                    TimeZone = "Yankee Time Zone"
                Case -11
                    TimeZone = "X-Ray Time Zone"
                Case -10
                    TimeZone = "Whiskey Time Zone"
                Case -9
                    TimeZone = "Victor Time Zone"
                Case -8
                    TimeZone = "Uniform Time Zone"
                Case -7
                    TimeZone = "Tango Time Zone"
                Case -6
                    TimeZone = "Sierra Time Zone"
                Case -5
                    TimeZone = "Romeo Time Zone"
                Case -4
                    TimeZone = "Quebec Time Zone"
                Case -3
                    TimeZone = "Papa Time Zone"
                Case -2
                    TimeZone = "Oscar Time Zone"
                Case -1
                    TimeZone = "November Time Zone"
                Case 1
                    TimeZone = "Alpha Time Zone"
                Case 2
                    TimeZone = "Bravo Time Zone"
                Case 3
                    TimeZone = "Charlie Time Zone"
                Case 4
                    TimeZone = "Delta Time Zone"
                Case 5
                    TimeZone = "Echo Time Zone"
                Case 6
                    TimeZone = "Foxtrot Time Zone"
                Case 7
                    TimeZone = "Golf Time Zone"
                Case 8
                    TimeZone = "Hotel Time Zone"
                Case 9
                    TimeZone = "India Time Zone"
                Case 10
                    TimeZone = "Kilo Time Zone"
                Case 11
                    TimeZone = "Lima Time Zone"
                Case 12
                    TimeZone = "Mike Time Zone"
                Case Else
                    TimeZone = "Unknown Time Zone"
            End Select

[COLOR="SeaGreen"]            'finally we write to the console the city, utc and timezone
[/COLOR]            Console.Write("City: {0}", city)
            Console.Write(vbTab)
            Console.Write("UTC: {0}", utc)
            Console.Write(vbTab)
            Console.WriteLine("Time Zone: {0}", TimeZone)
        Loop

    End Sub

End Module[/COLOR]
 
Last edited by a moderator:
Back
Top