first commit

This commit is contained in:
Blizzard
2026-02-27 13:54:01 +08:00
commit fc585fa4df
127 changed files with 18548 additions and 0 deletions
+30
View File
@@ -0,0 +1,30 @@
package timer
import (
"strconv"
"strings"
"time"
)
// ParseDuration 解析时间
func ParseDuration(d string) (time.Duration, error) {
d = strings.TrimSpace(d)
dr, err := time.ParseDuration(d)
if err == nil {
return dr, nil
}
if strings.Contains(d, "d") {
index := strings.Index(d, "d")
hour, _ := strconv.Atoi(d[:index])
dr = time.Hour * 24 * time.Duration(hour)
ndr, err := time.ParseDuration(d[index+1:])
if err != nil {
return dr, nil
}
return dr + ndr, nil
}
dv, err := strconv.ParseInt(d, 10, 64)
return time.Duration(dv), err
}
+48
View File
@@ -0,0 +1,48 @@
package timer
import (
"fmt"
"time"
)
// TimeInterval 计算两个时间的间隔,超过24小时返回天数,否则返回几小时前
func TimeInterval(targetTime time.Time) string {
// 1. 将当前时间和目标时间统一转换为本地时区(time.Local)
now := time.Now().In(time.Local) // 当前时间转为本地时区
target := targetTime.In(time.Local) // 目标时间转为本地时区
// 2. 计算时间差(取绝对值,避免因目标时间在未来导致负数)
var diff time.Duration
if now.After(target) {
diff = now.Sub(target)
} else {
diff = target.Sub(now)
}
// 3. 转换为总小时数(取整数部分,自动截断小数)
// 转换为总分钟数(取整数部分,自动截断秒数)
totalMinutes := int(diff.Minutes())
// 按不同阈值返回对应格式
switch {
case totalMinutes >= 24*60: // 24小时 = 1440分钟
days := totalMinutes / (24 * 60)
return fmt.Sprintf("%d天前", days)
case totalMinutes >= 60: // 1小时 = 60分钟
hours := totalMinutes / 60
return fmt.Sprintf("%d小时前", hours)
case totalMinutes < 1:
return "刚刚"
default: // 不足1小时
return fmt.Sprintf("%d分钟前", totalMinutes)
}
}
// GetZeroTime 获取当天零点时间
func GetZeroTime() time.Time {
now := time.Now()
// 2. 使用当天的年月日,将时分秒纳秒设为0,并保留原时区(Location)
zeroTime := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
return zeroTime
}
+219
View File
@@ -0,0 +1,219 @@
package timer
import (
"sync"
"github.com/robfig/cron/v3"
)
type Timer interface {
FindCronList() map[string]*taskManager
AddTaskByFuncWithSecond(cronName string, spec string, fun func(), taskName string, options ...cron.Option) (cron.EntryID, error) // 添加Task Func以秒的形式加入
AddTaskByJobWithSecond(cronName string, spec string, job interface{ Run() }, taskName string, options ...cron.Option) (cron.EntryID, error) // 添加Task Job以秒的形式加入
AddTaskByFunc(cronName string, spec string, task func(), taskName string, options ...cron.Option) (cron.EntryID, error) // 添加Task Func加入
AddTaskByJob(cronName string, spec string, job interface{ Run() }, taskName string, options ...cron.Option) (cron.EntryID, error) // 添加Task Job加入
FindCron(cronName string) (*taskManager, bool) //获取对应taskName的cron 可能会为空
StartCron(cronName string) // 启动对应cron
StopCron(cronName string) // 停止对应cron
FindTask(cronName string, taskName string) (*task, bool) //获取对应taskName的task
RemoveTask(cronName string, id int) //删除对应taskName的task
RemoveTaskByName(cronName string, taskName string) //删除对应taskName的task
Clear(cronName string) //清空对应cronName的task
Close() // 关闭所有定时任务
}
type task struct {
EntryId cron.EntryID
Spec string
TaskName string
}
type taskManager struct {
corn *cron.Cron
tasks map[cron.EntryID]*task
}
// timer 定时任务管理
type timer struct {
cronList map[string]*taskManager
sync.Mutex
}
// AddTaskByFuncWithSecond 通过函数的方法使用WithSeconds添加任务
func (t *timer) AddTaskByFuncWithSecond(cronName string, spec string, fun func(), taskName string, option ...cron.Option) (cron.EntryID, error) {
t.Lock()
defer t.Unlock()
option = append(option, cron.WithSeconds())
if _, ok := t.cronList[cronName]; !ok {
tasks := make(map[cron.EntryID]*task)
t.cronList[cronName] = &taskManager{
corn: cron.New(option...),
tasks: tasks,
}
}
id, err := t.cronList[cronName].corn.AddFunc(spec, fun)
t.cronList[cronName].corn.Start()
t.cronList[cronName].tasks[id] = &task{
EntryId: id,
Spec: spec,
TaskName: taskName,
}
return id, err
}
// AddTaskByFunc 通过函数的方法添加任务
func (t *timer) AddTaskByFunc(cronName string, spec string, fun func(), taskName string, option ...cron.Option) (cron.EntryID, error) {
t.Lock()
defer t.Unlock()
if _, ok := t.cronList[cronName]; !ok {
tasks := make(map[cron.EntryID]*task)
t.cronList[cronName] = &taskManager{
corn: cron.New(option...),
tasks: tasks,
}
}
id, err := t.cronList[cronName].corn.AddFunc(spec, fun)
t.cronList[cronName].corn.Start()
t.cronList[cronName].tasks[id] = &task{
EntryId: id,
Spec: spec,
TaskName: taskName,
}
return id, err
}
// AddTaskByJobWithSecond 通过Job的方法使用WithSeconds添加任务
func (t *timer) AddTaskByJobWithSecond(cronName string, spec string, job interface{ Run() }, taskName string, option ...cron.Option) (cron.EntryID, error) {
t.Lock()
defer t.Unlock()
option = append(option, cron.WithSeconds())
if _, ok := t.cronList[cronName]; !ok {
tasks := make(map[cron.EntryID]*task)
t.cronList[cronName] = &taskManager{
corn: cron.New(option...),
tasks: tasks,
}
}
id, err := t.cronList[cronName].corn.AddJob(spec, job)
t.cronList[cronName].corn.Start()
t.cronList[cronName].tasks[id] = &task{
EntryId: id,
Spec: spec,
TaskName: taskName,
}
return id, err
}
// AddTaskByJob 通过Job的方法添加任务
func (t *timer) AddTaskByJob(cronName string, spec string, job interface{ Run() }, taskName string, option ...cron.Option) (cron.EntryID, error) {
t.Lock()
defer t.Unlock()
if _, ok := t.cronList[cronName]; !ok {
tasks := make(map[cron.EntryID]*task)
t.cronList[cronName] = &taskManager{
corn: cron.New(option...),
tasks: tasks,
}
}
id, err := t.cronList[cronName].corn.AddJob(spec, job)
t.cronList[cronName].corn.Start()
t.cronList[cronName].tasks[id] = &task{
EntryId: id,
Spec: spec,
TaskName: taskName,
}
return id, err
}
// FindCron 获取对应cronName的cron 可能会为空
func (t *timer) FindCron(cronName string) (*taskManager, bool) {
t.Lock()
defer t.Unlock()
v, ok := t.cronList[cronName]
return v, ok
}
// FindTask 获取对应taskName的task
func (t *timer) FindTask(cronName string, taskName string) (*task, bool) {
t.Lock()
defer t.Unlock()
v, ok := t.cronList[cronName]
if !ok {
return nil, false
}
for _, t2 := range v.tasks {
if t2.TaskName == taskName {
return t2, true
}
}
return nil, false
}
// FindCronList 获取所有cron
func (t *timer) FindCronList() map[string]*taskManager {
t.Lock()
defer t.Unlock()
return t.cronList
}
// StartCron 启动对应cron
func (t *timer) StartCron(cronName string) {
t.Lock()
defer t.Unlock()
if v, ok := t.cronList[cronName]; ok {
v.corn.Start()
}
}
// StopCron 停止对应cron
func (t *timer) StopCron(cronName string) {
t.Lock()
defer t.Unlock()
if v, ok := t.cronList[cronName]; ok {
v.corn.Stop()
}
}
// RemoveTask 从cronName 删除指定任务
func (t *timer) RemoveTask(cronName string, id int) {
t.Lock()
defer t.Unlock()
if v, ok := t.cronList[cronName]; ok {
v.corn.Remove(cron.EntryID(id))
delete(v.tasks, cron.EntryID(id))
}
}
// RemoveTaskByName 从cronName 删除指定任务
func (t *timer) RemoveTaskByName(cronName string, taskName string) {
fTask, ok := t.FindTask(cronName, taskName)
if !ok {
return
}
t.RemoveTask(cronName, int(fTask.EntryId))
}
// Clear 清空对应cronName的task
func (t *timer) Clear(cronName string) {
t.Lock()
defer t.Unlock()
if v, ok := t.cronList[cronName]; ok {
v.corn.Stop()
delete(t.cronList, cronName)
}
}
// Close 关闭所有定时任务 释放资源
func (t *timer) Close() {
t.Lock()
defer t.Unlock()
for _, v := range t.cronList {
v.corn.Stop()
}
}
// NewTimerTask 创建定时任务
func NewTimerTask() Timer {
return &timer{
cronList: make(map[string]*taskManager),
}
}