시간대 정보가없는 타임 스탬프가 있습니다. "+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))
}
다음 코드가 도움이 될 것입니다. 더 나은 이해를 위해 코드에 주석을 달았습니다.
package main
import (
"fmt"
"os"
"time"
)
func main() {
// Get the timezone
zone, offset := time.Now().Zone()
// Get the location
var loc = time.FixedZone(zone, offset)
// Reference format: Mon Jan 2 15:04:05 -0700 MST 2006
t, err := time.ParseInLocation("2006-01-02 15:04:05", "2020-11-14 05:53:40", loc)
if err != nil {
fmt.Println("Error: ", err)
os.Exit(1)
}
// Print the timestamp in RFC3339 format
fmt.Println(t.Format(time.RFC3339))
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다