Skip to content

Commit

Permalink
Merge pull request #390 from YenchangChan/main
Browse files Browse the repository at this point in the history
fix(proto): DateTime/DateTime64 with '1970-01-01 00:00:00 UTC'
  • Loading branch information
ernado authored Mar 8, 2024
2 parents 0585bc2 + 7b55cdd commit 470e9f1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
3 changes: 0 additions & 3 deletions proto/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ func ToDateTime(t time.Time) DateTime {

// Time returns DateTime as time.Time.
func (d DateTime) Time() time.Time {
if d == 0 {
return time.Time{}
}
// https://clickhouse.com/docs/en/sql-reference/data-types/datetime/#usage-remarks
// ClickHouse stores UTC timestamps that are timezone-agnostic.
return time.Unix(int64(d), 0)
Expand Down
3 changes: 0 additions & 3 deletions proto/datetime64.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ func ToDateTime64(t time.Time, p Precision) DateTime64 {

// Time returns DateTime64 as time.Time.
func (d DateTime64) Time(p Precision) time.Time {
if d == 0 {
return time.Time{}
}
nsec := int64(d) * p.Scale()
return time.Unix(nsec/1e9, nsec%1e9)
}
17 changes: 16 additions & 1 deletion proto/datetime64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestDateTime64_Time(t *testing.T) {
} {
t.Run(p.Duration().String(), func(t *testing.T) {
for _, v := range []time.Time{
{}, // zero time
time.Unix(0, 0).UTC(), // zero time
time.Unix(1546290000, 0).UTC(),
} {
d := ToDateTime64(v, p)
Expand All @@ -28,6 +28,21 @@ func TestDateTime64_Time(t *testing.T) {
assert.True(t, p.Valid())
}
})

t.Run("Zero_"+p.Duration().String(), func(t *testing.T) {
t1 := time.Time{}
t2 := time.Unix(0, 0).UTC()
d1 := ToDateTime64(t1, p)
d2 := ToDateTime64(t2, p)
vt1 := d1.Time(p)
vt2 := d2.Time(p)

assert.True(t, t1.IsZero())
assert.False(t, t2.IsZero())
assert.Equal(t, d1, d2)
assert.Equal(t, vt1.Unix(), int64(0))
assert.Equal(t, vt2.Unix(), int64(0))
})
}
t.Run("Duration", func(t *testing.T) {
assert.Equal(t, time.Second, PrecisionSecond.Duration(), "sec")
Expand Down
21 changes: 20 additions & 1 deletion proto/datetime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestDateTime_Time(t *testing.T) {
func TestDateTime_ToDateTime(t *testing.T) {
t.Run("Ok", func(t *testing.T) {
v := time.Unix(1546290000, 0).UTC()
d := ToDateTime(v)
Expand All @@ -19,3 +19,22 @@ func TestDateTime_Time(t *testing.T) {
assert.Equal(t, int32(0), int32(d))
})
}

func TestDateTime_Time(t *testing.T) {
t.Run("OK", func(t *testing.T) {
d := DateTime(1546290000)
assert.Equal(t, d.Time().Unix(), int64(1546290000))
})

t.Run("Zero", func(t *testing.T) {
d := DateTime(0)
assert.Equal(t, d.Time().Unix(), int64(0))
})

t.Run("IsZero", func(t *testing.T) {
d1 := DateTime(0)
d2 := time.Time{}
assert.Equal(t, d1.Time().IsZero(), false)
assert.Equal(t, d2.IsZero(), true)
})
}

0 comments on commit 470e9f1

Please sign in to comment.