github.com/haraldrudell/parl@v0.4.176/short_test.go (about) 1 /* 2 © 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/) 3 ISC License 4 */ 5 6 package parl 7 8 import ( 9 "testing" 10 "time" 11 12 "github.com/haraldrudell/parl/perrors" 13 ) 14 15 const ( 16 iANASweetHomeSanFrancisco = "America/Los_Angeles" 17 timeZonePST = "PST" 18 offsetPSTh = -8 19 ) 20 21 var locCaliforniaFixture = func() (loc *time.Location) { 22 var err error 23 24 // get a known time for a known location 25 if loc, err = time.LoadLocation(iANASweetHomeSanFrancisco); err != nil { 26 panic(perrors.Errorf("time.LoadLocation: %w", err)) 27 } 28 return loc 29 }() 30 31 var timeFixture = func() (t time.Time) { 32 t = time.Date(2022, time.Month(1), 1, 0, 0, 0, 0, locCaliforniaFixture) 33 34 // verify time zone 35 var name string 36 var offsetS int 37 name, offsetS = t.Zone() 38 if name != timeZonePST { 39 panic(perrors.Errorf("time zone abbreviation: %s exp %s", name, timeZonePST)) 40 } 41 expOffset := offsetPSTh * int(time.Hour/time.Second) 42 if offsetS != expOffset { 43 panic(perrors.Errorf("time zone abbreviation: %d exp %d", offsetS, expOffset)) 44 } 45 return t 46 }() 47 48 func TestShort(t *testing.T) { 49 exp := "220101_00:00:00-08" 50 input := timeFixture 51 actual := Short(input) 52 53 if actual != exp { 54 t.Errorf("ptime.Short %q exp %q", actual, exp) 55 } 56 }