package main
import (
"fmt"
"github.com/go-xorm/xorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
"time"
)
func main(){
engine, err := xorm.NewEngine("mysql", "root:password@/db_name?charset=utf8&loc=Local")
if err!=nil{
fmt.Println(err)
return
}
engine1, err := xorm.NewEngine("mysql", "root:112358@/dhb_newdata_2?charset=utf8")
if err!=nil{
fmt.Println(err)
return
}
var result string
now := time.Now()
sql := "SELECT DATE(?)"
_,err=engine.SQL(sql, now).Get(&result)
fmt.Println("engine, with timezone",result,now)
_,err=engine1.SQL(sql, now).Get(&result)
fmt.Println("engine1, no timezone", result,now)
}
输出的结果:
engine, with timezone 2018-11-08 2018-11-08 00:36:05.6461211 +0800 CST m=+0.016989801
engine1, no timezone 2018-11-07 2018-11-08 00:36:05.6461211 +0800 CST m=+0.016989801
可以看到在MySQL里使用DATE函数的时候导致少了一天
有疑问加站长微信联系(非本文作者)
![](https://static.golangjob.cn/static/img/footer.png?imageView2/2/w/280)