From 7d832aaf3b596ea88f95e10bd1d71169993a5f73 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Mon, 29 Dec 2025 12:46:26 +0800 Subject: [PATCH] :art: Improve template function `ISOMonth` https://github.com/siyuan-note/siyuan/issues/16718 Signed-off-by: Daniel <845765@qq.com> --- kernel/util/time.go | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/kernel/util/time.go b/kernel/util/time.go index 06ce818e9..d33ddfa27 100644 --- a/kernel/util/time.go +++ b/kernel/util/time.go @@ -75,14 +75,23 @@ func ISOYear(date time.Time) int { // ISOMonth returns the month in which the first day of the ISO 8601 week of date occurs. func ISOMonth(date time.Time) int { - weekday := int(date.Weekday()) - if weekday == 0 { - weekday = 7 - } + isoYear, isoWeek := date.ISOWeek() - daysToMonday := weekday - 1 - monday := date.AddDate(0, 0, -daysToMonday) - return int(monday.Month()) + // 1. 找到该 ISO 年份的 1 月 4 日(它必然属于第 1 周) + jan4 := time.Date(isoYear, time.January, 4, 0, 0, 0, 0, date.Location()) + + // 2. 找到第 1 周的周四 + // (jan4.Weekday() + 6) % 7 将周一~周日映射为 0~6 + daysToMonday := (int(jan4.Weekday()) + 6) % 7 + mondayOfWeek1 := jan4.AddDate(0, 0, -daysToMonday) + thursdayOfWeek1 := mondayOfWeek1.AddDate(0, 0, 3) + + // 3. 计算目标周的周四 + // 目标周四 = 第一周周四 + (isoWeek-1) * 7天 + targetThursday := thursdayOfWeek1.AddDate(0, 0, (isoWeek-1)*7) + + // 4. 返回该周四所在的自然月份 + return int(targetThursday.Month()) } // ISOWeekDate returns the date of the specified day of the week in the ISO 8601 week of date.