fix(spectypes): serialize SqlTimeStamp as RFC3339 with offset
Tests / Unit Tests (push) Failing after 11s
Tests / Integration Tests (push) Failing after 23s
Build , Vet Test, and Lint / Build (push) Successful in 53s
Build , Vet Test, and Lint / Run Vet Tests (1.24.x) (push) Successful in 56s
Build , Vet Test, and Lint / Run Vet Tests (1.23.x) (push) Successful in 56s
Build , Vet Test, and Lint / Lint Code (push) Successful in 1m3s

MarshalJSON and Value now emit time.RFC3339 instead of a naive
YYYY-MM-DDTHH:MM:SS layout, so timezone offset is preserved in JSON
and DB writes. UnmarshalJSON zero-check made offset-agnostic.
This commit is contained in:
2026-09-03 22:14:08 +02:00
parent cbac47e052
commit f841d58c59
2 changed files with 5 additions and 5 deletions
+4 -4
View File
@@ -336,21 +336,21 @@ type (
SqlUUID = SqlNull[uuid.UUID] SqlUUID = SqlNull[uuid.UUID]
) )
// SqlTimeStamp - Timestamp with custom formatting (YYYY-MM-DDTHH:MM:SS). // SqlTimeStamp - Timestamp serialized as RFC3339 with timezone offset.
type SqlTimeStamp struct{ SqlNull[time.Time] } type SqlTimeStamp struct{ SqlNull[time.Time] }
func (t SqlTimeStamp) MarshalJSON() ([]byte, error) { func (t SqlTimeStamp) MarshalJSON() ([]byte, error) {
if !t.Valid || t.Val.IsZero() || t.Val.Before(time.Date(0002, 1, 1, 0, 0, 0, 0, time.UTC)) { if !t.Valid || t.Val.IsZero() || t.Val.Before(time.Date(0002, 1, 1, 0, 0, 0, 0, time.UTC)) {
return []byte("null"), nil return []byte("null"), nil
} }
return []byte(fmt.Sprintf(`"%s"`, t.Val.Format("2006-01-02T15:04:05"))), nil return []byte(fmt.Sprintf(`"%s"`, t.Val.Format(time.RFC3339))), nil
} }
func (t *SqlTimeStamp) UnmarshalJSON(b []byte) error { func (t *SqlTimeStamp) UnmarshalJSON(b []byte) error {
if err := t.SqlNull.UnmarshalJSON(b); err != nil { if err := t.SqlNull.UnmarshalJSON(b); err != nil {
return err return err
} }
if t.Valid && (t.Val.IsZero() || t.Val.Format("2006-01-02T15:04:05") == "0001-01-01T00:00:00") { if t.Valid && (t.Val.IsZero() || t.Val.Year() <= 1) {
t.Valid = false t.Valid = false
} }
return nil return nil
@@ -360,7 +360,7 @@ func (t SqlTimeStamp) Value() (driver.Value, error) {
if !t.Valid || t.Val.IsZero() || t.Val.Before(time.Date(0002, 1, 1, 0, 0, 0, 0, time.UTC)) { if !t.Valid || t.Val.IsZero() || t.Val.Before(time.Date(0002, 1, 1, 0, 0, 0, 0, time.UTC)) {
return nil, nil return nil, nil
} }
return t.Val.Format("2006-01-02T15:04:05"), nil return t.Val.Format(time.RFC3339), nil
} }
func SqlTimeStampNow() SqlTimeStamp { func SqlTimeStampNow() SqlTimeStamp {
+1 -1
View File
@@ -178,7 +178,7 @@ func TestSqlTimeStamp_JSON(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Marshal failed: %v", err) t.Fatalf("Marshal failed: %v", err)
} }
expected := `"2024-01-15T10:30:45"` expected := `"2024-01-15T10:30:45Z"`
if string(data) != expected { if string(data) != expected {
t.Errorf("expected %s, got %s", expected, string(data)) t.Errorf("expected %s, got %s", expected, string(data))
} }