|
| 1 | +package _189_tv |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | +) |
| 7 | + |
| 8 | +func TestTimeUnmarshal(t *testing.T) { |
| 9 | + tests := []struct { |
| 10 | + name string |
| 11 | + input string |
| 12 | + want time.Time |
| 13 | + }{ |
| 14 | + {"numeric date", `"2026-08-11 10:37:18"`, time.Date(2026, 8, 11, 10, 37, 18, 0, time.FixedZone("", 8*3600))}, |
| 15 | + {"legacy month date", `"Aug 11, 2026 10:37:18 PM"`, time.Date(2026, 8, 11, 22, 37, 18, 0, time.FixedZone("", 8*3600))}, |
| 16 | + {"new format with tz", `"Aug 11, 2026, 10:37:18 PM +08"`, time.Date(2026, 8, 11, 22, 37, 18, 0, time.FixedZone("", 8*3600))}, |
| 17 | + {"new format no tz", `"Aug 11, 2026, 10:37:18 PM"`, time.Date(2026, 8, 11, 22, 37, 18, 0, time.FixedZone("", 8*3600))}, |
| 18 | + {"narrow no-break space (U+202F)", "\"Aug 12, 2026, 12:35:41\u202fAM +08\"", time.Date(2026, 8, 12, 0, 35, 41, 0, time.FixedZone("", 8*3600))}, |
| 19 | + {"no-break space (U+00A0)", "\"Aug 12, 2026, 12:35:41\u00a0AM +08\"", time.Date(2026, 8, 12, 0, 35, 41, 0, time.FixedZone("", 8*3600))}, |
| 20 | + } |
| 21 | + for _, tt := range tests { |
| 22 | + t.Run(tt.name, func(t *testing.T) { |
| 23 | + var tm Time |
| 24 | + if err := tm.Unmarshal([]byte(tt.input)); err != nil { |
| 25 | + t.Fatalf("Unmarshal(%s) error: %v", tt.input, err) |
| 26 | + } |
| 27 | + if !tt.want.Equal(time.Time(tm)) { |
| 28 | + t.Fatalf("Unmarshal(%s) = %v, want %v", tt.input, time.Time(tm), tt.want) |
| 29 | + } |
| 30 | + }) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func TestTimeUnmarshalRejectsInvalid(t *testing.T) { |
| 35 | + var tm Time |
| 36 | + if err := tm.Unmarshal([]byte("Aug 12, 2026, 25:32:44 AM")); err == nil { |
| 37 | + t.Fatal("Unmarshal accepted an invalid time") |
| 38 | + } |
| 39 | +} |
0 commit comments