J’essaie d’parsingr un horodatage Unix mais je suis en erreur hors de scope. Cela n’a pas vraiment de sens pour moi, car la mise en page est correcte (comme dans les documents Go):
package main import "fmt" import "time" func main() { tm, err := time.Parse("1136239445", "1405544146") if err != nil{ panic(err) } fmt.Println(tm) }
Cour de récréation
La fonction time.Parse
ne traite pas les horodatages Unix. Au lieu de cela, vous pouvez utiliser strconv.ParseInt
pour parsingr la chaîne dans int64
et créer l’horodatage avec time.Unix
:
package main import ( "fmt" "time" "strconv" ) func main() { i, err := strconv.ParseInt("1405544146", 10, 64) if err != nil { panic(err) } tm := time.Unix(i, 0) fmt.Println(tm) }
Sortie:
2014-07-16 20:55:46 +0000 UTC
Aire de jeux: http://play.golang.org/p/v_j6UIro7a
Modifier:
strconv.Atoi
de strconv.Atoi
à strconv.ParseInt
pour éviter les débordements int sur les systèmes 32 bits.
vous pouvez directement utiliser le temps.Une fonction de temps unique qui convertit l’horodatage unix en UTC
package main import ( "fmt" "time" ) func main() { unixTimeUTC:=time.Unix(1405544146, 0) //gives unix time stamp in utc unitTimeInRFC3339 :=unixTimeUTC.Format(time.RFC3339) // converts utc time to RFC3339 format fmt.Println("unix time stamp in UTC :--->",unixTimeUTC) fmt.Println("unix time stamp in unitTimeInRFC3339 format :->",unitTimeInRFC3339)
}
Sortie
unix time stamp in UTC :---> 2014-07-16 20:55:46 +0000 UTC unix time stamp in unitTimeInRFC3339 format :----> 2014-07-16T20:55:46Z
vérifier Go Playground: https://play.golang.org/p/5FtRdnkxAd
Partager quelques fonctions que j’ai créées pour les dates:
S’il vous plaît noter que je voulais obtenir du temps pour un endroit particulier (pas seulement l’heure UTC). Si vous voulez l’heure UTC, supprimez simplement la variable loc et l’appel de la fonction .In (loc).
func GetTimeStamp() ssortingng { loc, _ := time.LoadLocation("America/Los_Angeles") t := time.Now().In(loc) return t.Format("20060102150405") } func GetTodaysDate() ssortingng { loc, _ := time.LoadLocation("America/Los_Angeles") current_time := time.Now().In(loc) return current_time.Format("2006-01-02") } func GetTodaysDateTime() ssortingng { loc, _ := time.LoadLocation("America/Los_Angeles") current_time := time.Now().In(loc) return current_time.Format("2006-01-02 15:04:05") } func GetTodaysDateTimeFormatted() ssortingng { loc, _ := time.LoadLocation("America/Los_Angeles") current_time := time.Now().In(loc) return current_time.Format("Jan 2, 2006 at 3:04 PM") } func GetTimeStampFromDate(dtformat ssortingng) ssortingng { form := "Jan 2, 2006 at 3:04 PM" t2, _ := time.Parse(form, dtformat) return t2.Format("20060102150405") }
Consultez ce repository: https://github.com/araddon/dateparse
Vous pouvez utiliser
t, err := dateparse.ParseAny(timestampSsortingng)
Juste utiliser le temps.Parse
Exemple:
package main import ( "fmt" "time" ) func main() { fromSsortingng := "Wed, 6 Sep 2017 10:43:01 +0300" t, e := time.Parse("Mon, _2 Jan 2006 15:04:05 -0700", fromSsortingng) if e != nil { fmt.Printf("err: %s\n", e) } fmt.Printf("UTC time: %v\n", t.UTC()) }
Exemple de travail sur play.golang.org .