custom_group_usage_rollup.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package service
  2. import (
  3. "context"
  4. "time"
  5. appTimezone "github.com/Wei-Shaw/sub2api/internal/pkg/timezone"
  6. )
  7. const groupUsageDateFormat = "2006-01-02"
  8. // GroupUsageRollupRepository 是分组日汇总的可选持久化能力。
  9. type GroupUsageRollupRepository interface {
  10. SyncGroupUsageRollups(ctx context.Context, todayStart time.Time) error
  11. }
  12. // GroupUsageTimezoneName 返回服务端配置的时区名称。
  13. func GroupUsageTimezoneName() string {
  14. return appTimezone.Location().String()
  15. }
  16. // GroupUsageTodayStart 返回指定时刻在服务端配置时区内的自然日 UTC 起点。
  17. func GroupUsageTodayStart(at time.Time) time.Time {
  18. return appTimezone.StartOfDay(at).UTC()
  19. }
  20. // GroupUsageYesterdayStart 返回指定时刻前一个本地日历日的 UTC 起点。
  21. func GroupUsageYesterdayStart(at time.Time) time.Time {
  22. return appTimezone.StartOfDay(at).AddDate(0, 0, -1).UTC()
  23. }
  24. // GroupUsageDate 返回指定时刻在服务端配置时区内的日期。
  25. func GroupUsageDate(at time.Time) string {
  26. return at.In(appTimezone.Location()).Format(groupUsageDateFormat)
  27. }
  28. // ParseGroupUsageDate 解析服务端配置时区内的日期并返回其零点。
  29. func ParseGroupUsageDate(value string) (time.Time, error) {
  30. return appTimezone.ParseInLocation(groupUsageDateFormat, value)
  31. }