import ( "fmt" "time" _ "github.com/go-sql-driver/mysql" "xorm.io/xorm" )
[Go 時區]
取得目前時區與跟UTC的偏移量
t := time.Now() zone, offset := t.Zone() fmt.Println(zone, offset) //CST 28800
轉換成指定時區的時間
now := time.Now() fmt.Println(now) //2021-05-07 03:05:05.029289 +0800 CST m=+0.002740401 time_zone_1, err1 := time.LoadLocation("") //UTC if err1 != nil { fmt.Println(err1) } fmt.Println(now.In(time_zone_1)) //2021-05-06 19:05:05.029289 +0000 UTC time_zone_2, err2 := time.LoadLocation("Local") //Server環境的時區 if err2 != nil { fmt.Println(err2) } fmt.Println(now.In(time_zone_2)) //2021-05-07 03:05:05.029289 +0800 CST time_zone_3, err3 := time.LoadLocation("America/Los_Angeles") if err3 != nil { fmt.Println(err3) } fmt.Println(now.In(time_zone_3)) //2021-05-06 12:05:05.029289 -0700 PDT time_zone_4 := time.FixedZone("test", 60*60*3) //自訂一個時區,名稱test, UTC+3 fmt.Println(now.In(time_zone_4)) //2021-05-06 22:05:05.029289 +0300 test //time_zone_5, err5 := time.LoadLocation("Asia/Shanghai") //time_zone_5, err5 := time.LoadLocation("Asia/Taipei")
[xorm 時區]
取得 xorm 的時區設定
fmt.Println(engine.TZLocation) //Local
設定 xorm 時區
engine.TZLocation, _ = time.LoadLocation("America/Los_Angeles") fmt.Println(engine.TZLocation) //America/Los_Angeles
其他:
- time.Time 類型的資料包含時區
- parseTime=true,將 DATE、DATETIME 型態的資料用 time.Time 表示。
parseTime=false,將 DATE、DATETIME 型態的資料用 []byte/string 表示。//engine, err := xorm.NewEngine("....?charset=utf8&parseTime=false", connStr) sql := "select STR_TO_DATE('2003-12-31 01:02:03', '%Y-%m-%d %H:%i:%s') AS t" results, err := engine.Query(sql) fmt.Println("results :", results) fmt.Println("results t:", string(results[0]["t"])) //parseTime=true //results : [map[t:[50 48 48 51 45 49 50 45 51 49 84 48 49 58 48 50 58 48 51 90]]] //results t: 2003-12-31T01:02:03Z //parseTime=false //results : [map[t:[50 48 48 51 45 49 50 45 51 49 32 48 49 58 48 50 58 48 51]]] //results t: 2003-12-31 01:02:03 sql = "select STR_TO_DATE('0000-00-00 00:00:00', '%Y-%m-%d %H:%i:%s') AS t" results, err = engine.Query(sql) fmt.Println("results :", results) fmt.Println("results t:", string(results[0]["t"])) //parseTime=true //results : [map[t:[48 48 48 49 45 48 49 45 48 49 84 48 48 58 48 48 58 48 48 90]]] //results t: 0001-01-01T00:00:00Z //parseTime=false //results : [map[t:[48 48 48 48 45 48 48 45 48 48 32 48 48 58 48 48 58 48 48]]] //results t: 0000-00-00 00:00:00
參考:
- https://stackoverflow.com/questions/34975007/how-can-i-extract-the-value-of-my-current-local-time-offset
go - How can I extract the value of my current local time offset? - Stack Overflow - https://syaning.github.io/go-pkgs/time/#location
time | Go语言标准包解析 - https://studygolang.com/topics/2192
golang积累-时间、时区、格式的使用 - Go语言中文网 - Golang中文社区 - https://books.studygolang.com/xorm/chapter-04/1.created.html
创建时间Created · XORM操作指南 - https://www.w3schools.com/SQl/func_mysql_str_to_date.asp
MySQL STR_TO_DATE() Function - https://github.com/go-sql-driver/mysql#dsn-data-source-name
GitHub - go-sql-driver/mysql: Go MySQL Driver is a MySQL driver for Go's (golang) database/sql package
沒有留言:
張貼留言