2022-09-06 09:09:34 +02:00
|
|
|
package stepper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-12-12 13:05:44 +01:00
|
|
|
"sort"
|
2022-09-06 09:09:34 +02:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// StepMonth represents a one month interval
|
|
|
|
StepMonth string = "month"
|
2023-07-31 16:55:59 +02:00
|
|
|
// StepWeek represents a one week interval
|
|
|
|
StepWeek string = "week"
|
2023-08-12 16:32:11 +02:00
|
|
|
// StepDay represents a one day interval
|
|
|
|
StepDay string = "day"
|
2022-09-06 09:09:34 +02:00
|
|
|
// StepHour represents a one hour interval
|
|
|
|
StepHour string = "hour"
|
2022-11-29 22:53:28 +01:00
|
|
|
// StepMinute represents a one minute interval
|
|
|
|
StepMinute string = "minute"
|
2022-09-06 09:09:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// SplitDateRange splits start-end range in a subset of ranges respecting the given step
|
|
|
|
// Ranges with granularity of StepMonth are aligned to 1st of each month in order to improve export efficiency at block transfer level
|
2023-12-12 13:05:44 +01:00
|
|
|
func SplitDateRange(start, end time.Time, step string, timeReverse bool) ([][]time.Time, error) {
|
2022-09-06 09:09:34 +02:00
|
|
|
|
|
|
|
if start.After(end) {
|
|
|
|
return nil, fmt.Errorf("start time %q should come before end time %q", start.Format(time.RFC3339), end.Format(time.RFC3339))
|
|
|
|
}
|
|
|
|
|
|
|
|
var nextStep func(time.Time) (time.Time, time.Time)
|
|
|
|
|
|
|
|
switch step {
|
|
|
|
case StepMonth:
|
|
|
|
nextStep = func(t time.Time) (time.Time, time.Time) {
|
|
|
|
endOfMonth := time.Date(t.Year(), t.Month()+1, 1, 0, 0, 0, 0, t.Location()).Add(-1 * time.Nanosecond)
|
|
|
|
if t == endOfMonth {
|
|
|
|
endOfMonth = time.Date(t.Year(), t.Month()+2, 1, 0, 0, 0, 0, t.Location()).Add(-1 * time.Nanosecond)
|
|
|
|
t = time.Date(t.Year(), t.Month()+1, 1, 0, 0, 0, 0, t.Location())
|
|
|
|
}
|
|
|
|
return t, endOfMonth
|
|
|
|
}
|
|
|
|
case StepDay:
|
|
|
|
nextStep = func(t time.Time) (time.Time, time.Time) {
|
|
|
|
return t, t.AddDate(0, 0, 1)
|
|
|
|
}
|
2023-07-31 16:55:59 +02:00
|
|
|
case StepWeek:
|
|
|
|
nextStep = func(t time.Time) (time.Time, time.Time) {
|
|
|
|
return t, t.Add(7 * 24 * time.Hour)
|
|
|
|
}
|
2022-09-06 09:09:34 +02:00
|
|
|
case StepHour:
|
|
|
|
nextStep = func(t time.Time) (time.Time, time.Time) {
|
|
|
|
return t, t.Add(time.Hour * 1)
|
|
|
|
}
|
2022-11-29 22:53:28 +01:00
|
|
|
case StepMinute:
|
|
|
|
nextStep = func(t time.Time) (time.Time, time.Time) {
|
|
|
|
return t, t.Add(time.Minute * 1)
|
|
|
|
}
|
2022-09-06 09:09:34 +02:00
|
|
|
default:
|
2022-11-29 22:53:28 +01:00
|
|
|
return nil, fmt.Errorf("failed to parse step value, valid values are: '%s', '%s', '%s', '%s'. provided: '%s'",
|
|
|
|
StepMonth, StepDay, StepHour, StepMinute, step)
|
2022-09-06 09:09:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
currentStep := start
|
|
|
|
|
|
|
|
ranges := make([][]time.Time, 0)
|
|
|
|
|
|
|
|
for end.After(currentStep) {
|
|
|
|
s, e := nextStep(currentStep)
|
|
|
|
if e.After(end) {
|
|
|
|
e = end
|
|
|
|
}
|
|
|
|
ranges = append(ranges, []time.Time{s, e})
|
|
|
|
currentStep = e
|
|
|
|
}
|
2023-12-12 13:05:44 +01:00
|
|
|
if timeReverse {
|
|
|
|
sort.SliceStable(ranges, func(i, j int) bool {
|
|
|
|
return ranges[i][0].After(ranges[j][0])
|
|
|
|
})
|
|
|
|
}
|
2022-09-06 09:09:34 +02:00
|
|
|
|
|
|
|
return ranges, nil
|
|
|
|
}
|