diff --git a/internal/base/server/http_funcmap.go b/internal/base/server/http_funcmap.go index a5d530b50..cf3cf5144 100644 --- a/internal/base/server/http_funcmap.go +++ b/internal/base/server/http_funcmap.go @@ -78,8 +78,7 @@ var funcMap = template.FuncMap{ return trans }, "timeFormatISO": func(tz string, timestamp int64) string { - _, _ = time.LoadLocation(tz) - return time.Unix(timestamp, 0).Format("2006-01-02T15:04:05.000Z") + return day.FormatISO(timestamp, tz) }, "translatorTimeFormatLongDate": func(la i18n.Language, tz string, timestamp int64) string { trans := translator.GlobalTrans.Tr(la, "ui.dates.long_date_with_time") @@ -91,7 +90,6 @@ var funcMap = template.FuncMap{ between int64 = 0 trans string ) - _, _ = time.LoadLocation(tz) if now > timestamp { between = now - timestamp } @@ -119,7 +117,7 @@ var funcMap = template.FuncMap{ if between >= 3600*24 && between < 3600*24*366 && - time.Unix(timestamp, 0).Format("2006") == time.Unix(now, 0).Format("2006") { + sameYearInZone(timestamp, now, tz) { trans = translator.GlobalTrans.Tr(la, "ui.dates.long_date") return day.Format(timestamp, trans, tz) } @@ -137,6 +135,18 @@ var funcMap = template.FuncMap{ "urlTitle": htmltext.UrlTitle, } +// sameYearInZone reports whether two instants fall into the same calendar +// year when rendered in the given IANA zone. The relative/absolute date +// rendering uses this to decide whether the year may be omitted, and the +// decision must follow the zone the value is rendered in, not the server's +// zone: two instants sharing a year in Tokyo can sit on opposite sides of +// New Year in Los Angeles. +func sameYearInZone(a, b int64, tz string) bool { + yearA := time.Unix(a, 0).In(day.Location(tz)).Format("2006") + yearB := time.Unix(b, 0).In(day.Location(tz)).Format("2006") + return yearA == yearB +} + func FormatLinkNofollow(html string) string { var hrefRegexp = regexp.MustCompile("(?m).*?") match := hrefRegexp.FindAllString(html, -1) diff --git a/internal/base/server/http_funcmap_test.go b/internal/base/server/http_funcmap_test.go new file mode 100644 index 000000000..cb0aba5df --- /dev/null +++ b/internal/base/server/http_funcmap_test.go @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package server + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +// timeFormatISO feeds every server-rendered