es wegen der Zeitzone ist, sind Sie das richtige Datum bekommen, aber sprig
Formate "Local" standardmäßig in dem golang.org/pkg/time
standardmäßig „UTC "
Hier ist ein Beispielcode: (Fehlerbehandlung zur Vereinfachung weglassen)
func main() {
// using the "time" package
mydate, _ := time.Parse("2006-01-02", "2016-07-08")
fmt.Println("time:", mydate.In(time.Local).Format("January 02, 2006 (MST)"), "-- specify Local time zone")
fmt.Println("time:", mydate.Format("January 02, 2006 (MST)"), "-- defaults to UTC")
d := struct{ MyDate time.Time }{mydate}
//using sprig
fmap := sprig.TxtFuncMap()
localTpl := `sprig: {{ date "January 02, 2006 (MST)" .MyDate }} -- defaults to Local`
t := template.Must(template.New("test").Funcs(fmap).Parse(localTpl))
var localdate bytes.Buffer
t.Execute(&localdate, d)
fmt.Println(localdate.String())
utcTpl := `sprig: {{ dateInZone "January 02, 2006 (MST)" .MyDate "UTC"}} -- specify UTC time zone`
t = template.Must(template.New("test").Funcs(fmap).Parse(utcTpl))
var utcdate bytes.Buffer
t.Execute(&utcdate, d)
fmt.Println(utcdate.String())
}
Ausgang:
time: July 07, 2016 (EDT) -- specify Local time zone
time: July 08, 2016 (UTC) -- defaults to UTC
sprig: July 07, 2016 (EDT) -- defaults to Local
sprig: July 08, 2016 (UTC) -- specify UTC time zone
Hier einige Referenz:
Zeit: https://golang.org/pkg/time
In Abwesenheit einer Anzeige Zeitzone, Parse liefert ein Zeit in UTC.
SPIG: https://github.com/Masterminds/sprig/blob/master/functions.go#L407
func date(fmt string, date interface{}) string {
return dateInZone(fmt, date, "Local")
}
Hinweis: wenn Sie wollen eine bestimmte Zeitzone zu formatieren, am 2. Vorlage aussehen:
utcTpl := `sprig: {{ dateInZone "January 02, 2006 (MST)" .MyDate "UTC"}} -- specify UTC time zone`
Es ist ein Problem sein könnte, mit Zeitzone Konvertierung in UTC –