Golang, MySQL连接不设置时区的问题

栏目: Go · 发布时间: 7年前

内容简介:package mainimport ("fmt"

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函数的时候导致少了一天


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Probabilistic Method Second Edition

The Probabilistic Method Second Edition

Noga Alon、Joel H. Spencer / Wiley-Blackwell / 2000 / $121.95

The leading reference on probabilistic methods in combinatorics-now expanded and updated When it was first published in 1991, The Probabilistic Method became instantly the standard reference on one......一起来看看 《The Probabilistic Method Second Edition》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具