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 Four

The Four

Scott Galloway / Portfolio / 2017-10-3 / USD 28.00

NEW YORK TIMES BESTSELLER USA TODAY BESTSELLER Amazon, Apple, Facebook, and Google are the four most influential companies on the planet. Just about everyone thinks they know how they got there.......一起来看看 《The Four》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

html转js在线工具
html转js在线工具

html转js在线工具