package main
import (
"fmt"
"github.com/go-xorm/xorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
"time"
)
func main(){
engine, err := xorm.NewEngine("mysql", "root:[email protected]/db_name?charset=utf8&loc=Local")
if err!=nil{
fmt.Println(err)
return
}
engine1, err := xorm.NewEngine("mysql", "root:[email protected]/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函数的时候导致少了一天
原文地址:http://blog.51cto.com/3732370/2314278
时间: 2024-10-17 11:34:50