Go中如何将EDT/CEST等时区转为time.Location及获取代表位置
Great question! Let’s tackle your two Go time package questions one by one.
time.Location Go’s standard time.LoadLocation function relies exclusively on IANA time zone identifiers (like Europe/Berlin or America/New_York) rather than abbreviations such as CEST or EDT. The core issue with abbreviations is their ambiguity—for example, CST can stand for Central Standard Time (US), China Standard Time, or Cuba Standard Time—so the standard library doesn’t support direct lookup by abbreviation out of the box.
Here are two practical solutions:
Option 1: Build a Custom Mapping
If you’re working with a limited, unambiguous set of abbreviations, create a simple map to translate them to valid IANA IDs:package main import ( "fmt" "time" ) var tzAbbrevToIANA = map[string]string{ "CEST": "Europe/Berlin", "EDT": "America/New_York", "WET": "Europe/London", // Add more abbreviations as needed for your use case } func getLocationFromAbbrev(abbrev string) (*time.Location, error) { ianaID, exists := tzAbbrevToIANA[abbrev] if !exists { return nil, fmt.Errorf("no IANA mapping found for abbreviation: %s", abbrev) } return time.LoadLocation(ianaID) } func main() { loc, err := getLocationFromAbbrev("CEST") if err != nil { panic(err) } fmt.Println("Resolved location:", loc) // Use loc with time.Time.In(loc) as needed }This works well for controlled environments but won’t handle ambiguous abbreviations or edge cases you haven’t explicitly mapped.
Option 2: Use Third-Party Libraries
For robust, full-featured handling (including resolving ambiguous abbreviations), use third-party libraries that maintain comprehensive mappings between abbreviations and IANA identifiers. These tools parse official IANA time zone data to ensure accuracy.
To retrieve a list of IANA time zones linked to an abbreviation (like WET mapping to Europe/London and Atlantic/Reykjavik), you’ll need a structured mapping—either custom or derived from official data.
Custom Mapping Approach
Define a map that maps abbreviations to slices of IANA IDs:var abbrevToLocations = map[string][]string{ "WET": {"Europe/London", "Atlantic/Reykjavik"}, "CEST": {"Europe/Berlin", "Europe/Paris", "Europe/Madrid"}, "EDT": {"America/New_York", "America/Toronto"}, } func getLocationsForAbbrev(abbrev string) ([]string, error) { locations, exists := abbrevToLocations[abbrev] if !exists { return nil, fmt.Errorf("no locations found for abbreviation: %s", abbrev) } return locations, nil }Parse Official IANA Data
For a complete, up-to-date list, parse thezone.tabfile from the IANA time zone database. This file includes every IANA time zone, its geographic coordinates, and associated country codes. You can extract abbreviations and map them to their corresponding time zones by processing this file (many third-party libraries automate this).
Key Note
Time zone abbreviations only represent a specific offset (e.g., CEST is UTC+2) during part of the year, while IANA identifiers represent full time zones that automatically handle standard and daylight saving time transitions. Whenever possible, using IANA IDs directly is more reliable than relying on abbreviations.
内容的提问来源于stack exchange,提问作者Oleg Sklyar

