From e17125cf6f7b38a9070c61d4cf51823462b18d1c Mon Sep 17 00:00:00 2001 From: skanehira Date: Fri, 23 Oct 2020 19:23:12 +0900 Subject: [PATCH] PostgreSQL and MySQL accepts microsecond --- dialects/time.go | 9 +++- dialects/time_test.go | 113 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 dialects/time_test.go diff --git a/dialects/time.go b/dialects/time.go index b0394745..ab6957e7 100644 --- a/dialects/time.go +++ b/dialects/time.go @@ -18,8 +18,15 @@ func FormatTime(dialect Dialect, sqlTypeName string, t time.Time) (v interface{} v = s[11:19] case schemas.Date: v = t.Format("2006-01-02") - case schemas.DateTime, schemas.TimeStamp, schemas.Varchar: // !DarthPestilane! format time when sqlTypeName is schemas.Varchar. + case schemas.Varchar: // !DarthPestilane! format time when sqlTypeName is schemas.Varchar. v = t.Format("2006-01-02 15:04:05") + case schemas.TimeStamp, schemas.DateTime: + dbType := dialect.URI().DBType + if dbType == schemas.POSTGRES || dbType == schemas.MYSQL { + v = t.Format("2006-01-02T15:04:05.999999") + } else { + v = t.Format("2006-01-02 15:04:05") + } case schemas.TimeStampz: if dialect.URI().DBType == schemas.MSSQL { v = t.Format("2006-01-02T15:04:05.9999999Z07:00") diff --git a/dialects/time_test.go b/dialects/time_test.go new file mode 100644 index 00000000..571bd216 --- /dev/null +++ b/dialects/time_test.go @@ -0,0 +1,113 @@ +// Copyright 2020 The Xorm Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package dialects + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" + "xorm.io/xorm/schemas" +) + +type dialect struct { + dbType schemas.DBType + Dialect +} + +func (d dialect) URI() *URI { + return &URI{ + DBType: d.dbType, + } +} + +func TestFormatTime(t *testing.T) { + date := time.Date(2020, 10, 23, 10, 14, 15, 123456, time.Local) + tests := []struct { + name string + dialect Dialect + sqlTypeName string + t time.Time + want interface{} + }{ + { + "test time", + dialect{}, + schemas.Time, + date, + date.Format("2006-01-02 15:04:05")[11:19], + }, + { + "test date", + dialect{}, + schemas.Date, + date, + date.Format("2006-01-02"), + }, + { + "test varchar", + dialect{}, + schemas.Varchar, + date, + date.Format("2006-01-02 15:04:05"), + }, + { + "test timestamp and postgres", + dialect{dbType: schemas.POSTGRES}, + schemas.TimeStamp, + date, + date.Format("2006-01-02T15:04:05.999999"), + }, + { + "test datetime and mysql", + dialect{dbType: schemas.MYSQL}, + schemas.DateTime, + date, + date.Format("2006-01-02T15:04:05.999999"), + }, + { + "test datetime", + dialect{}, + schemas.DateTime, + date, + date.Format("2006-01-02 15:04:05"), + }, + { + "test timestampz", + dialect{dbType: schemas.MSSQL}, + schemas.TimeStampz, + date, + date.Format("2006-01-02T15:04:05.9999999Z07:00"), + }, + { + "test bigint", + dialect{}, + schemas.BigInt, + date, + date.Unix(), + }, + { + "test int", + dialect{}, + schemas.Int, + date, + date.Unix(), + }, + { + "test default", + dialect{}, + "", + date, + date, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := FormatTime(tt.dialect, tt.sqlTypeName, tt.t) + assert.Equal(t, tt.want, got) + }) + } +}