mirror of
https://github.com/bitechdev/ResolveSpec.git
synced 2026-09-06 16:32:35 +00:00
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
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:
@@ -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 {
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user