plugins/utils/helper/time.go

69 lines
1.6 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 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
}
}