package main
import (
"fmt"
"time"
)
func main() {
location, _ := time.LoadLocation("Asia/Shanghai")
inputTime := "2010-10-10 10:10:10"
layout := "2006-01-02 15:04:05"
t, _ := time.ParseInLocation(layout, inputTime, location)
dateTime := time.Unix(t.Unix(), 0).In(location).Format(layout)
fmt.Println(inputTime, dateTime)
// 2010-10-10 10:10:10 2010-10-10 10:10:10 推荐转换方法
}
func main0() {
location, _ := time.LoadLocation("Asia/Shanghai")
inputTime := "2010-10-10 10:10:10"
layout := "2006-01-02 15:04:05"
t, _ := time.Parse(layout, inputTime)
dateTime := time.Unix(t.Unix(), 0).In(location).Format(layout)
fmt.Println(inputTime, dateTime)
//结果与正常时间相差八小时,2010-10-10 10:10:10 2010-10-10 18:10:10,是时区设置问题导致的
//原因是Parse方法在未指定时区时默认使用UTC作为时区
}