forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_range.go
178 lines (138 loc) · 3.92 KB
/
time_range.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
package internal
import (
"errors"
"time"
)
//TimeOfDay represents the time of day
type TimeOfDay struct {
hour, minute, second int
d time.Duration
}
const shortForm = "15:04:05"
var errParseTime = errors.New("Time must be in the format HH:MM:SS")
//NewTimeOfDay returns a newly initialized TimeOfDay
func NewTimeOfDay(hour, minute, second int) TimeOfDay {
d := time.Duration(second)*time.Second +
time.Duration(minute)*time.Minute +
time.Duration(hour)*time.Hour
return TimeOfDay{hour: hour, minute: minute, second: second, d: d}
}
//ParseTimeOfDay parses a TimeOfDay from a string in the format HH:MM:SS
func ParseTimeOfDay(str string) (TimeOfDay, error) {
t, err := time.Parse(shortForm, str)
if err != nil {
return TimeOfDay{}, errParseTime
}
return NewTimeOfDay(t.Clock()), nil
}
//TimeRange represents a time band in a given time zone
type TimeRange struct {
startTime, endTime TimeOfDay
startDay, endDay *time.Weekday
loc *time.Location
}
//NewUTCTimeRange returns a time range in UTC
func NewUTCTimeRange(start, end TimeOfDay) *TimeRange {
return NewTimeRangeInLocation(start, end, time.UTC)
}
//NewTimeRangeInLocation returns a time range in a given location
func NewTimeRangeInLocation(start, end TimeOfDay, loc *time.Location) *TimeRange {
if loc == nil {
panic("time: missing Location in call to NewTimeRangeInLocation")
}
return &TimeRange{startTime: start, endTime: end, loc: loc}
}
//NewUTCWeekRange returns a weekly TimeRange
func NewUTCWeekRange(startTime, endTime TimeOfDay, startDay, endDay time.Weekday) *TimeRange {
return NewWeekRangeInLocation(startTime, endTime, startDay, endDay, time.UTC)
}
//NewWeekRangeInLocation returns a time range in a given location
func NewWeekRangeInLocation(startTime, endTime TimeOfDay, startDay, endDay time.Weekday, loc *time.Location) *TimeRange {
r := NewTimeRangeInLocation(startTime, endTime, loc)
r.startDay = &startDay
r.endDay = &endDay
return r
}
func (r *TimeRange) isInTimeRange(t time.Time) bool {
t = t.In(r.loc)
ts := NewTimeOfDay(t.Clock()).d
if r.startTime.d < r.endTime.d {
return r.startTime.d <= ts && ts <= r.endTime.d
}
return !(r.endTime.d < ts && ts < r.startTime.d)
}
func (r *TimeRange) isInWeekRange(t time.Time) bool {
t = t.In(r.loc)
day := t.Weekday()
if *r.startDay == *r.endDay {
if day == *r.startDay {
return r.isInTimeRange(t)
}
if r.startTime.d < r.endTime.d {
return false
}
return true
}
switch {
case *r.startDay < *r.endDay:
if day < *r.startDay || *r.endDay < day {
return false
}
default:
if *r.endDay < day && day < *r.startDay {
return false
}
}
timeOfDay := NewTimeOfDay(t.Clock())
if day == *r.startDay {
return timeOfDay.d >= r.startTime.d
}
if day == *r.endDay {
return timeOfDay.d <= r.endTime.d
}
return true
}
//IsInRange returns true if time t is within in the time range
func (r *TimeRange) IsInRange(t time.Time) bool {
if r == nil {
return true
}
if r.startDay != nil {
return r.isInWeekRange(t)
}
return r.isInTimeRange(t)
}
//IsInSameRange determines if two points in time are in the same time range
func (r *TimeRange) IsInSameRange(t1, t2 time.Time) bool {
if r == nil {
return true
}
if !(r.IsInRange(t1) && r.IsInRange(t2)) {
return false
}
if t2.Before(t1) {
t1, t2 = t2, t1
}
t1 = t1.In(r.loc)
t1Time := NewTimeOfDay(t1.Clock())
dayOffset := 0
if r.endDay == nil {
if r.startTime.d >= r.endTime.d && t1Time.d >= r.startTime.d {
dayOffset = 1
}
} else {
switch {
case *r.endDay < t1.Weekday():
dayOffset = 7 + int(*(r.endDay)-t1.Weekday())
case t1.Weekday() == *r.endDay:
if r.endTime.d <= t1Time.d {
dayOffset = 7
}
default:
dayOffset = int(*(r.endDay) - t1.Weekday())
}
}
sessionEnd := time.Date(t1.Year(), t1.Month(), t1.Day(), r.endTime.hour, r.endTime.minute, r.endTime.second, 0, r.loc)
sessionEnd = sessionEnd.AddDate(0, 0, dayOffset)
return t2.Before(sessionEnd)
}