Skip to content

Commit 6b948ab

Browse files
authored
feat: Support conversion of Unix timestamps in timestamptz (#570)
This adds support for Unix timestamps to the Timestamptz type. I think it's reasonable to expect that an integer type is to be interpreted as unix time, and a custom resolver should be used if this is not the case.
1 parent a643ddf commit 6b948ab

2 files changed

Lines changed: 9 additions & 0 deletions

File tree

schema/timestamptz.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ func (dst *Timestamptz) Set(src any) error {
7676
}
7777

7878
switch value := src.(type) {
79+
case int:
80+
*dst = Timestamptz{Time: time.Unix(int64(value), 0), Status: Present}
81+
case int64:
82+
*dst = Timestamptz{Time: time.Unix(value, 0), Status: Present}
83+
case uint64:
84+
*dst = Timestamptz{Time: time.Unix(int64(value), 0), Status: Present}
7985
case time.Time:
8086
*dst = Timestamptz{Time: value, Status: Present}
8187
case *time.Time:

schema/timestamptz_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ func TestTimestamptzSet(t *testing.T) {
2525
{source: time.Date(2000, 1, 1, 0, 0, 0, 0, time.Local), result: Timestamptz{Time: time.Date(2000, 1, 1, 0, 0, 0, 0, time.Local), Status: Present}},
2626
{source: time.Date(2000, 1, 1, 0, 0, 1, 0, time.Local), result: Timestamptz{Time: time.Date(2000, 1, 1, 0, 0, 1, 0, time.Local), Status: Present}},
2727
{source: time.Date(2200, 1, 1, 0, 0, 0, 0, time.Local), result: Timestamptz{Time: time.Date(2200, 1, 1, 0, 0, 0, 0, time.Local), Status: Present}},
28+
{source: int(time.Date(2000, 1, 1, 0, 0, 0, 0, time.Local).Unix()), result: Timestamptz{Time: time.Date(2000, 1, 1, 0, 0, 0, 0, time.Local), Status: Present}},
29+
{source: uint64(time.Date(2000, 1, 1, 0, 0, 0, 0, time.Local).Unix()), result: Timestamptz{Time: time.Date(2000, 1, 1, 0, 0, 0, 0, time.Local), Status: Present}},
30+
{source: time.Date(2000, 1, 1, 0, 0, 0, 0, time.Local).Unix(), result: Timestamptz{Time: time.Date(2000, 1, 1, 0, 0, 0, 0, time.Local), Status: Present}},
2831
{source: _time(time.Date(1970, 1, 1, 0, 0, 0, 0, time.Local)), result: Timestamptz{Time: time.Date(1970, 1, 1, 0, 0, 0, 0, time.Local), Status: Present}},
2932
{source: Infinity, result: Timestamptz{InfinityModifier: Infinity, Status: Present}},
3033
{source: NegativeInfinity, result: Timestamptz{InfinityModifier: NegativeInfinity, Status: Present}},

0 commit comments

Comments
 (0)