From df23a6c25acd8fbaebe17e3d7653168064662742 Mon Sep 17 00:00:00 2001 From: tkuchiki Date: Sat, 16 Mar 2019 00:42:25 +0900 Subject: [PATCH] simplified GetTimezoneAbbreviation --- timezone.go | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/timezone.go b/timezone.go index 7133160..be325d3 100644 --- a/timezone.go +++ b/timezone.go @@ -1182,22 +1182,21 @@ func FixedTimezone(t time.Time, timezone string) (time.Time, error) { return t.In(loc), err } -func GetTimezoneAbbreviation(timezoneName string) (string, error) { - tzShort := "" - - // Loop through all of the timezones and look for a match against the - // provided timezone to find the timezone abbreviation - for tzAbbrev, zonenames := range timezones { - for _, zonename := range zonenames { - if zonename == timezoneName { - tzShort = tzAbbrev - } - } +func GetTimezoneAbbreviation(timezoneName string, dst ...bool) (string, error) { + loc, err := time.LoadLocation(timezoneName) + if err != nil { + return "", err } - if tzShort == "" { - return tzShort, errors.New(fmt.Sprintf("Failed to find timezone abbreviation for: %s", timezoneName)) + var zone string + + if len(dst) == 0 || !dst[0] { + now := time.Now() + zone, _ = time.Date(now.Year(), time.January, 1, 0, 0, 0, 0, time.UTC).In(loc).Zone() + } else { + // considering DST + zone, _ = time.Now().In(loc).Zone() } - return tzShort, nil + return zone, nil }