feat(user/questions): 手机端时间跨年自动补年份,避免省略年份后产生歧义

DateCompact:与当前同一年的渲染「月-日 时:分」,跨年补回完整「年-月-日 时:分」。
只砍年份会让「去年今天」和「今年今天」显示成同一串,逐条补年份比按年分组便宜得多
(列表带游标分页 + 可切排序/筛选,组标题会跨页重复或错位)。
桌面端不受影响,仍显示 Y-m-d H:i:s。
测试覆盖年份边界(同年 1/1 与 12/31、跨年);预览数据里放了一条 2025 年的提问用于肉眼核对。
This commit is contained in:
tamakyi
2026-10-23 21:04:50 +08:00
parent 812b70cb4c
commit c680671b7e
4 changed files with 49 additions and 3 deletions
+30
View File
@@ -0,0 +1,30 @@
package template
import (
"testing"
"time"
)
// dateCompact 是手机端列表的时间格式:同年省略年份,跨年补回年份。
// 这条测试盯的是「年份边界」——同一年的 1 月 1 日也要省略年份,
// 不能因为「看起来像很久以前」就提前补年。
func TestDateCompact(t *testing.T) {
now := time.Date(2026, 10, 23, 21, 0, 0, 0, time.Local)
cases := []struct {
in time.Time
want string
}{
{time.Date(2026, 9, 9, 1, 18, 3, 0, time.Local), "09-09 01:18"},
{time.Date(2026, 1, 1, 0, 0, 0, 0, time.Local), "01-01 00:00"},
{time.Date(2026, 12, 31, 23, 59, 59, 0, time.Local), "12-31 23:59"},
{time.Date(2025, 9, 5, 4, 45, 22, 0, time.Local), "2025-09-05 04:45"},
{time.Date(2027, 1, 1, 0, 0, 0, 0, time.Local), "2027-01-01 00:00"},
}
for _, c := range cases {
if got := dateCompact(c.in, now); got != c.want {
t.Errorf("dateCompact(%s) = %q, want %q", c.in.Format(time.RFC3339), got, c.want)
}
}
}
+15
View File
@@ -47,6 +47,9 @@ func FuncMap() []template.FuncMap {
format = replacer.Replace(format)
return t.Format(format)
},
"DateCompact": func(t time.Time) string {
return dateCompact(t, time.Now())
},
"QuestionFormat": func(input string) template.HTML {
input = html.EscapeString(input)
input = strings.ReplaceAll(input, "\n", "</br>")
@@ -236,3 +239,15 @@ func FuncMap() []template.FuncMap {
func Safe(raw string) template.HTML {
return template.HTML(raw)
}
// dateCompact 手机端列表用的紧凑时间:与 now 同一年的只给「月-日 时:分」。
// 手机一行的宽度放不下「Y-m-d H:i:s」(单它就要 127px),但只砍年份会让
// 「去年的今天」和「今年今天」渲染成同一串,所以在跨年时自动补回年份。
// 注意:这是「逐条补年份」,不是「按年分组」—— 列表带游标分页、可切排序与筛选,
// 分组标题会跨页重复/错位,收益不如补年份直接。
func dateCompact(t, now time.Time) string {
if t.Year() == now.Year() {
return t.Format("01-02 15:04")
}
return t.Format("2006-01-02 15:04")
}