diff --git a/kernel/model/template.go b/kernel/model/template.go index 7a12e1747..d76743355 100644 --- a/kernel/model/template.go +++ b/kernel/model/template.go @@ -43,32 +43,13 @@ import ( "github.com/siyuan-note/siyuan/kernel/util" ) -/** - * [0-6] represents [Sunday - Saturday] - */ -func Weekday(date time.Time) int { - return int(date.Weekday()) -} - -func WeekdayCN(date time.Time) string { - week := GetWeekday(date) - weekdayCN := []string{"日", "一", "二", "三", "四", "五", "六"} - return weekdayCN[week] -} - -// date is in `week`-th weak of this year -func ISOWeek(date time.Time) int { - _, week := date.ISOWeek() - return week -} - func RenderGoTemplate(templateContent string) (ret string, err error) { tmpl := template.New("") tmpl = tmpl.Funcs(sprig.TxtFuncMap()) tmpl = tmpl.Funcs(template.FuncMap{ - "Weekday": Weekday, - "WeekdayCN": WeekdayCN, - "ISOWeek": ISOWeek, + "Weekday": util.Weekday, + "WeekdayCN": util.WeekdayCN, + "ISOWeek": util.ISOWeek, }) tpl, err := tmpl.Parse(templateContent) if nil != err { @@ -266,9 +247,9 @@ func renderTemplate(p, id string, preview bool) (string, error) { } return ret } - funcMap["Weekday"] = Weekday - funcMap["WeekdayCN"] = WeekdayCN - funcMap["ISOWeek"] = ISOWeek + funcMap["Weekday"] = util.Weekday + funcMap["WeekdayCN"] = util.WeekdayCN + funcMap["ISOWeek"] = util.ISOWeek goTpl := template.New("").Delims(".action{", "}") tpl, err := goTpl.Funcs(funcMap).Parse(gulu.Str.FromBytes(md)) diff --git a/kernel/util/time.go b/kernel/util/time.go index e432cb39f..33c7b0304 100644 --- a/kernel/util/time.go +++ b/kernel/util/time.go @@ -26,6 +26,28 @@ import ( "github.com/dustin/go-humanize" ) +// Weekday returns the day of the week specified by date. +// Sunday=0, Monday=1, ..., Saturday=6. +func Weekday(date time.Time) int { + return int(date.Weekday()) +} + +// WeekdayCN returns the day of the week specified by date. +// Sunday=日, Monday=一, ..., Saturday=六. +func WeekdayCN(date time.Time) string { + week := Weekday(date) + weekdayCN := []string{"日", "一", "二", "三", "四", "五", "六"} + return weekdayCN[week] +} + +// ISOWeek returns the ISO 8601 year and week number in which date occurs. +// Week ranges from 1 to 53. Jan 01 to Jan 03 of year n might belong to week 52 or 53 of year n-1, +// and Dec 29 to Dec 31 might belong to week 1 of year n+1. +func ISOWeek(date time.Time) int { + _, week := date.ISOWeek() + return week +} + func Millisecond2Time(t int64) time.Time { sec := t / 1000 msec := t % 1000