2021-05-26 15:41:45 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2022-01-24 16:24:12 +00:00
|
|
|
"fmt"
|
2021-05-26 15:41:45 +00:00
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func MillisecondsJitter(d time.Duration, jitterInMilliseconds int) time.Duration {
|
|
|
|
n := rand.Intn(jitterInMilliseconds)
|
|
|
|
return d + time.Duration(n)*time.Millisecond
|
|
|
|
}
|
2021-06-26 12:09:26 +00:00
|
|
|
|
|
|
|
func BeginningOfTheDay(t time.Time) time.Time {
|
|
|
|
year, month, day := t.Date()
|
|
|
|
return time.Date(year, month, day, 0, 0, 0, 0, t.Location())
|
|
|
|
}
|
|
|
|
|
2021-10-29 02:40:14 +00:00
|
|
|
func Over24Hours(since time.Time) bool {
|
2022-02-15 05:55:19 +00:00
|
|
|
return time.Since(since) >= 24*time.Hour
|
2021-10-29 02:40:14 +00:00
|
|
|
}
|
2021-12-23 14:20:47 +00:00
|
|
|
|
|
|
|
func UnixMilli() int64 {
|
|
|
|
return time.Now().UnixNano() / int64(time.Millisecond)
|
2022-01-24 16:24:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ParseTimeWithFormats(strTime string, formats []string) (time.Time, error) {
|
|
|
|
for _, format := range formats {
|
|
|
|
tt, err := time.Parse(format, strTime)
|
|
|
|
if err == nil {
|
|
|
|
return tt, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return time.Time{}, fmt.Errorf("failed to parse time %s, valid formats are %+v", strTime, formats)
|
|
|
|
}
|