Files
weatherreporter/internal/timeutil/parse_test.go

61 lines
1.6 KiB
Go

package timeutil
import (
"testing"
"time"
)
func TestLoadLocationAcceptsChicagoAlias(t *testing.T) {
location, err := LoadLocation("Chicago")
if err != nil {
t.Fatalf("LoadLocation() error = %v", err)
}
if location.String() != "America/Chicago" {
t.Fatalf("location = %q, want America/Chicago", location.String())
}
}
func TestLoadLocationAcceptsWeatherAPITimezones(t *testing.T) {
tests := []string{"Stl", "CDT", "-5", "+09:30"}
for _, tt := range tests {
t.Run(tt, func(t *testing.T) {
if _, err := LoadLocation(tt); err != nil {
t.Fatalf("LoadLocation(%q) error = %v", tt, err)
}
})
}
}
func TestParseLocalDate(t *testing.T) {
location := time.FixedZone("Test", -5*60*60)
got, err := ParseLocalDate("2026-05-29", location)
if err != nil {
t.Fatalf("ParseLocalDate() error = %v", err)
}
if got.Format(DateLayout) != "2026-05-29" {
t.Fatalf("date = %s, want 2026-05-29", got.Format(DateLayout))
}
if got.Location() != location {
t.Fatalf("location = %v, want test location", got.Location())
}
}
func TestParseStormTime(t *testing.T) {
location := time.FixedZone("Test", -5*60*60)
local, err := ParseStormTime("2026-05-29T18:00", location)
if err != nil {
t.Fatalf("ParseStormTime(local) error = %v", err)
}
if local.Location() != location {
t.Fatalf("local location = %v, want test location", local.Location())
}
rfc3339, err := ParseStormTime("2026-05-29T18:00:00-05:00", location)
if err != nil {
t.Fatalf("ParseStormTime(rfc3339) error = %v", err)
}
if rfc3339.Format(time.RFC3339) != "2026-05-29T18:00:00-05:00" {
t.Fatalf("rfc3339 = %s, want preserved offset time", rfc3339.Format(time.RFC3339))
}
}