Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a template function about date #9815

Merged
merged 2 commits into from Dec 6, 2023
Merged

Add a template function about date #9815

merged 2 commits into from Dec 6, 2023

Conversation

TCOTC
Copy link
Contributor

@TCOTC TCOTC commented Dec 5, 2023

补充一个日期相关的模板函数

增加「星期“天”」

关联:


  • Please commit to the dev branch
  • For contributing new features, please supplement and improve the corresponding user guide documents
  • For bug fixes, please describe the problem and solution via code comments
  • For text improvements (such as typos and wording adjustments), please submit directly

添加一个与日期相关的模板函数,有「星期天」了
@TCOTC
Copy link
Contributor Author

TCOTC commented Dec 5, 2023

这些需要针对性修改吗?→ https://github.com/siyuan-note/siyuan/pull/9811/files

@frostime
Copy link
Contributor

frostime commented Dec 5, 2023

我觉得也许可以不用增加一个函数,给原来的函数添加一个参数。我改了一下原始的代码:

func WeekdayCN(args ...interface{}) string {
	weekdayCN := []string{"日", "一", "二", "三", "四", "五", "六"}

	var week int
	// 判断参数数量和类型
	switch len(args) {
	case 1:
		//单个参数,类型应该为 time.Time
		if t, ok := args[0].(time.Time); ok {
			week = Weekday(t)
		}
	case 2:
		// 两个参数,第一个为 string,第二个为 time.Time
		if dayStr, ok := args[0].(string); ok {
			weekdayCN[0] = dayStr
			if t, ok := args[1].(time.Time); ok {
				week = Weekday(t)
			}
		}
	default:
		// 多于一个参数,忽略
	}

	return weekdayCN[week]
}

现在 WeekdayCN 有两种调用方案,{{WeekdayCN now}} ,以及 {{WeekdayCN "天" now}}

我在 playground 测试了一下,貌似可行

package main

import (
	"html/template"
	"log"
	"os"
	"time"
)

func LastSunday() time.Time {
	now := time.Now()
	daysSinceSunday := int(now.Weekday())
	if daysSinceSunday != 0 {
		daysAgo := (daysSinceSunday + 7) % 7
		lastSunday := now.AddDate(0, 0, -daysAgo)
		return lastSunday
	}
	return now
}

func main() {
	// 创建一个新的模板
	tmpl := template.New("example")
	// 注册自定义函数
	tmpl.Funcs(template.FuncMap{
		"WeekdayCN": WeekdayCN,
		//"now":       time.Now,
		"now": LastSunday,
	})
	// 解析模板内容
	tmpl, err := tmpl.Parse(" {{now}}: 星期({{WeekdayCN now}})    星期({{ now | WeekdayCN }})  星期({{ WeekdayCN \"哈哈\" now}})   星期({{ now | WeekdayCN \"\"}})  ")
	if err != nil {
		log.Fatal(err)
	}

	// 获取当前时间
	now := LastSunday()
	// 执行模板
	err = tmpl.Execute(os.Stdout, now)
	if err != nil {
		log.Fatal(err)
	}
}

func Weekday(date time.Time) int {
	return int(date.Weekday())
}

func WeekdayCN(args ...interface{}) string {
	weekdayCN := []string{"日", "一", "二", "三", "四", "五", "六"}

	var week int
	// 判断参数数量和类型
	switch len(args) {
	case 1:
		//单个参数,类型应该为 time.Time
		if t, ok := args[0].(time.Time); ok {
			week = Weekday(t)
		}
	case 2:
		// 两个参数,第一个为 string,第二个为 time.Time
		if dayStr, ok := args[0].(string); ok {
			weekdayCN[0] = dayStr
			if t, ok := args[1].(time.Time); ok {
				week = Weekday(t)
			}
		}
	default:
		// 其他情况,忽略
	}

	return weekdayCN[week]
}

image

@TCOTC
Copy link
Contributor Author

TCOTC commented Dec 5, 2023

突然想到还有:「礼拜“一”」这种形式,感觉是不是可以再改一下

@frostime
Copy link
Contributor

frostime commented Dec 5, 2023

突然想到还有:「礼拜“一”」这种形式,感觉是不是可以再改一下

礼拜这种字就不是放在模板里的了,而是放在外面了

星期{{now | WeekdayCN}}
周{{now | WeekdayCN}}
礼拜{{now | WeekdayCN}}

@TCOTC
Copy link
Contributor Author

TCOTC commented Dec 5, 2023

感觉你这个挺合理的,就是不知道除了「周日」和「星期天」以外还能有什么。

刚才突然脑抽了,说了个完全对不上想法的例子😓,其实我本来想说的是可能有人既不用「周“一”」也不用「星期“一”」,既然现在可以用任意字符替换「日」,或许可以在这个基础上提供更高的自定义——不只是替换「日」,「一」到「六」也可以替换为其他的字符。

@frostime
Copy link
Contributor

frostime commented Dec 5, 2023

感觉你这个挺合理的,就是不知道除了「周日」和「星期天」以外还能有什么。

刚才突然脑抽了,说了个完全对不上想法的例子😓,其实我本来想说的是可能有人既不用「周“一”」也不用「星期“一”」,既然现在可以用任意字符替换「日」,或许可以在这个基础上提供更高的自定义——不只是替换「日」,「一」到「六」也可以替换为其他的字符。

其实想要自定义完全可以用 Weekday 配合自定义数组。
Weekday 返回的就是 0 ~ 6 的数字下标。

添加一个日期相关的模板函数
@TCOTC TCOTC changed the title 添加一个日期相关的模板函数 补充一个日期相关的模板函数 Dec 5, 2023
@TCOTC TCOTC changed the title 补充一个日期相关的模板函数 Add a template function about date 补充一个日期相关的模板函数 Dec 5, 2023
@88250
Copy link
Member

88250 commented Dec 5, 2023

这个暂时不考虑合并了,还是下标配合外部文本比较好吧。

@TCOTC
Copy link
Contributor Author

TCOTC commented Dec 5, 2023

感觉还是加上这个比较简单方便

@88250 88250 merged commit 3dc72e2 into siyuan-note:dev Dec 6, 2023
6 checks passed
@88250 88250 changed the title Add a template function about date 补充一个日期相关的模板函数 Add a template function about date Dec 6, 2023
@88250 88250 added this to the 2.11.2 milestone Dec 6, 2023
@TCOTC TCOTC deleted the dev branch December 6, 2023 01:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants