go时区问题

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作为时区
}
上一篇:学习笔记Android(二)Fragment


下一篇:PyQt5基础学习-QFileDialog(文件打开对话框) 1.QFileDialog.getOpenFileName(返回打开对应格式的文件名) 2. QFileDialog().setFileM