我有一个没有时区信息的时间戳。我需要以“+02:00”格式添加时区偏移量。那么在下面的代码中,如何将偏移变量转换为“+02:00”字符串以便获得正确的 RFC3339 时间?
func main() {
zone, offset := time.Now().Zone()
fmt.Println("zone :", zone)
fmt.Println("offset :", offset )
logtimestamp := "2020-11-14 05:53:40,103"
logtimestamp = strings.Split(logtimestamp, ",")[0]
logtimestampFields := strings.Fields(logtimestamp)
if len(logtimestampFields) > 1 {
logtimestamp = logtimestampFields[0] + "T" + logtimestampFields[1] + "+02:00" //replace "+02:00" with proper offset here
}
formattedTime, _ := time.Parse(time.RFC3339, logtimestamp)
fmt.Println("formatted timestamp " + formattedTime.Format(time.RFC3339))
}
智慧大石
相关分类