Skip to content

Commit

Permalink
Adds support for zero value time for date types.
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinJoiner committed Mar 8, 2024
1 parent 470e9f1 commit 8eabc79
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 23 deletions.
3 changes: 3 additions & 0 deletions proto/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ func (d Date) String() string {

// ToDate returns Date of time.Time.
func ToDate(t time.Time) Date {
if t.IsZero() {
return 0
}
_, offset := t.Zone()
return Date((t.Unix() + int64(offset)) / secInDay)
}
Expand Down
3 changes: 3 additions & 0 deletions proto/date32.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ func (d Date32) String() string {

// ToDate32 returns Date32 of time.Time.
func ToDate32(t time.Time) Date32 {
if t.IsZero() {
return 0
}
_, offset := t.Zone()
return Date32((t.Unix() + int64(offset)) / secInDay)
}
Expand Down
46 changes: 35 additions & 11 deletions proto/date32_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,44 @@ func TestDate32_Time(t *testing.T) {
}

func TestToDate32(t *testing.T) {
const secInMinute = 60
const secInHour = 60 * secInMinute
for _, tc := range []struct {
Value string
name string
Value time.Time
expected *Date32
}{
{Value: "2006-01-02T06:04:03+07:00"},
{Value: "2008-01-02T06:44:15+03:00"},
{Value: "2009-01-01T06:03:31+12:00"},
{Value: "2006-12-31T22:04:41-06:30"},
{
name: "2006-01-02T06:04:03+07:00",
Value: time.Date(2006, 1, 2, 6, 4, 3, 0, time.FixedZone("UTC+7", 7*secInHour)),
},
{
name: "2008-01-02T06:44:15+03:00",
Value: time.Date(2008, 1, 2, 6, 44, 15, 0, time.FixedZone("UTC+3", 3*secInHour)),
},
{
name: "2009-01-01T06:03:31+12:00",
Value: time.Date(2009, 1, 1, 6, 3, 31, 0, time.FixedZone("UTC+12", 12*secInHour)),
},
{
name: "2006-12-31T22:04:41-06:30",
Value: time.Date(2006, 12, 31, 22, 4, 41, 0, time.FixedZone("UTC-6:30", -6*secInHour-30*secInMinute)),
},
{
name: "zero value",
Value: time.Time{},
expected: new(Date32),
},
} {
t.Run(tc.Value, func(t *testing.T) {
v, err := time.Parse(time.RFC3339, tc.Value)
require.NoError(t, err)
d := ToDate32(v)
expected := NewDate32(v.Year(), v.Month(), v.Day())
assert.Equal(t, v.Format(DateLayout), d.String())
t.Run(tc.name, func(t *testing.T) {
d := ToDate32(tc.Value)
var expected Date32
if tc.expected != nil {
expected = *tc.expected
} else {
expected = NewDate32(tc.Value.Year(), tc.Value.Month(), tc.Value.Day())
assert.Equal(t, tc.Value.Format(DateLayout), d.String())
}
assert.Equal(t, expected.String(), d.String())
assert.Equal(t, expected, d)
})
Expand Down
47 changes: 35 additions & 12 deletions proto/date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestDate_Time(t *testing.T) {
Expand Down Expand Up @@ -36,20 +35,44 @@ func TestDate_Time(t *testing.T) {
}

func TestToDate(t *testing.T) {
const secInMinute = 60
const secInHour = 60 * secInMinute
for _, tc := range []struct {
Value string
name string
Value time.Time
expected *Date
}{
{Value: "2006-01-02T06:04:03+07:00"},
{Value: "2008-01-02T06:44:15+03:00"},
{Value: "2009-01-01T06:03:31+12:00"},
{Value: "2006-12-31T22:04:41-06:30"},
{
name: "2006-01-02T06:04:03+07:00",
Value: time.Date(2006, 1, 2, 6, 4, 3, 0, time.FixedZone("UTC+7", 7*secInHour)),
},
{
name: "2008-01-02T06:44:15+03:00",
Value: time.Date(2008, 1, 2, 6, 44, 15, 0, time.FixedZone("UTC+3", 3*secInHour)),
},
{
name: "2009-01-01T06:03:31+12:00",
Value: time.Date(2009, 1, 1, 6, 3, 31, 0, time.FixedZone("UTC+12", 12*secInHour)),
},
{
name: "2006-12-31T22:04:41-06:30",
Value: time.Date(2006, 12, 31, 22, 4, 41, 0, time.FixedZone("UTC-6:30", -6*secInHour-30*secInMinute)),
},
{
name: "zero value",
Value: time.Time{},
expected: new(Date),
},
} {
t.Run(tc.Value, func(t *testing.T) {
v, err := time.Parse(time.RFC3339, tc.Value)
require.NoError(t, err)
d := ToDate(v)
expected := NewDate(v.Year(), v.Month(), v.Day())
assert.Equal(t, v.Format(DateLayout), d.String())
t.Run(tc.name, func(t *testing.T) {
d := ToDate(tc.Value)
var expected Date
if tc.expected != nil {
expected = *tc.expected
} else {
expected = NewDate(tc.Value.Year(), tc.Value.Month(), tc.Value.Day())
assert.Equal(t, tc.Value.Format(DateLayout), d.String())
}
assert.Equal(t, expected.String(), d.String())
assert.Equal(t, expected, d)
})
Expand Down

0 comments on commit 8eabc79

Please sign in to comment.