Files
2026-12-01 21:25:01 +01:00

38 lines
1.7 KiB
Go

package repository
import (
"testing"
"time"
)
// TestParseStoredTime checks the stored time forms read directly give the
// same instant as the driver forms, and that other text falls back to them
func TestParseStoredTime(t *testing.T) {
cases := map[string]time.Time{
"2026-11-13 10:43:50.023172473+00:00": time.Date(2026, 11, 13, 10, 43, 50, 23172473, time.UTC),
"2026-11-13T10:43:50.023172473Z": time.Date(2026, 11, 13, 10, 43, 50, 23172473, time.UTC),
"2026-11-13T10:43:50Z": time.Date(2026, 11, 13, 10, 43, 50, 0, time.UTC),
"2026-11-13 10:43:50+00:00": time.Date(2026, 11, 13, 10, 43, 50, 0, time.UTC),
"2026-11-13 10:43:50.5+02:00": time.Date(2026, 11, 13, 8, 43, 50, 500000000, time.UTC),
"2026-11-13 10:43:50.5-01:30": time.Date(2026, 11, 13, 12, 13, 50, 500000000, time.UTC),
"2026-11-13 10:43:50": time.Date(2026, 11, 13, 10, 43, 50, 0, time.UTC),
"2026-11-13T10:43:50.123": time.Date(2026, 11, 13, 10, 43, 50, 123000000, time.UTC),
// not read directly, the driver forms read them
"2026-11-13 10:43": time.Date(2026, 11, 13, 10, 43, 0, 0, time.UTC),
"2026-11-13": time.Date(2026, 11, 13, 0, 0, 0, 0, time.UTC),
"": {},
"not a time at all!!": {},
"2026-02-30 10:00:00": {},
}
for in, want := range cases {
if got := parseStoredTime([]byte(in)); !got.Equal(want) {
t.Fatalf("parseStoredTime(%q) = %v, want %v", in, got, want)
}
}
for _, bad := range []string{"2026-11-13 10:43:50.", "2026-11-13 10:43:50.1234567890Z", "2026-11-13 10:43:50+0200", "2026-13-13 10:43:50"} {
if _, ok := parseStoredTimeFast([]byte(bad)); ok {
t.Fatalf("parseStoredTimeFast(%q) must refuse it", bad)
}
}
}