All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 12m59s
30 lines
1.2 KiB
Go
30 lines
1.2 KiB
Go
package carrierthreshold
|
||
|
||
import (
|
||
"time"
|
||
|
||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||
)
|
||
|
||
// shanghaiLocation 是计费周期边界的固定时区。
|
||
// 中国自 1991 年起不实行夏令时,使用固定偏移可避免依赖宿主机 tzdata。
|
||
var shanghaiLocation = time.FixedZone("Asia/Shanghai", 8*60*60)
|
||
|
||
// PeriodStart 计算 now 所属计费周期的起点(上海时区零点)。
|
||
//
|
||
// 周期起点 = 本月重置日 0 点,若 now 早于本月重置日 0 点则取上月重置日 0 点。
|
||
// resetDay 由该卡所属运营商(锁行自身的 carrier)的 data_reset_day 提供,合法范围 1-28,
|
||
// 因此每个月都有定义。该口径与 isTrafficResetWindow(判断读数回落是否为合法清零的观测窗口)
|
||
// 是两个独立口径,互不修改。
|
||
func PeriodStart(now time.Time, resetDay int) (time.Time, error) {
|
||
if resetDay < MinResetDay || resetDay > MaxResetDay {
|
||
return time.Time{}, errors.New(errors.CodeInvalidParam, "运营商上游流量重置日必须在 1-28 之间")
|
||
}
|
||
local := now.In(shanghaiLocation)
|
||
current := time.Date(local.Year(), local.Month(), resetDay, 0, 0, 0, 0, shanghaiLocation)
|
||
if !local.Before(current) {
|
||
return current, nil
|
||
}
|
||
return current.AddDate(0, -1, 0), nil
|
||
}
|