diff --git a/pkg/spectypes/sql_types.go b/pkg/spectypes/sql_types.go index f9a81d6..37fcd4c 100644 --- a/pkg/spectypes/sql_types.go +++ b/pkg/spectypes/sql_types.go @@ -336,21 +336,21 @@ type ( 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] } 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)) { 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 { if err := t.SqlNull.UnmarshalJSON(b); err != nil { 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 } 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)) { return nil, nil } - return t.Val.Format("2006-01-02T15:04:05"), nil + return t.Val.Format(time.RFC3339), nil } func SqlTimeStampNow() SqlTimeStamp { diff --git a/pkg/spectypes/sql_types_test.go b/pkg/spectypes/sql_types_test.go index 0dbf757..de71c78 100644 --- a/pkg/spectypes/sql_types_test.go +++ b/pkg/spectypes/sql_types_test.go @@ -178,7 +178,7 @@ func TestSqlTimeStamp_JSON(t *testing.T) { if err != nil { t.Fatalf("Marshal failed: %v", err) } - expected := `"2024-01-15T10:30:45"` + expected := `"2024-01-15T10:30:45Z"` if string(data) != expected { t.Errorf("expected %s, got %s", expected, string(data)) }