69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
package helper
|
||
|
||
import (
|
||
"fmt"
|
||
"time"
|
||
)
|
||
|
||
const (
|
||
DefaultParseFormatLayout = "2006-1-02 15:04:05" // 默认解析时间格式
|
||
DefaultFormatLayout = "2006-01-02 15:04:05" // 默认时间格式
|
||
)
|
||
|
||
func Parse(localTimeStr string) (time.Time, error) {
|
||
if localTimeStr == "" {
|
||
return time.Time{}, fmt.Errorf("时间参数未传递")
|
||
}
|
||
layouts := []string{
|
||
DefaultParseFormatLayout,
|
||
DefaultFormatLayout,
|
||
"2006-01-02 15:04",
|
||
"2006-1-02 15:04",
|
||
"2006-01-02",
|
||
"2006-1-02",
|
||
time.RFC3339Nano,
|
||
}
|
||
var (
|
||
localTime time.Time
|
||
err error
|
||
)
|
||
loc, _ := time.LoadLocation(GetTimeZone())
|
||
for _, layout := range layouts {
|
||
localTime, err = time.ParseInLocation(layout, localTimeStr, loc)
|
||
if err == nil {
|
||
break
|
||
}
|
||
}
|
||
if err != nil {
|
||
return time.Time{}, fmt.Errorf("时间转换错误:%v", err)
|
||
}
|
||
|
||
return localTime, nil
|
||
}
|
||
|
||
// Duration [微信]方24小时内未完成验证,自动关闭批次单,24小时后,不再触发查询了
|
||
// [我方]不管什么渠道,首次间隔5s查询,第二次后间隔5s,第三次间隔10s,大于2分钟后间隔1分钟,大于5分钟后间隔5分钟,总共只查询30分钟,后续不在查询
|
||
func Duration(t *time.Time) (int, error) {
|
||
if t == nil {
|
||
return 5, nil
|
||
}
|
||
rt := *t
|
||
if rt.IsZero() {
|
||
return 5, nil
|
||
}
|
||
duration := time.Since(rt).Abs().Minutes()
|
||
seconds := time.Since(rt).Abs().Seconds()
|
||
switch {
|
||
case duration > 30:
|
||
return 0, fmt.Errorf("已超过30分钟")
|
||
case duration > 5:
|
||
return 5 * 60, nil
|
||
case duration > 2:
|
||
return 2 * 60, nil
|
||
case seconds > 5 && seconds <= 10:
|
||
return 5, nil
|
||
default:
|
||
return 10, nil
|
||
}
|
||
}
|