/
dapegen.go
197 lines (174 loc) · 4.43 KB
/
dapegen.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package dapegen
import (
"errors"
"github.com/RangelReale/epochdate"
"time"
)
// Grouping enum
type GROUP int
const (
DAY GROUP = iota
WEEK
MONTH
)
// Generator object
type Generator struct {
StartDate epochdate.Date
EndDate epochdate.Date
Group GROUP
FirstDate epochdate.Date
LastDate epochdate.Date
CurrentDate epochdate.Date
FirstDayOfWeek time.Weekday
currentEndDate epochdate.Date
isfirst bool
isforward bool
}
// Creates a forward generator
func NewGenerator(startdate epochdate.Date, enddate epochdate.Date, group GROUP) (*Generator, error) {
d := &Generator{
StartDate: startdate,
EndDate: enddate,
Group: group,
FirstDayOfWeek: time.Monday,
isfirst: true,
isforward: true,
}
if err := d.initialize(); err != nil {
return nil, err
}
return d, nil
}
// Creates a backwards generator
func NewGeneratorBackwards(startdate epochdate.Date, enddate epochdate.Date, group GROUP) (*Generator, error) {
d := &Generator{
StartDate: startdate,
EndDate: enddate,
Group: group,
FirstDayOfWeek: time.Monday,
isfirst: true,
isforward: false,
}
if err := d.initialize(); err != nil {
return nil, err
}
return d, nil
}
// checks the passed parameters
func (d *Generator) initialize() error {
if d.isforward {
if d.StartDate.After(d.EndDate) {
return errors.New("Start date must be before end date")
}
} else {
if d.StartDate.Before(d.EndDate) {
return errors.New("Start date must be after end date")
}
}
return nil
}
// returns the first day from the date, depending on the group
func (d Generator) FirstFromDate(date epochdate.Date) epochdate.Date {
switch d.Group {
case WEEK:
// first date is the previous monday of the week
wd := date.UTC().Weekday()
if wd > d.FirstDayOfWeek {
return date - epochdate.Date(wd) + epochdate.Date(d.FirstDayOfWeek)
} else if wd < d.FirstDayOfWeek {
return date - 6 + epochdate.Date(wd)
}
case MONTH:
// first day of the month
year, month, _ := date.Date()
ret, _ := epochdate.NewFromDate(year, month, 1)
return ret
}
return date
}
// Generates the next period, returns false if finished
func (d *Generator) Next() bool {
ret, _ := d.nextUntilInternal(nil, true)
return ret
}
// Generates the next period until the passed date, returns if have next
func (d *Generator) NextUntil(date epochdate.Date) bool {
r, _ := d.NextUntilOrFinished(date)
return r
}
// Generates the next period until the passed date, returns if have next or finished
func (d *Generator) NextUntilOrFinished(date epochdate.Date) (havenext bool, isfinished bool) {
f := d.FirstFromDate(date)
havenext, isfinished = d.nextUntilInternal(&f, false)
return
}
// Returns if is finished
func (d *Generator) IsFinished() bool {
return d.isFinishedInternal(d.currentEndDate)
}
// Initialize internal values on first pass
func (d *Generator) initializeGeneration() {
d.CurrentDate = d.FirstFromDate(d.StartDate)
d.currentEndDate = d.FirstFromDate(d.EndDate)
d.FirstDate = d.CurrentDate
d.LastDate = d.currentEndDate
}
// Calculate the next period until the passed date, or the end date if nil.
// includelast determines if the last item is included.
func (d *Generator) nextUntilInternal(date *epochdate.Date, includelast bool) (havenext bool, isfinished bool) {
if d.isfirst {
// initialize on first run
d.initializeGeneration()
}
if date == nil {
// use default end date
date = &d.currentEndDate
}
if d.isFinishedInternal(*date) {
d.isfirst = false
return false, true
}
if !d.isfirst {
switch d.Group {
case WEEK:
// advance 7 days
if d.isforward {
d.CurrentDate += 7
} else {
d.CurrentDate -= 7
}
case MONTH:
// advance day 1 of month
year, month, _ := d.CurrentDate.Date()
if d.isforward {
d.CurrentDate, _ = epochdate.NewFromDate(year, month+1, 1)
} else {
d.CurrentDate, _ = epochdate.NewFromDate(year, month-1, 1)
}
default:
// advance 1 day
if d.isforward {
d.CurrentDate++
} else {
d.CurrentDate--
}
}
} else {
d.isfirst = false
}
if !includelast && d.isFinishedInternal(*date) {
return false, false
}
return true, false
}
// checks if iteration finished on passed date
func (d *Generator) isFinishedInternal(date epochdate.Date) bool {
if d.CurrentDate == date {
return true
}
if d.isforward {
return !d.CurrentDate.Before(date)
} else {
return !d.CurrentDate.After(date)
}
}