diff --git a/kernel/api/icon.go b/kernel/api/icon.go
index 666b4773a..a7fb8b265 100644
--- a/kernel/api/icon.go
+++ b/kernel/api/icon.go
@@ -18,7 +18,6 @@ package api
import (
"fmt"
- "math"
"net/http"
"regexp"
"strings"
@@ -186,7 +185,8 @@ func getDateInfo(dateStr string, lang string, weekdayType string) map[string]int
today := time.Now()
today = time.Date(today.Year(), today.Month(), today.Day(), 0, 0, 0, 0, today.Location())
date = time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, date.Location())
- countDown := int(math.Floor(date.Sub(today).Hours() / 24)) // 注意最大返回106751天,go的时间戳最大值
+ // countDown := int(math.Floor(date.Sub(today).Hours() / 24)) // 注意最大返回106751天,go的时间戳最大值
+ countDown := daysBetween(today, date)
return map[string]interface{}{
"year": year,
@@ -200,6 +200,43 @@ func getDateInfo(dateStr string, lang string, weekdayType string) map[string]int
}
}
+func daysBetween(date1, date2 time.Time) int {
+ // 将两个日期都调整到UTC时间的0点
+ date1 = time.Date(date1.Year(), date1.Month(), date1.Day(), 0, 0, 0, 0, time.UTC)
+ date2 = time.Date(date2.Year(), date2.Month(), date2.Day(), 0, 0, 0, 0, time.UTC)
+
+ // 确保date1不晚于date2
+ swap := false
+ if date1.After(date2) {
+ date1, date2 = date2, date1
+ swap = true
+ }
+
+ // 计算天数差
+ days := 0
+ for y := date1.Year(); y < date2.Year(); y++ {
+ if isLeapYear(y) {
+ days += 366
+ } else {
+ days += 365
+ }
+ }
+
+ // 加上最后一年的天数
+ days += int(date2.YearDay() - date1.YearDay())
+
+ // 如果原始的date1晚于date2,返回负值
+ if swap {
+ return -days
+ }
+ return days
+}
+
+// 判断是否为闰年
+func isLeapYear(year int) bool {
+ return year%4 == 0 && (year%100 != 0 || year%400 == 0)
+}
+
// Type 1: 显示年月日星期
func generateTypeOneSVG(color string, lang string, dateInfo map[string]interface{}) string {
colorScheme := getColorScheme(color)
@@ -209,8 +246,8 @@ func generateTypeOneSVG(color string, lang string, dateInfo map[string]interface
%s
- %d
- %s
+ %d
+ %s
%d
`, colorScheme.Primary, dateInfo["month"], dateInfo["day"], dateInfo["weekday"], dateInfo["year"])
@@ -225,7 +262,7 @@ func generateTypeTwoSVG(color string, lang string, dateInfo map[string]interface
%s
- %d
+ %d
%d
`, colorScheme.Primary, dateInfo["month"], dateInfo["day"], dateInfo["year"])
@@ -248,7 +285,7 @@ func generateTypeThreeSVG(color string, lang string, dateInfo map[string]interfa
%d
- %s
+ %s
`, colorScheme.Primary, colorScheme.Secondary, dateInfo["year"], dateInfo["month"])
}
@@ -269,7 +306,7 @@ func generateTypeFourSVG(color string, lang string, dateInfo map[string]interfac
- %d
+ %d
`, colorScheme.Primary, colorScheme.Secondary, dateInfo["year"])
}
@@ -291,7 +328,7 @@ func generateTypeFiveSVG(color string, lang string, dateInfo map[string]interfac
%d
- %s
+ %s
`, colorScheme.Primary, colorScheme.Secondary, dateInfo["year"], dateInfo["week"])
}
@@ -345,7 +382,7 @@ func generateTypeSixSVG(color string, lang string, weekdayType string, dateInfo
- %s
+ %s
`, colorScheme.Primary, colorScheme.Secondary, colorScheme.Primary, fontSize, weekday)
}
@@ -358,17 +395,16 @@ func generateTypeSevenSVG(color string, lang string, dateInfo map[string]interfa
var tipText, diffDaysText string
// 设置输出字符
- if diffDays == 0 {
+ switch {
+ case diffDays == 0:
switch lang {
- case "zh_CN":
- tipText = "今天"
- case "zh_CHT":
+ case "zh_CN", "zh_CHT":
tipText = "今天"
default:
tipText = "Today"
}
diffDaysText = "--"
- } else if diffDays > 0 {
+ case diffDays > 0:
switch lang {
case "zh_CN":
tipText = "还有"
@@ -378,7 +414,7 @@ func generateTypeSevenSVG(color string, lang string, dateInfo map[string]interfa
tipText = "Left"
}
diffDaysText = fmt.Sprintf("%d", diffDays)
- } else {
+ default:
switch lang {
case "zh_CN":
tipText = "已过"
@@ -387,26 +423,30 @@ func generateTypeSevenSVG(color string, lang string, dateInfo map[string]interfa
default:
tipText = "Past"
}
- diffDaysText = fmt.Sprintf("%d", int(math.Abs(float64(diffDays))))
+ absDiffDays := -diffDays
+ diffDaysText = fmt.Sprintf("%d", absDiffDays)
}
- dayStr := map[string]string{
- "zh_CN": "天",
- "zh_CHT": "天",
- "default": "days",
- }[lang]
- if dayStr == "" {
+ var dayStr string
+ switch lang {
+ case "zh_CN", "zh_CHT":
+ dayStr = "天"
+ default:
dayStr = "days"
}
-
- fontSize := 240.0
- if len(diffDaysText) >= 6 {
- fontSize = 130
- } else if len(diffDaysText) == 5 {
- fontSize = 140
- } else if len(diffDaysText) == 4 {
+ // 动态变化字体大小
+ var fontSize float64
+ switch {
+ case len(diffDaysText) <= 3:
+ fontSize = 240
+ case len(diffDaysText) == 4:
fontSize = 190
+ case len(diffDaysText) == 5:
+ fontSize = 140
+ case len(diffDaysText) >= 6:
+ fontSize = 780 / float64(len(diffDaysText))
}
+
return fmt.Sprintf(`
`, colorScheme.Primary, dateInfo["year"], dateInfo["date"], tipText, fontSize, diffDaysText, dayStr)
}
@@ -423,32 +463,45 @@ func generateTypeSevenSVG(color string, lang string, dateInfo map[string]interfa
func generateTypeEightSVG(color, content string) string {
colorScheme := getColorScheme(color)
+ // 动态变化字体大小
isChinese := regexp.MustCompile(`[\p{Han}]`).MatchString(content)
-
var fontSize float64
- switch {
- case len([]rune(content)) == 1:
- fontSize = 320
- case len([]rune(content)) == 2:
- fontSize = 240
- case len([]rune(content)) == 3:
- fontSize = 160
- case len([]rune(content)) == 4:
- fontSize = 120
- case len([]rune(content)) == 5:
- fontSize = 95
- default:
- if isChinese {
+ if isChinese {
+ switch {
+ case len([]rune(content)) == 1:
+ fontSize = 320
+ default:
fontSize = 480 / float64(len([]rune(content)))
- } else {
+ }
+ } else {
+ switch {
+ case len([]rune(content)) == 1:
+ fontSize = 480
+ case len([]rune(content)) == 2:
+ fontSize = 300
+ case len([]rune(content)) == 3:
+ fontSize = 240
+ default:
fontSize = 750 / float64(len([]rune(content)))
}
}
+ // 当内容为单个字符时,一些小写字母需要调整文字位置(暂时没法批量解决)
+ dy := "0%"
+ if len([]rune(content)) == 1 {
+ switch content {
+ case "g", "p", "y", "q":
+ dy = "-10%"
+ case "j":
+ dy = "-5%"
+ default:
+ dy = "0%"
+ }
+ }
return fmt.Sprintf(`
- `, colorScheme.Primary, fontSize, content)
+ %s
+
+ `, colorScheme.Primary, dy, fontSize, content)
}