PostgreSQL and MySQL accepts microsecond #1815

Closed
skanehira wants to merge 3 commits from master into master
8 changed files with 154 additions and 37 deletions

View File

@ -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")

113
dialects/time_test.go Normal file
View File

@ -0,0 +1,113 @@
// Copyright 2020 The Xorm Authors. All rights reserved.
skanehira marked this conversation as resolved Outdated
Outdated
Review

Please add copyright header.

Please add copyright header.

Done :)

Done :)
// 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)
})
}
}

2
go.mod
View File

@ -1,4 +1,4 @@
module xorm.io/xorm
module gitea.com/skanehira/xorm
go 1.11

View File

@ -195,7 +195,7 @@ func TestUnscopeDelete(t *testing.T) {
type UnscopeDeleteStruct struct {
Id int64
Name string
DeletedAt time.Time `xorm:"deleted"`
DeletedAt time.Time `xorm:"deleted datetime(6)"`
}
assertSync(t, new(UnscopeDeleteStruct))

View File

@ -174,8 +174,8 @@ type DefaultInsert struct {
Id int64
Status int `xorm:"default -1"`
Name string
Created time.Time `xorm:"created"`
Updated time.Time `xorm:"updated"`
Created time.Time `xorm:"created datetime(6)"`
Updated time.Time `xorm:"updated datetime(6)"`
}
func TestInsertDefault(t *testing.T) {
@ -227,7 +227,7 @@ func TestInsertDefault2(t *testing.T) {
type CreatedInsert struct {
Id int64
Created time.Time `xorm:"created"`
Created time.Time `xorm:"created datetime(6)"`
}
type CreatedInsert2 struct {
@ -384,7 +384,7 @@ func TestDefaultTime3(t *testing.T) {
type MyJSONTime struct {
Id int64 `json:"id"`
Created JSONTime `xorm:"created" json:"created_at"`
Created JSONTime `xorm:"created datetime(6)" json:"created_at"`
}
func TestCreatedJsonTime(t *testing.T) {

View File

@ -462,7 +462,7 @@ func TestUpdateIncrDecr(t *testing.T) {
type UpdatedUpdate struct {
Id int64
Updated time.Time `xorm:"updated"`
Updated time.Time `xorm:"updated datetime(6)"`
}
type UpdatedUpdate2 struct {
@ -834,8 +834,8 @@ func TestCreatedUpdated2(t *testing.T) {
type CreatedUpdatedStruct struct {
Id int64
Name string
CreateAt time.Time `xorm:"created" json:"create_at"`
UpdateAt time.Time `xorm:"updated" json:"update_at"`
CreateAt time.Time `xorm:"created datetime(6)" json:"create_at"`
UpdateAt time.Time `xorm:"updated datetime(6)" json:"update_at"`
}
assertSync(t, new(CreatedUpdatedStruct))

View File

@ -7,7 +7,6 @@ package integrations
import (
"fmt"
"sort"
"strings"
"testing"
"time"
@ -1104,7 +1103,7 @@ func TestTagTime(t *testing.T) {
type TagUTCStruct struct {
Id int64
Name string
Created time.Time `xorm:"created utc"`
Created time.Time `xorm:"created utc datetime(6)"`
}
assertSync(t, new(TagUTCStruct))
@ -1128,8 +1127,7 @@ func TestTagTime(t *testing.T) {
has, err = testEngine.Table("tag_u_t_c_struct").Cols("created").Get(&tm)
assert.NoError(t, err)
assert.True(t, has)
assert.EqualValues(t, s.Created.UTC().Format("2006-01-02 15:04:05"),
strings.Replace(strings.Replace(tm, "T", " ", -1), "Z", "", -1))
assert.EqualValues(t, s.Created.UTC().Format("2006-01-02 15:04:05.999999"), tm)
}
func TestTagAutoIncr(t *testing.T) {

View File

@ -10,9 +10,8 @@ import (
"testing"
"time"
"xorm.io/xorm/internal/utils"
"github.com/stretchr/testify/assert"
"xorm.io/xorm/internal/utils"
)
func formatTime(t time.Time) string {
@ -23,8 +22,8 @@ func TestTimeUserTime(t *testing.T) {
assert.NoError(t, PrepareEngine())
type TimeUser struct {
Id string
OperTime time.Time
Id string `xorm:"id"`
OperTime time.Time `xorm:"datetime(6)"`
}
assertSync(t, new(TimeUser))
@ -60,7 +59,7 @@ func TestTimeUserTimeDiffLoc(t *testing.T) {
type TimeUser2 struct {
Id string
OperTime time.Time
OperTime time.Time `xorm:"datetime(6)"`
}
assertSync(t, new(TimeUser2))
@ -90,7 +89,7 @@ func TestTimeUserCreated(t *testing.T) {
type UserCreated struct {
Id string
CreatedAt time.Time `xorm:"created"`
CreatedAt time.Time `xorm:"created datetime(6)"`
}
assertSync(t, new(UserCreated))
@ -125,7 +124,7 @@ func TestTimeUserCreatedDiffLoc(t *testing.T) {
type UserCreated2 struct {
Id string
CreatedAt time.Time `xorm:"created"`
CreatedAt time.Time `xorm:"created datetime(6)"`
}
assertSync(t, new(UserCreated2))
@ -154,8 +153,8 @@ func TestTimeUserUpdated(t *testing.T) {
type UserUpdated struct {
Id string
CreatedAt time.Time `xorm:"created"`
UpdatedAt time.Time `xorm:"updated"`
CreatedAt time.Time `xorm:"created datetime(6)"`
UpdatedAt time.Time `xorm:"updated datetime(6)"`
}
assertSync(t, new(UserUpdated))
@ -211,8 +210,8 @@ func TestTimeUserUpdatedDiffLoc(t *testing.T) {
type UserUpdated2 struct {
Id string
CreatedAt time.Time `xorm:"created"`
UpdatedAt time.Time `xorm:"updated"`
CreatedAt time.Time `xorm:"created datetime(6)"`
UpdatedAt time.Time `xorm:"updated datetime(6)"`
}
assertSync(t, new(UserUpdated2))
@ -262,9 +261,9 @@ func TestTimeUserDeleted(t *testing.T) {
type UserDeleted struct {
Id string
CreatedAt time.Time `xorm:"created"`
UpdatedAt time.Time `xorm:"updated"`
DeletedAt time.Time `xorm:"deleted"`
CreatedAt time.Time `xorm:"created datetime(6)"`
UpdatedAt time.Time `xorm:"updated datetime(6)"`
DeletedAt time.Time `xorm:"deleted datetime(6)"`
CreatedAtStr string `xorm:"datetime created"`
UpdatedAtStr string `xorm:"datetime updated"`
}
@ -318,9 +317,9 @@ func TestTimeUserDeletedDiffLoc(t *testing.T) {
type UserDeleted2 struct {
Id string
CreatedAt time.Time `xorm:"created"`
UpdatedAt time.Time `xorm:"updated"`
DeletedAt time.Time `xorm:"deleted"`
CreatedAt time.Time `xorm:"created datetime(6)"`
UpdatedAt time.Time `xorm:"updated datetime(6)"`
DeletedAt time.Time `xorm:"deleted datetime(6)"`
}
assertSync(t, new(UserDeleted2))
@ -389,9 +388,9 @@ func TestCustomTimeUserDeleted(t *testing.T) {
type UserDeleted3 struct {
Id string
CreatedAt JSONDate `xorm:"created"`
UpdatedAt JSONDate `xorm:"updated"`
DeletedAt JSONDate `xorm:"deleted"`
CreatedAt JSONDate `xorm:"created datetime(6)"`
UpdatedAt JSONDate `xorm:"updated datetime(6)"`
DeletedAt JSONDate `xorm:"deleted datetime(6)"`
}
assertSync(t, new(UserDeleted3))
@ -442,9 +441,9 @@ func TestCustomTimeUserDeletedDiffLoc(t *testing.T) {
type UserDeleted4 struct {
Id string
CreatedAt JSONDate `xorm:"created"`
UpdatedAt JSONDate `xorm:"updated"`
DeletedAt JSONDate `xorm:"deleted"`
CreatedAt JSONDate `xorm:"created datetime(6)"`
UpdatedAt JSONDate `xorm:"updated datetime(6)"`
DeletedAt JSONDate `xorm:"deleted datetime(6)"`
}
assertSync(t, new(UserDeleted4))