Do not panic if we have a timezone that we can't load.

This commit is contained in:
Mihai Parparita 2014-08-18 23:17:29 -07:00
parent 846a4240b2
commit a667873958

View file

@ -29,27 +29,28 @@ func initTimezones() Timezones {
if err != nil { if err != nil {
log.Panicf("Could not parse timezones config %s: %s", configBytes, err.Error()) log.Panicf("Could not parse timezones config %s: %s", configBytes, err.Error())
} }
timezones = make(Timezones, len(config.LocationNames)) timezones = make(Timezones, 0, len(config.LocationNames))
// Use a January date so that UTC offsets are not affected by DST in the northern hemisphere // Use a January date so that UTC offsets are not affected by DST in the northern hemisphere
now := time.Unix(380937600, 0) now := time.Unix(380937600, 0)
for i, locationName := range config.LocationNames { for _, locationName := range config.LocationNames {
if locationName == "" { if locationName == "" {
timezones[i] = Timezone{ timezones = append(timezones, Timezone{
LocationName: locationName, LocationName: locationName,
} })
continue continue
} }
location, err := time.LoadLocation(locationName) location, err := time.LoadLocation(locationName)
if err != nil { if err != nil {
log.Panicf("Location %s could not be loaded: %s", locationName, err.Error()) log.Printf("Location %s could not be loaded: %s", locationName, err.Error())
continue
} }
utcDelta := now.Sub(time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second(), now.Nanosecond(), location)) utcDelta := now.Sub(time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second(), now.Nanosecond(), location))
utcDeltaHours := int(utcDelta.Hours()) utcDeltaHours := int(utcDelta.Hours())
utcDeltaMinutes := int(utcDelta.Minutes()) - 60*utcDeltaHours utcDeltaMinutes := int(utcDelta.Minutes()) - 60*utcDeltaHours
timezones[i] = Timezone{ timezones = append(timezones, Timezone{
LocationName: locationName, LocationName: locationName,
DisplayUTCOffset: fmt.Sprintf("%d:%02d", utcDeltaHours, utcDeltaMinutes), DisplayUTCOffset: fmt.Sprintf("%d:%02d", utcDeltaHours, utcDeltaMinutes),
} })
} }
return timezones return timezones
} }