diff --git a/proto/datetime.go b/proto/datetime.go index 4917339e..a7f48e33 100644 --- a/proto/datetime.go +++ b/proto/datetime.go @@ -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) diff --git a/proto/datetime64.go b/proto/datetime64.go index 6d2a9997..f60ba49b 100644 --- a/proto/datetime64.go +++ b/proto/datetime64.go @@ -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) } diff --git a/proto/datetime64_test.go b/proto/datetime64_test.go index def009c6..322b0615 100644 --- a/proto/datetime64_test.go +++ b/proto/datetime64_test.go @@ -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) @@ -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") diff --git a/proto/datetime_test.go b/proto/datetime_test.go index 9d9d4798..a02a9838 100644 --- a/proto/datetime_test.go +++ b/proto/datetime_test.go @@ -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) @@ -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) + }) +}