github.com/haraldrudell/parl@v0.4.176/ptime/format_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 ptime 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 exp = "2022-01-01T08:00:00.000000000Z" 20 expuS = "2022-01-01T08:00:00.000000Z" 21 expmS = "2022-01-01T08:00:00.000Z" 22 expS = "2022-01-01T08:00:00Z" 23 ) 24 25 var locCaliforniaFixture = func() (loc *time.Location) { 26 var err error 27 28 // get a known time for a known location 29 if loc, err = time.LoadLocation(IANASweetHomeSanFrancisco); err != nil { 30 panic(perrors.Errorf("time.LoadLocation: %w", err)) 31 } 32 return loc 33 }() 34 35 var timeFixture = func() (t time.Time) { 36 t = time.Date(2022, time.Month(1), 1, 0, 0, 0, 0, locCaliforniaFixture) 37 38 // verify time zone 39 var name string 40 var offsetS int 41 name, offsetS = t.Zone() 42 if name != timeZonePST { 43 panic(perrors.Errorf("time zone abbreviation: %s exp %s", name, timeZonePST)) 44 } 45 expOffset := offsetPSTh * int(time.Hour/time.Second) 46 if offsetS != expOffset { 47 panic(perrors.Errorf("time zone abbreviation: %d exp %d", offsetS, expOffset)) 48 } 49 return t 50 }() 51 52 func TestRfc3339nsz(t *testing.T) { 53 actual := Rfc3339nsz(timeFixture) 54 if actual != exp { 55 t.Errorf(".Format(parl.Rfc3339nsz) format: %q actual: %s exp: %s", rfc3339nsz, actual, exp) 56 } 57 } 58 59 func TestRfc3339usz(t *testing.T) { 60 actual := Rfc3339usz(timeFixture) 61 if actual != expuS { 62 t.Errorf(".Format(parl.Rfc3339usz) format: %q actual: %s exp: %s", rfc3339usz, actual, expuS) 63 } 64 } 65 66 func TestRfc3339msz(t *testing.T) { 67 actual := Rfc3339msz(timeFixture) 68 if actual != expmS { 69 t.Errorf(".Format(parl.Rfc3339msz) format: %q actual: %s exp: %s", rfc3339msz, actual, expmS) 70 } 71 } 72 73 func TestRfc3339sz(t *testing.T) { 74 actual := Rfc3339sz(timeFixture) 75 if actual != expS { 76 t.Errorf(".Format(parl.Rfc3339sz) format: %q actual: %s exp: %s", rfc3339sz, actual, expS) 77 } 78 }