-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrolling_date.go
More file actions
131 lines (111 loc) · 3.11 KB
/
rolling_date.go
File metadata and controls
131 lines (111 loc) · 3.11 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright (c) 2019-2023 Vincent Cheung (coolingfall@gmail.com).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package lork
import (
"time"
)
const (
topOfSecond periodicType = iota + 1
topOfMinute
topOfHour
topOfDay
topOfMonth
)
const (
secondsInOneMinute = 60
secondsInOneHour = 60 * secondsInOneMinute
secondsInOneDay = 24 * secondsInOneHour
)
var (
periods = []periodicType{
topOfSecond, topOfMinute, topOfHour, topOfDay, topOfMonth,
}
)
type periodicType int8
type rollingDate struct {
datePattern string
_type periodicType
}
func newRollingDate(datePattern string) *rollingDate {
rd := &rollingDate{
datePattern: datePattern,
}
rd._type = rd.calcPeriodType()
return rd
}
func (rd *rollingDate) calcPeriodType() periodicType {
now := time.Now()
for _, t := range periods {
tl := now.Format(rd.datePattern)
next := rd.endOfThisPeriod(t, now)
tr := next.Format(rd.datePattern)
if tl != tr {
return t
}
}
return topOfSecond
}
func (rd *rollingDate) _endOfNextNPeriod(
periodicType periodicType, now time.Time, periods int) time.Time {
switch periodicType {
case topOfMinute:
return time.Date(
now.Year(), now.Month(), now.Day(), now.Hour(),
now.Minute()+periods, 0, 0, now.Location())
case topOfHour:
return time.Date(
now.Year(), now.Month(), now.Day(),
now.Hour()+periods, 0, 0, 0, now.Location())
case topOfDay:
return time.Date(
now.Year(), now.Month(), now.Day()+periods, 0, 0, 0, 0, now.Location())
case topOfMonth:
return time.Date(
now.Year(), now.Month()+time.Month(periods), 1, 0, 0, 0, 0, now.Location())
case topOfSecond:
fallthrough
default:
return time.Date(
now.Year(), now.Month(), now.Day(), now.Hour(),
now.Minute(), now.Second()+periods, 0, now.Location())
}
}
func (rd *rollingDate) endOfNextNPeriod(now time.Time, periods int) time.Time {
return rd._endOfNextNPeriod(rd._type, now, periods)
}
func (rd *rollingDate) endOfThisPeriod(pt periodicType, now time.Time) time.Time {
return rd._endOfNextNPeriod(pt, now, 1)
}
func (rd *rollingDate) next(t time.Time) time.Time {
return rd.endOfNextNPeriod(t, 1)
}
func (rd *rollingDate) periodCrossed(start int64, end int64) int {
diff := end - start
switch rd._type {
case topOfMinute:
return int(diff / secondsInOneMinute)
case topOfHour:
return int(diff / secondsInOneHour)
case topOfDay:
return int(diff / secondsInOneDay)
case topOfMonth:
startTime := time.Unix(start, 0)
endTime := time.Unix(end, 0)
yearDiff := endTime.Year() - startTime.Year()
monthDiff := endTime.Month() - startTime.Month()
return yearDiff*12 + int(monthDiff)
default:
return 0
}
}