Files
EdgeAPI/internal/utils/time.go

157 lines
3.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package utils
import (
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/types"
timeutil "github.com/iwind/TeaGo/utils/time"
"regexp"
"time"
)
// RangeDays 计算日期之间的所有日期格式为YYYYMMDD
func RangeDays(dayFrom string, dayTo string) ([]string, error) {
ok, err := regexp.MatchString(`^\d{8}$`, dayFrom)
if err != nil {
return nil, err
}
if !ok {
return nil, errors.New("invalid 'dayFrom'")
}
ok, err = regexp.MatchString(`^\d{8}$`, dayTo)
if err != nil {
return nil, err
}
if !ok {
return nil, errors.New("invalid 'dayTo'")
}
if dayFrom > dayTo {
dayFrom, dayTo = dayTo, dayFrom
}
// 不能超过N天
maxDays := 100 - 1 // -1 是去掉默认加入的dayFrom
result := []string{dayFrom}
year := types.Int(dayFrom[:4])
month := types.Int(dayFrom[4:6])
day := types.Int(dayFrom[6:])
t := time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.Local)
for {
t = t.AddDate(0, 0, 1)
newDay := timeutil.Format("Ymd", t)
if newDay <= dayTo {
result = append(result, newDay)
} else {
break
}
maxDays--
if maxDays <= 0 {
break
}
}
return result, nil
}
// RangeMonths 计算日期之间的所有月份格式为YYYYMM
func RangeMonths(dayFrom string, dayTo string) ([]string, error) {
ok, err := regexp.MatchString(`^\d{8}$`, dayFrom)
if err != nil {
return nil, err
}
if !ok {
return nil, errors.New("invalid 'dayFrom'")
}
ok, err = regexp.MatchString(`^\d{8}$`, dayTo)
if err != nil {
return nil, err
}
if !ok {
return nil, errors.New("invalid 'dayTo'")
}
if dayFrom > dayTo {
dayFrom, dayTo = dayTo, dayFrom
}
result := []string{dayFrom[:6]}
year := types.Int(dayFrom[:4])
month := types.Int(dayFrom[4:6])
day := types.Int(dayFrom[6:])
t := time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.Local)
for {
t = t.AddDate(0, 0, 20)
newDay := timeutil.Format("Ymd", t)
if newDay <= dayTo {
var monthString = newDay[:6]
if !lists.ContainsString(result, monthString) {
result = append(result, monthString)
}
} else {
break
}
}
var endMonth = dayTo[:6]
if !lists.ContainsString(result, endMonth) {
result = append(result, endMonth)
}
return result, nil
}
// RangeHours 计算小时之间的所有小时格式为YYYYMMDDHH
func RangeHours(hourFrom string, hourTo string) ([]string, error) {
ok, err := regexp.MatchString(`^\d{10}$`, hourFrom)
if err != nil {
return nil, err
}
if !ok {
return nil, errors.New("invalid 'hourFrom'")
}
ok, err = regexp.MatchString(`^\d{10}$`, hourTo)
if err != nil {
return nil, err
}
if !ok {
return nil, errors.New("invalid 'hourTo'")
}
if hourFrom > hourTo {
hourFrom, hourTo = hourTo, hourFrom
}
// 不能超过N天
maxHours := 100 - 1 // -1 是去掉默认加入的dayFrom
result := []string{hourFrom}
year := types.Int(hourFrom[:4])
month := types.Int(hourFrom[4:6])
day := types.Int(hourFrom[6:8])
hour := types.Int(hourFrom[8:])
t := time.Date(year, time.Month(month), day, hour, 0, 0, 0, time.Local)
for {
t = t.Add(1 * time.Hour)
newHour := timeutil.Format("YmdH", t)
if newHour <= hourTo {
result = append(result, newHour)
} else {
break
}
maxHours--
if maxHours <= 0 {
break
}
}
return result, nil
}