Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ch/chschema/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,13 @@ func parseDateTime64Prec(s string) int {
if s == "" {
return 0
}

// DateTime64 can have timezone, e.g.:
// DateTime64(3, 'Asia/Bangkok')
// DateTime64(3)
// we need to remove timezone to get precision
s = strings.Split(s, ",")[0]

prec, err := strconv.Atoi(s)
if err != nil {
return 0
Expand Down
9 changes: 7 additions & 2 deletions ch/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ func TestScanArrayUint8(t *testing.T) {

func TestDateTime64(t *testing.T) {
type Model struct {
Time time.Time `ch:"type:DateTime64(9)"`
Time time.Time `ch:"type:DateTime64(9)"`
TimeWithTZ time.Time `ch:"type:DateTime64(9, 'UTC')"`
}

ctx := context.Background()
Expand All @@ -278,14 +279,18 @@ func TestDateTime64(t *testing.T) {
err := db.ResetModel(ctx, (*Model)(nil))
require.NoError(t, err)

in := &Model{Time: time.Unix(0, 12345678912345)}
in := &Model{
Time: time.Unix(0, 12345678912345),
TimeWithTZ: time.Unix(0, 12345678912345).In(time.UTC),
}
_, err = db.NewInsert().Model(in).Exec(ctx)
require.NoError(t, err)

out := new(Model)
err = db.NewSelect().Model(out).Scan(ctx)
require.NoError(t, err)
require.Equal(t, in.Time.UnixNano(), out.Time.UnixNano())
require.Equal(t, in.TimeWithTZ.UnixNano(), out.TimeWithTZ.UnixNano())
}

func TestInvalidType(t *testing.T) {
Expand Down