func Test_Tmp(t *testing.T) {
tmp := func() time.Time {
// 1615219198 2021-03-08 23:59:58
// tmpStamp是没有时区的,他代表当地时区下的时间戳,所以要使用tmpStamp.AsTime().Local()转化为带时区的time类型
tmpStamp := timestamppb.New(time.Unix(1615219198, 0))
tmpTime := tmpStamp.AsTime().Local() // 2021-03-08 23:59:58 +0800
// Parse会丢失时区,所以要使用ParseInLocation
// todayZeroTime, _ := time.Parse("2006-01-02", tmpTime.Format("2006-01-02"))
todayZeroTime, _ := time.ParseInLocation("2006-01-02", tmpTime.Format("2006-01-02"), time.Local) // 2021-03-08 00:00:00 +0800
res := todayZeroTime.AddDate(0, 0, 1).Add(-1 * time.Second) // 2021-03-08 23:59:59 +0800
return res
}()
t.Log(tmp)
}
golang时区问题
猜你喜欢
转载自blog.csdn.net/chushoufengli/article/details/114839367
今日推荐
周排行