From 44258fe51449699027834be2e35147aee30705b2 Mon Sep 17 00:00:00 2001 From: John Wang Date: Sun, 19 Nov 2023 20:59:40 -0800 Subject: [PATCH] dep: `time/timezone`: externalize code using `github.com/tkuchiki/go-timezone` --- go.mod | 1 - go.sum | 2 - time/timezone/abbreviations.go | 69 ----------------------------- time/timezone/abbreviations_test.go | 36 --------------- 4 files changed, 108 deletions(-) delete mode 100644 time/timezone/abbreviations.go delete mode 100644 time/timezone/abbreviations_test.go diff --git a/go.mod b/go.mod index 50e4511f..1bc55001 100644 --- a/go.mod +++ b/go.mod @@ -23,7 +23,6 @@ require ( github.com/martinlindhe/base36 v1.1.1 github.com/microcosm-cc/bluemonday v1.0.26 github.com/shopspring/decimal v1.3.1 - github.com/tkuchiki/go-timezone v0.2.2 github.com/valyala/quicktemplate v1.7.0 github.com/zhuyie/golzf v0.0.0-20161112031142-8387b0307ade golang.org/x/crypto v0.15.0 diff --git a/go.sum b/go.sum index 0f661022..a077f80e 100644 --- a/go.sum +++ b/go.sum @@ -80,8 +80,6 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/tkuchiki/go-timezone v0.2.2 h1:MdHR65KwgVTwWFQrota4SKzc4L5EfuH5SdZZGtk/P2Q= -github.com/tkuchiki/go-timezone v0.2.2/go.mod h1:oFweWxYl35C/s7HMVZXiA19Jr9Y0qJHMaG/J2TES4LY= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/fasthttp v1.30.0/go.mod h1:2rsYD01CKFrjjsvFxx75KlEUNpWNBY9JWD3K/7o2Cus= diff --git a/time/timezone/abbreviations.go b/time/timezone/abbreviations.go deleted file mode 100644 index ae493307..00000000 --- a/time/timezone/abbreviations.go +++ /dev/null @@ -1,69 +0,0 @@ -package timezone - -import ( - "fmt" - "strings" - "time" - - timezone "github.com/tkuchiki/go-timezone" -) - -const errFormatInvalidTzAbbreviation = "invalid timezone abbreviation [%s]" - -// LoadLocationByAbbreviation returns a `*time.Location` given a timezone abbreviation and name. -// For example: ("PST", "Pacific Standard Time"). If the abbreviation only represents one timezone -// the timezone name can be omitted. An error is returned if more than one location is present. -// This is a wrapper for `github.com/tkuchiki/go-timezone` -// which defines the timezone abbreviation and names. -func LoadLocationByAbbreviation(tzAbbr, tzName string) (*time.Location, error) { - tzAbbr = strings.ToUpper(strings.TrimSpace(tzAbbr)) - tzName = strings.TrimSpace(tzName) - tz := timezone.New() - if len(tzName) > 0 { - tzi, err := tz.GetTzAbbreviationInfoByTZName(tzAbbr, tzName) - if err != nil { - return nil, err - } else if tzi == nil || strings.TrimSpace(tzi.Name()) == "" { - return nil, fmt.Errorf(errFormatInvalidTzAbbreviation, tzAbbr) - } - return time.FixedZone(tzi.Name(), tzi.Offset()), nil - } - tzis, err := tz.GetTzAbbreviationInfo(tzAbbr) - if err != nil { - return nil, err - } - switch len(tzis) { - case 0: - return nil, fmt.Errorf(errFormatInvalidTzAbbreviation, tzAbbr) - case 1: - return time.FixedZone(tzis[0].Name(), tzis[0].Offset()), nil - default: - return nil, timezone.ErrAmbiguousTzAbbreviations - } -} - -// LoadLocationsByAbbreviation loads all the matching locations by timezone -// abbreviation and name. Errors are returned as an empty slice. -func LoadLocationsByAbbreviation(tzAbbr, tzName string) []*time.Location { - locs := []*time.Location{} - tzAbbr = strings.ToUpper(strings.TrimSpace(tzAbbr)) - tzName = strings.TrimSpace(tzName) - tz := timezone.New() - if len(tzName) > 0 { - tzi, err := tz.GetTzAbbreviationInfoByTZName(tzAbbr, tzName) - if err != nil { - return []*time.Location{} - } else if tzi == nil || strings.TrimSpace(tzi.Name()) == "" { - return []*time.Location{} - } - return append(locs, time.FixedZone(tzi.Name(), tzi.Offset())) - } - tzis, err := tz.GetTzAbbreviationInfo(tzAbbr) - if err != nil { - return locs - } - for _, tzi := range tzis { - locs = append(locs, time.FixedZone(tzi.Name(), tzi.Offset())) - } - return locs -} diff --git a/time/timezone/abbreviations_test.go b/time/timezone/abbreviations_test.go deleted file mode 100644 index a9f29424..00000000 --- a/time/timezone/abbreviations_test.go +++ /dev/null @@ -1,36 +0,0 @@ -package timezone - -import ( - "testing" -) - -var locationTzAbbreviationAndNameTests = []struct { - tzAbbrReq string - tzNameReq string - tzNameResp string - tzOffet int - isErr bool -}{ - {"PDT", "Pacific Daylight Time", "Pacific Daylight Time", -7 * 60, false}, - {"PST", "Pacific Standard Time", "Pacific Standard Time", -8 * 60, false}, - {"CAT", "", "Central Africa Time", 60, false}, - {"CAT", "Central Africa Time", "Central Africa Time", 60, false}, - {"CET", "", "Central European Time/Central European Standard Time", 60, false}, -} - -// TestLoadLocationByAbbreviation tests retrieving time zones. -func TestLoadLocationByAbbreviation(t *testing.T) { - for _, tt := range locationTzAbbreviationAndNameTests { - loc, err := LoadLocationByAbbreviation(tt.tzAbbrReq, tt.tzNameReq) - if err != nil { - if tt.isErr { - continue - } else { - t.Errorf("timezone.LoadLocationFromAbbreviation(\"%s\", \"%s\") Error [%s]", tt.tzAbbrReq, tt.tzNameReq, err.Error()) - } - } - if loc.String() != tt.tzNameResp { - t.Errorf("timezone.LoadLocationFromAbbreviation(\"%s\", \"%s\") Want [%s] Got [%s]", tt.tzAbbrReq, tt.tzNameReq, tt.tzNameResp, loc.String()) - } - } -}