Your approach is wrong. A country can have several time zones, for example, the USA and Russia. Due to daylight saving time (DST), the time zone may have more than once, for example, in Hungary. Hungary is UTC +1: 00, as well as UTC + 2: 00 for DST.
For each location where you want local time for a given UTC time, use the IANA time zone location (tzdata). For example,
package main import ( "fmt" "time" ) func main() { utc := time.Now().UTC() fmt.Println(utc) local := utc location, err := time.LoadLocation("Europe/Budapest") if err == nil { local = local.In(location) } fmt.Println("UTC", utc.Format("15:04"), local.Location(), local.Format("15:04")) local = utc location, err = time.LoadLocation("America/Los_Angeles") if err == nil { local = local.In(location) } fmt.Println("UTC", utc.Format("15:04"), local.Location(), local.Format("15:04")) }
Output:
2014-08-14 23:57:09.151377514 +0000 UTC UTC 23:57 Europe/Budapest 01:57 UTC 23:57 America/Los_Angeles 16:57
Literature:
IANA Time Zone Database
tz database
tz database time zones
Timezone
Time in Hungary
peterSO
source share