Skip to content

Commit

Permalink
fix: Postgres DB with non-UTC timezone incorrectly parse in timestamp…
Browse files Browse the repository at this point in the history
…tz fields (#252)
  • Loading branch information
Rascal authored Sep 25, 2023
1 parent d1d89a2 commit 3be1adc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dialect/postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (d postgreSQL) EncodeBool(b bool) string {
}

func (d postgreSQL) EncodeTime(t time.Time) string {
return MySQL.EncodeTime(t)
return `'` + t.Format(time.RFC3339Nano) + `'`
}

func (d postgreSQL) EncodeBytes(b []byte) string {
Expand Down
33 changes: 33 additions & 0 deletions dialect/postgresql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package dialect

import (
"github.com/stretchr/testify/require"
"testing"
"time"
)

func TestEncodeTime(t *testing.T) {
for _, test := range []struct {
in time.Time
want string
}{
{
in: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
want: `'2009-11-10T23:00:00Z'`,
},
{
in: time.Date(2009, time.November, 10, 15, 0, 0, 0, time.FixedZone("UTC-8", -8*60*60)),
want: `'2009-11-10T15:00:00-08:00'`,
},
{
in: time.Date(2009, time.November, 11, 07, 0, 0, 0, time.FixedZone("UTC+8", 8*60*60)),
want: `'2009-11-11T07:00:00+08:00'`,
},
{
in: time.Date(2009, time.November, 10, 23, 45, 59, 123456789, time.UTC),
want: `'2009-11-10T23:45:59.123456789Z'`,
},
} {
require.Equal(t, test.want, PostgreSQL.EncodeTime(test.in))
}
}

0 comments on commit 3be1adc

Please sign in to comment.