-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime_period.go
63 lines (57 loc) · 1.48 KB
/
time_period.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package rotatelogs
import "time"
type (
TimePeriod string
)
const (
Minutely TimePeriod = "minutely"
Hourly TimePeriod = "hourly"
Daily TimePeriod = "daily"
Monthly TimePeriod = "monthly"
)
func (slf TimePeriod) isValid() bool {
return slf == Minutely || slf == Hourly || slf == Daily || slf == Monthly
}
func (slf TimePeriod) TimeFormat() string {
switch slf {
case Minutely:
return "20060102-1504"
case Hourly:
return "20060102-1500"
case Daily:
return "20060102"
case Monthly:
return "20060101"
default:
return "20060102"
}
}
func (slf TimePeriod) UntilNextTime(now time.Time) time.Duration {
switch slf {
case Minutely:
year, month, day := now.Date()
date := time.Date(year, month, day, now.Hour(), now.Minute(), 0, 0, now.Location())
next := date.Add(time.Minute)
return next.Sub(now)
case Hourly:
year, month, day := now.Date()
date := time.Date(year, month, day, now.Hour(), 0, 0, 0, now.Location())
next := date.Add(time.Hour)
return next.Sub(now)
case Daily:
year, month, day := now.Date()
date := time.Date(year, month, day, 0, 0, 0, 0, now.Location())
next := date.AddDate(0, 0, 1)
return next.Sub(now)
case Monthly:
year, month, _ := now.Date()
date := time.Date(year, month, 1, 0, 0, 0, 0, now.Location())
next := date.AddDate(0, 1, 0)
return next.Sub(now)
default:
year, month, day := now.Date()
date := time.Date(year, month, day, 0, 0, 0, 0, now.Location())
next := date.AddDate(0, 0, 1)
return next.Sub(now)
}
}