This commit is contained in:
23
main.go
23
main.go
@@ -1667,7 +1667,7 @@ func main() {
|
||||
if t.IsZero() {
|
||||
return ""
|
||||
}
|
||||
return utcDBTime(t).In(loc).Format("2006-01-02 15:04:05 MST")
|
||||
return normalizeTime(t).In(loc).Format("2006-01-02 15:04:05 MST")
|
||||
},
|
||||
"short": func(s string, n int) string {
|
||||
if len(s) <= n {
|
||||
@@ -1739,11 +1739,30 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
func utcDBTime(t time.Time) time.Time {
|
||||
func normalizeTime(t time.Time) time.Time {
|
||||
if t.IsZero() {
|
||||
return t
|
||||
}
|
||||
|
||||
// Fall 1:
|
||||
// MySQL DATETIME wird mit loc=UTC gelesen.
|
||||
// Dann ist t.Location() bereits UTC und alles ist gut.
|
||||
if t.Location() == time.UTC {
|
||||
return t
|
||||
}
|
||||
|
||||
// Fall 2:
|
||||
// Der MySQL-Treiber hat die Zeit bereits als echten Zeitpunkt mit Location gelesen.
|
||||
// Dann NICHT die Uhrzeit neu als UTC interpretieren, sondern Instant nach UTC konvertieren.
|
||||
name := t.Location().String()
|
||||
if name != "" && name != "Local" {
|
||||
return t.UTC()
|
||||
}
|
||||
|
||||
// Fall 3:
|
||||
// MySQL DATETIME ohne echte Zeitzone wurde als time.Local interpretiert.
|
||||
// In deiner Anwendung speicherst du DB-Zeiten logisch als UTC.
|
||||
// Deshalb interpretieren wir die Wandzeit als UTC.
|
||||
return time.Date(
|
||||
t.Year(),
|
||||
t.Month(),
|
||||
|
||||
Reference in New Issue
Block a user