Skip to content

Commit

Permalink
fix timestamp serialization zero data <-> zeroTimestamp
Browse files Browse the repository at this point in the history
Changes:
1) Unmarshalling `zero data` into `time.Time` return `time.Time{}` before, now returns `zeroTimestamp`
2) Marshalling the `0001-1-1T00:00:00Z` value of the `time.Time` return `zero data` before, now returns a data that is equivalent to the difference between `0001-1-1T00:00:00Z` and `1970-1-1T00:00:00Z`
3) Tests fixed
  • Loading branch information
illia-li committed Dec 13, 2024
1 parent 8e52b3c commit 5f6e337
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 18 deletions.
6 changes: 4 additions & 2 deletions cassandra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2075,7 +2075,9 @@ func TestNilInQuery(t *testing.T) {

// Don't initialize time.Time bind variable if cassandra timestamp column is empty
func TestEmptyTimestamp(t *testing.T) {
zeroTimestamp := time.UnixMilli(0).UTC()
session := createSession(t)

defer session.Close()

if err := createTable(session, "CREATE TABLE gocql_test.test_empty_timestamp (id int, time timestamp, num int, PRIMARY KEY (id))"); err != nil {
Expand All @@ -2092,8 +2094,8 @@ func TestEmptyTimestamp(t *testing.T) {
t.Fatalf("failed to select with err: %v", err)
}

if !timeVal.IsZero() {
t.Errorf("time.Time bind variable should still be empty (was %s)", timeVal)
if !timeVal.Equal(zeroTimestamp) {
t.Errorf("time.Time bind variable should be zero (was %s)", timeVal)
}
}

Expand Down
3 changes: 0 additions & 3 deletions serialization/timestamp/marshal_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ func EncTime(v time.Time) ([]byte, error) {
if v.After(maxTimestamp) || v.Before(minTimestamp) {
return nil, fmt.Errorf("failed to marshal timestamp: the (%T)(%s) value should be in the range from -292275055-05-16T16:47:04.192Z to 292278994-08-17T07:12:55.807", v, v.Format(time.RFC3339Nano))
}
if v.IsZero() {
return make([]byte, 0), nil
}
ms := v.Unix()*1e3 + int64(v.Nanosecond())/1e6
return []byte{byte(ms >> 56), byte(ms >> 48), byte(ms >> 40), byte(ms >> 32), byte(ms >> 24), byte(ms >> 16), byte(ms >> 8), byte(ms)}, nil
}
Expand Down
5 changes: 3 additions & 2 deletions serialization/timestamp/unmarshal_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func DecTime(p []byte, v *time.Time) error {
}
switch len(p) {
case 0:
*v = time.Time{}
*v = zeroTimestamp
case 8:
*v = decTime(p)
default:
Expand All @@ -73,7 +73,8 @@ func DecTimeR(p []byte, v **time.Time) error {
if p == nil {
*v = nil
} else {
*v = new(time.Time)
val := zeroTimestamp
*v = &val
}
case 8:
val := decTime(p)
Expand Down
15 changes: 4 additions & 11 deletions tests/serialization/marshal_16_timestamp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,7 @@ func TestMarshalsTimestamp(t *testing.T) {
},
}

zeroTime := time.Unix(0, 0).UTC()

// The `time` package have a speciality - values `time.Time{}` and `time.Unix(0,0).UTC()` are different
// The old unmarshal function unmarshalls `nil` and `zero` data into `time.Time{}`, but data with zeros into `time.Unix(0,0).UTC()`
brokenTime := serialization.GetTypes(time.Time{}, &time.Time{})
_ = brokenTime
zeroTimestamp := time.Unix(0, 0).UTC()

for _, tSuite := range testSuites {
marshal := tSuite.marshal
Expand All @@ -62,23 +57,21 @@ func TestMarshalsTimestamp(t *testing.T) {
serialization.PositiveSet{
Data: nil,
Values: mod.Values{
int64(0), zeroTime,
int64(0), zeroTimestamp,
}.AddVariants(mod.CustomType),
BrokenUnmarshalTypes: brokenTime,
}.Run("[nil]unmarshal", t, nil, unmarshal)

serialization.PositiveSet{
Data: make([]byte, 0),
Values: mod.Values{
int64(0), zeroTime,
int64(0), zeroTimestamp,
}.AddVariants(mod.All...),
BrokenUnmarshalTypes: brokenTime,
}.Run("[]unmarshal", t, nil, unmarshal)

serialization.PositiveSet{
Data: []byte("\x00\x00\x00\x00\x00\x00\x00\x00"),
Values: mod.Values{
int64(0), zeroTime,
int64(0), zeroTimestamp,
}.AddVariants(mod.All...),
}.Run("zeros", t, marshal, unmarshal)

Expand Down

0 comments on commit 5f6e337

Please sign in to comment.