My adventures in cities and dates and time
A work-in-progress date & time link:
A few hours of coding, right?
I found GeoNames!
(Do you know of any alternatives?)
time
packageSingapore-SG, Slough-England-GB, Albany-New_York-US
alternatenames
for cities, admin1 and country so I could later implement fuzzy searchalternatenames
sometimes have very similar or duplicates, I filtered some outfunc readCities(f string, countries map[string]data.Country, admin1s map[string]data.Admin1) (map[string]*data.City, error) {
file, err := os.Open(f)
...
r := csv.NewReader(file)
r.Comma = '\t'
r.Comment = '#'
m := make(map[string]*data.City)
for {
...
name := record[1]
ref := normalizeName(record[2])
alternateNames, err := limitNames(name, splitNames(record[3]))
...
admin1Code := record[10]
countryRef := record[8]
population, err := strconv.ParseUint(record[14], 10, 64)
...
timezone := record[17]
...
country := countries[countryRef]
admin1 := admin1s[countryRef+"."+admin1Code]
eref := extendRef(ref, admin1.Ref, country.Ref)
...
c := &**data.City{
Ref: ref,
Name: name,
AlternateNames: alternateNames,
Timezone: timezone,
Population: population,
Admin1: admin1,
Country: country,
}**
...
}
}
func main() {
admin1s, err := readAdmin1Divisions("../third-party/admin1CodesASCII.txt")
...
countries, err := readCountries("../third-party/countryInfo.txt")
...
cities, err := readCities("../third-party/cities15000.txt", countries, admin1s)
...
b, err := json.Marshal(cities)
...
err = ioutil.WriteFile("../data/cities.json", b, 0644)
}
Now I have an ASCII-based key for every city, and their alternate names and timezone!
{
...
"Albany-Georgia-US": {
"n": "Albany",
"an": [
"City of Opportunity",
"albany jarjya",
"Олбани",
"ao er ba ni",
"olbeoni",
"orubani",
"albani"
],
"t": "America/New_York",
"p": 74843,
"a1": {
"n": "Georgia"
},
"c": {
"r": "US",
"n": "United States"
}
},
...
"Singapore-SG": {
"n": "Singapore",
"an": [
"Σιγκαπούρη",
"prathes singkhpor",
"Сингапур",
"Singapore City",
"sing-gapoleu",
"xing jia po",
"Sîn-kâ-po",
"Sinkapoure",
"singkh por",
"shingaporu",
"Sin-ka-po"
],
"t": "Asia/Singapore",
"p": 3547809,
"a1": {
"n": ""
},
"c": {
"r": "SG",
"n": "Singapore"
}
},
...
}
...
// Parse time portion
var t time.Time
timeString := "2022-07-27T00:00Z"
...
for _, f := range timeFormats {
t, err = **time.Parse(f, timeString)**
if err == nil {
break
}
}
...
loc, err := **time.LoadLocation("Asia/Singapore")**
...
timeInSingapore := t.In(loc) // So easy!
log.Printf("%s", timeInSingapore)
import _ “time/tzdata”
or go build -tags timetzdata
A work-in-progress date & time link:
Why Go:
Future improvements:
(I’m really slowly building it)