To parse an ISO 8601 timestamp in GoLang, you can use the time.Parse
function with the appropriate layout. For your timestamp format 2016-07-25T02:22:33+0000
, the layout would be "2006-01-02T15:04:05Z0700"
.
Here’s how you can do it:
gopackage main
import (
"fmt"
"time"
)
func main() {
timestamp := "2016-07-25T02:22:33+0000"
layout := "2006-01-02T15:04:05Z0700"
parsedTime, err := time.Parse(layout, timestamp)
if err != nil {
fmt.Println("Error parsing timestamp:", err)
return
}
fmt.Println("Parsed timestamp:", parsedTime)
}
This code will output:
yamlParsed timestamp: 2016-07-25 02:22:33 +0000 +0000
The layout string uses a reference time of Mon Jan 2 15:04:05 -0700 MST 2006
to define the format for the timestamp. Each component (year, month, day, hour, minute, second, timezone offset) is represented by a specific value (e.g., 2006
for the year, 01
for the month, 02
for the day, etc.) to match the format of your timestamp.
I hope this helps! Let me know if you need further assistance.
المزيد من المعلومات
بالتأكيد! تنسيق الـ ISO 8601 للتاريخ والوقت يعتمد على تمثيل دقيق وموحد للتواريخ والأوقات، ويشمل تمثيلًا للتواريخ الميلادية والتواريخ الزمنية. يتمثل التواريخ الميلادية في الصيغة YYYY-MM-DD حيث تمثل YYYY السنة بأربعة أرقام، MM الشهر، و DD اليوم. أما التواريخ الزمنية فتتضمن تواريخًا مع الوقت والتوقيت، ويمكن أن يكون لها توقيت محلي أو توقيت عالمي (UTC).
في حالتك، النمط الذي تتبعه الطابعة للتوقيت هو "2006-01-02T15:04:05Z0700"
:
2006
يمثل السنة بأربعة أرقام.01
يمثل الشهر.02
يمثل اليوم.15
يمثل الساعة (بنظام 24 ساعة).04
يمثل الدقائق.05
يمثل الثواني.Z0700
يمثل التوقيت الزمني العالمي UTC بتوقيتZ
، و0700
يمثل الفارق بالساعات عن التوقيت العالمي.
عند استخدام time.Parse
، يجب تمرير نفس النمط (الـ layout) الذي تم استخدامه في تشكيل النص.