2026-04-24 13:04:06 +08:00

71 lines
1.7 KiB
Go

package invite
import (
"strings"
"time"
)
// defaultIfBlank 在字符串为空时返回默认值。
func defaultIfBlank(value, fallback string) string {
if strings.TrimSpace(value) == "" {
return fallback
}
return value
}
// normalizeSysOrigin 统一系统标识格式。
func normalizeSysOrigin(value string) string {
return strings.TrimSpace(value)
}
// normalizeTimezone 统一时区配置,缺省回退到默认时区。
func normalizeTimezone(value string) string {
value = strings.TrimSpace(value)
if value == "" {
return defaultTimezone
}
return value
}
// monthKeyAt 计算某个时间点在指定时区下的月份标识。
func monthKeyAt(t time.Time, timezone string) string {
location, err := time.LoadLocation(normalizeTimezone(timezone))
if err != nil {
location = time.FixedZone(defaultTimezone, 3*3600)
}
return t.In(location).Format("200601")
}
// monthResetInfoAt 返回指定时间所在活动月的结束时间和剩余秒数。
func monthResetInfoAt(t time.Time, timezone string) (time.Time, int64) {
location, err := time.LoadLocation(normalizeTimezone(timezone))
if err != nil {
location = time.FixedZone(defaultTimezone, 3*3600)
}
localTime := t.In(location)
periodEnd := time.Date(localTime.Year(), localTime.Month()+1, 1, 0, 0, 0, 0, location)
seconds := int64(periodEnd.Sub(localTime).Seconds())
if seconds < 0 {
seconds = 0
}
return periodEnd, seconds
}
// digitsOnly 判断字符串是否只包含数字。
func digitsOnly(value string) bool {
for _, ch := range value {
if ch < '0' || ch > '9' {
return false
}
}
return true
}
// truncate 截断字符串,避免超出数据库字段长度。
func truncate(value string, size int) string {
if len(value) <= size {
return value
}
return value[:size]
}