This repository has been archived by the owner on Jan 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtime_window.go
179 lines (164 loc) · 4.63 KB
/
time_window.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Kiebitz - Privacy-Friendly Appointment Scheduling
// Copyright (C) 2021-2021 The Kiebitz Authors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version. Additional terms
// as defined in section 7 of the license (e.g. regarding attribution)
// are specified at https://kiebitz.eu/en/docs/open-source/additional-terms.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package services
import (
"time"
)
type TimeWindow struct {
From int64
To int64
Type string
}
type TimeWindowFunc func(int64) TimeWindow
func (t *TimeWindow) EqualTo(tw *TimeWindow) bool {
if t.Type != tw.Type || t.From != tw.From || t.To != tw.To {
return false
}
return true
}
func Second(value int64) TimeWindow {
t := time.Unix(value/1e9, value%1e9).UTC()
from := time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), 0, t.Location())
to := from.Add(time.Second * 1)
return TimeWindow{
From: from.UnixNano(),
To: to.UnixNano(),
Type: "second",
}
}
func Minute(value int64) TimeWindow {
t := time.Unix(value/1e9, value%1e9).UTC()
from := time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), 0, 0, t.Location())
to := from.Add(time.Minute * 1)
return TimeWindow{
From: from.UnixNano(),
To: to.UnixNano(),
Type: "minute",
}
}
func QuarterHour(value int64) TimeWindow {
t := time.Unix(value/1e9, value%1e9).UTC()
q := t.Minute() / 15
from := time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), q*15, 0, 0, t.Location())
to := from.Add(time.Minute * 15)
return TimeWindow{
From: from.UnixNano(),
To: to.UnixNano(),
Type: "quarterHour",
}
}
func Hour(value int64) TimeWindow {
t := time.Unix(value/1e9, value%1e9).UTC()
from := time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), 0, 0, 0, t.Location())
to := from.Add(time.Hour * 1)
return TimeWindow{
From: from.UnixNano(),
To: to.UnixNano(),
Type: "hour",
}
}
func Day(value int64) TimeWindow {
t := time.Unix(value/1e9, value%1e9).UTC()
from := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
to := from.AddDate(0, 0, 1)
return TimeWindow{
From: from.UnixNano(),
To: to.UnixNano(),
Type: "day",
}
}
func Week(value int64) TimeWindow {
t := time.Unix(value/1e9, value%1e9).UTC()
from := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
wd := (int(t.Weekday()) - 1) % 7 // weekday starting from Monday
if wd < 0 {
wd += 7
}
from = from.AddDate(0, 0, -wd)
to := from.AddDate(0, 0, 7)
return TimeWindow{
From: from.UnixNano(),
To: to.UnixNano(),
Type: "week",
}
}
func Month(value int64) TimeWindow {
t := time.Unix(value/1e9, value%1e9).UTC()
from := time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location())
to := from.AddDate(0, 1, 0)
return TimeWindow{
From: from.UnixNano(),
To: to.UnixNano(),
Type: "month",
}
}
func MakeTimeWindow(t int64, twType string) TimeWindow {
switch twType {
case "second":
return Second(t)
case "minute":
return Minute(t)
case "hour":
return Hour(t)
case "quarterHour":
return QuarterHour(t)
case "day":
return Day(t)
case "week":
return Week(t)
case "month":
return Month(t)
}
return TimeWindow{}
}
func (t *TimeWindow) Copy() TimeWindow {
return TimeWindow{
Type: t.Type,
From: t.From,
To: t.To,
}
}
func (t *TimeWindow) IncreaseBy(n int64) {
from := time.Unix(t.From/1e9, t.From%1e9).UTC()
to := time.Unix(t.To/1e9, t.To%1e9).UTC()
switch t.Type {
case "second":
from = from.Add(time.Second * time.Duration(n))
to = to.Add(time.Second * time.Duration(n))
case "minute":
from = from.Add(time.Minute * time.Duration(n))
to = to.Add(time.Minute * time.Duration(n))
case "quarterHour":
from = from.Add(time.Minute * time.Duration(15*n))
to = to.Add(time.Minute * time.Duration(15*n))
case "hour":
from = from.Add(time.Hour * time.Duration(n))
to = to.Add(time.Hour * time.Duration(n))
case "day":
from = from.AddDate(0, 0, int(n))
to = to.AddDate(0, 0, int(n))
case "week":
from = from.AddDate(0, 0, 7*int(n))
to = to.AddDate(0, 0, 7*int(n))
case "month":
from = from.AddDate(0, int(n), 0)
to = to.AddDate(0, int(n), 0)
}
t.From = from.UnixNano()
t.To = to.UnixNano()
}