diff --git a/go.mod b/go.mod index bec361d..a5a189d 100644 --- a/go.mod +++ b/go.mod @@ -48,6 +48,8 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.0.8 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/samber/do/v2 v2.0.0-beta.5 // indirect + github.com/samber/go-type-to-string v1.2.0 // indirect github.com/smarty/assertions v1.15.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/templexxx/cpu v0.1.0 // indirect diff --git a/go.sum b/go.sum index 49a8bcd..20d695c 100644 --- a/go.sum +++ b/go.sum @@ -127,6 +127,10 @@ github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTE github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8= github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/samber/do/v2 v2.0.0-beta.5 h1:KpQhVkkzDlsLSDC5WXgyCL8Q3SqOYoInFJIbvntPazM= +github.com/samber/do/v2 v2.0.0-beta.5/go.mod h1:FNMy1RSKMX11Ag8v4KW95n9k+ZkCXn8GuvDKufVKN9E= +github.com/samber/go-type-to-string v1.2.0 h1:Pvdqx3r/EHn9/DTKoW6RoHz/850s5yV1vA6MqKKG5Ys= +github.com/samber/go-type-to-string v1.2.0/go.mod h1:jpU77vIDoIxkahknKDoEx9C8bQ1ADnh2sotZ8I4QqBU= github.com/smarty/assertions v1.15.0 h1:cR//PqUBUiQRakZWqBiFFQ9wb8emQGDb0HeGdqGByCY= github.com/smarty/assertions v1.15.0/go.mod h1:yABtdzeQs6l1brC900WlRNwj6ZR55d7B+E8C6HtKdec= github.com/smartystreets/goconvey v1.8.1 h1:qGjIddxOk4grTu9JPOU31tVfq3cNdBlNa5sSznIX1xY= diff --git a/modular/service.go b/modular/service.go index eea158b..b192d70 100644 --- a/modular/service.go +++ b/modular/service.go @@ -27,6 +27,12 @@ func RegisterServices(s ...Service) { application.RegisterServices(s...) } +// RegisterService 注册服务 +func RegisterService[S Service](service S) S { + application.RegisterServices(service) + return service +} + func newService(instance Service) *service { vof := reflect.ValueOf(instance) return &service{ diff --git a/server/server.go b/server/server.go index ad6cc94..4ea9b6f 100644 --- a/server/server.go +++ b/server/server.go @@ -104,6 +104,11 @@ func BindData(srv *Server, name string, data any) { srv.BindData(name, data) } +// GetNetwork 返回服务器网络类型 +func (srv *Server) GetNetwork() Network { + return srv.network +} + // LoadData 加载绑定的服务器数据 func (srv *Server) LoadData(name string, data any) any { return srv.data[name] diff --git a/utils/collection/listings/priority_slice.go b/utils/collection/listings/priority_slice.go index 6877b48..fe70d99 100644 --- a/utils/collection/listings/priority_slice.go +++ b/utils/collection/listings/priority_slice.go @@ -56,6 +56,15 @@ func (slf *PrioritySlice[V]) Appends(priority int, vs ...V) { slf.sort() } +// AppendByOptionalPriority 添加元素 +func (slf *PrioritySlice[V]) AppendByOptionalPriority(v V, priority ...int) { + if len(priority) == 0 { + slf.Append(v, 0) + } else { + slf.Append(v, priority[0]) + } +} + // Get 获取元素 func (slf *PrioritySlice[V]) Get(index int) (V, int) { i := slf.items[index] diff --git a/utils/times/helper.go b/utils/times/helper.go index d14e812..81a84ad 100644 --- a/utils/times/helper.go +++ b/utils/times/helper.go @@ -15,32 +15,32 @@ const ( Week = Day * 7 ) -// GetCurrWeekDate 获取以 week 为周一的本周 week 时间 -func GetCurrWeekDate(now time.Time, week time.Weekday) time.Time { +// GetWeekdayDateRelativeToNowWithOffset 获取相对于当前日期所在周的指定星期的日期,可以根据需要进行周数的偏移。 +// - now:当前时间。 +// - week:要获取日期的目标星期。 +// - offsetWeeks:要偏移的周数,正数表示向未来偏移,负数表示向过去偏移。 +// +// 该函数通常适用于排行榜等场景,例如排行榜为每周六更新,那么通过该函数可以获取到上周排行榜、本周排行榜的准确日期 +// +// 该函数将不会保留 now 的时分秒信息,如果需要,可使用 GetWeekdayTimeRelativeToNowWithOffset 函数 +func GetWeekdayDateRelativeToNowWithOffset(now time.Time, week time.Weekday, offsetWeeks int) time.Time { w := int(week) monday := GetMondayZero(now) var curr time.Time if WeekDay(now) >= w { - curr = monday.AddDate(0, 0, w-1) + curr = monday.AddDate(0, 0, (w-1)+offsetWeeks*7) } else { - curr = monday.AddDate(0, 0, w-1) + curr = monday.AddDate(0, 0, (w-1)+offsetWeeks*7) curr = curr.AddDate(0, 0, -7) } return curr } -// GetLastWeekDate 获取以 week 为周一的特定时间 now 的上周 week 时间 -func GetLastWeekDate(now time.Time, week time.Weekday) time.Time { - w := int(week) - monday := GetMondayZero(now) - var last time.Time - if WeekDay(now) >= w { - last = monday.AddDate(0, 0, -(7 - w + 1)) - } else { - last = monday.AddDate(0, 0, -(7 - w + 1)) - last = last.AddDate(0, 0, -7) - } - return last +// GetWeekdayTimeRelativeToNowWithOffset 获取相对于当前日期所在周的指定星期的日期,并根据传入的 now 参数保留时、分、秒等时间信息。 +// - 参数解释可参考 GetWeekdayDateRelativeToNowWithOffset 函数 +func GetWeekdayTimeRelativeToNowWithOffset(now time.Time, week time.Weekday, offsetWeeks int) time.Time { + curr := GetWeekdayDateRelativeToNowWithOffset(now, week, offsetWeeks) + return time.Date(curr.Year(), curr.Month(), curr.Day(), now.Hour(), now.Minute(), now.Second(), now.Nanosecond(), now.Location()) } // GetMonthDays 获取一个时间当月共有多少天 diff --git a/utils/times/helper_test.go b/utils/times/helper_test.go new file mode 100644 index 0000000..58a5022 --- /dev/null +++ b/utils/times/helper_test.go @@ -0,0 +1,17 @@ +package times_test + +import ( + "github.com/kercylan98/minotaur/utils/times" + "testing" + "time" +) + +func TestGetCurrWeekDate(t *testing.T) { + now := time.Now() + date := time.Date(2024, 3, 1, now.Hour(), now.Minute(), now.Second(), now.Nanosecond(), time.Local) + + for i := 0; i < 31; i++ { + target := date.AddDate(0, 0, i) + t.Logf(target.Format(time.DateTime) + " -> " + times.GetWeekdayTimeRelativeToNowWithOffset(target, 6, 1).Format(time.DateTime)) + } +}