Error, tim zone, exec, time, xorm.exec("insert into abc (ctime) values(?)", time.Now()) #2005

Open
opened 2021-07-22 02:27:56 +00:00 by gepeng · 0 comments

package main

import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/stretchr/testify/require"
"testing"
"time"
"xorm.io/xorm"
)

type (
abc struct {
Code string
Count int
Ctime time.Time xorm:"created"
Mtime time.Time xorm:"updated"
Dtime *time.Time xorm:"deleted"
Ok int //bool
Weight float64
}
)

func TestInsert(t testing.T) {
loc, err := time.LoadLocation("Asia/Shanghai") //设置时区
if err != nil {
loc = time.FixedZone("CST", 8
3600)
}
time.Local = loc

const code="test"
x, err := xorm.NewEngine("mysql", "root:root@tcp/testDB?timeout=90s&collation=utf8mb4_unicode_ci")
require.NoError(t, err)
require.NoError(t, x.Sync(&abc{}))
if err != nil {
	fmt.Println(err)
	return
}
defer x.Close()
x.ShowSQL(true)
x.SetTZDatabase(loc)
x.SetTZLocation(loc)

tm:= time.Now()
_, err = x.NewSession().Exec("insert into abc (code, ctime) values(?,?)", code, tm)
require.NoError(t, err)
bean:= &abc{}
ok, err:= x.Where("code=?", code).Get(bean)
require.NoError(t, err)
require.True(t, ok)
fmt.Println(tm.Format("2006-01-02 15:04:05"), bean.Ctime.Format("2006-01-02 15:04:05"))
require.True(t, bean.Ctime.Equal(tm))
_, err = x.Where("code=?", code).Delete(&abc{})
require.NoError(t, err)

}

package main import ( "fmt" _ "github.com/go-sql-driver/mysql" "github.com/stretchr/testify/require" "testing" "time" "xorm.io/xorm" ) type ( abc struct { Code string Count int Ctime time.Time `xorm:"created"` Mtime time.Time `xorm:"updated"` Dtime *time.Time `xorm:"deleted"` Ok int //bool Weight float64 } ) func TestInsert(t *testing.T) { loc, err := time.LoadLocation("Asia/Shanghai") //设置时区 if err != nil { loc = time.FixedZone("CST", 8*3600) } time.Local = loc const code="test" x, err := xorm.NewEngine("mysql", "root:root@tcp/testDB?timeout=90s&collation=utf8mb4_unicode_ci") require.NoError(t, err) require.NoError(t, x.Sync(&abc{})) if err != nil { fmt.Println(err) return } defer x.Close() x.ShowSQL(true) x.SetTZDatabase(loc) x.SetTZLocation(loc) tm:= time.Now() _, err = x.NewSession().Exec("insert into abc (code, ctime) values(?,?)", code, tm) require.NoError(t, err) bean:= &abc{} ok, err:= x.Where("code=?", code).Get(bean) require.NoError(t, err) require.True(t, ok) fmt.Println(tm.Format("2006-01-02 15:04:05"), bean.Ctime.Format("2006-01-02 15:04:05")) require.True(t, bean.Ctime.Equal(tm)) _, err = x.Where("code=?", code).Delete(&abc{}) require.NoError(t, err) }
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: xorm/xorm#2005
No description provided.