@@ -6,20 +6,12 @@ import (
66 "time"
77
88 "github.com/slok/sloth/internal/http/backend/model"
9+ utilstime "github.com/slok/sloth/pkg/common/utils/time"
910)
1011
1112func calculateStepsForTimeRange (from , to time.Time ) time.Duration {
1213 const autoSteps = 50
13- totalDuration := to .Sub (from )
14- step := totalDuration / time .Duration (autoSteps )
15-
16- // Round step to minutes.
17- if step < time .Minute {
18- step = time .Minute
19- }
20- step = (time .Duration (int (step .Minutes ())) * time .Minute )
21-
22- return step
14+ return utilstime .CalculateStepsForTimeRange (from , to , autoSteps )
2315}
2416
2517func sanitizeDataPoints (dps []model.DataPoint , from , to time.Time , step time.Duration ) []model.DataPoint {
@@ -47,44 +39,16 @@ func sanitizeDataPoints(dps []model.DataPoint, from, to time.Time, step time.Dur
4739 return sanitizedDPs
4840}
4941
50- func roundTimeToDay (t time.Time ) time.Time {
51- return time .Date (t .Year (), t .Month (), t .Day (), 0 , 0 , 0 , 0 , t .Location ())
52- }
53-
54- func weekMonday (t time.Time ) time.Time {
55- diff := time .Duration (t .Weekday () - 1 )
56- if diff < 0 {
57- diff = 6
58- }
59-
60- return roundTimeToDay (t ).Add (- 1 * diff * 24 * time .Hour ) // Remove the diff days until monday.
61- }
62-
63- func monthFirst (t time.Time ) time.Time {
64- return time .Date (t .Year (), t .Month (), 1 , 0 , 0 , 0 , 0 , t .Location ())
65- }
66-
67- func quarterFirst (t time.Time ) time.Time {
68- // Gets the first day of the quarter the time is in.
69- month := ((t .Month ()- 1 )/ 3 )* 3 + 1
70- return time .Date (t .Year (), month , 1 , 0 , 0 , 0 , 0 , t .Location ())
71- }
72-
73- func yearFirst (t time.Time ) time.Time {
74- // Gets the first day of the year the time is in.
75- return time .Date (t .Year (), 1 , 1 , 0 , 0 , 0 , 0 , t .Location ())
76- }
77-
7842func startOfPeriod (t time.Time , periodType BudgetRangeType ) (time.Time , error ) {
7943 switch periodType {
8044 case BudgetRangeTypeYearly :
81- return yearFirst (t ), nil
45+ return utilstime . YearFirst (t ), nil
8246 case BudgetRangeTypeQuarterly :
83- return quarterFirst (t ), nil
47+ return utilstime . QuarterFirst (t ), nil
8448 case BudgetRangeTypeMonthly :
85- return monthFirst (t ), nil
49+ return utilstime . MonthFirst (t ), nil
8650 case BudgetRangeTypeWeekly :
87- return weekMonday (t ), nil
51+ return utilstime . WeekMonday (t ), nil
8852 }
8953
9054 return time.Time {}, fmt .Errorf ("unknown budget range type: %q" , periodType )
@@ -93,15 +57,13 @@ func startOfPeriod(t time.Time, periodType BudgetRangeType) (time.Time, error) {
9357func endOfPeriod (t time.Time , periodType BudgetRangeType ) (time.Time , error ) {
9458 switch periodType {
9559 case BudgetRangeTypeYearly :
96- return yearFirst ( t ). Add ( 365 * 24 * time . Hour - 1 ), nil
60+ return utilstime . EndOfYear ( t ), nil
9761 case BudgetRangeTypeQuarterly :
98- // TODO: This is a simplification, not all months have 30 days.
99- return quarterFirst (t ).Add (3 * 30 * 24 * time .Hour - 1 ), nil
62+ return utilstime .EndOfQuarter (t ), nil
10063 case BudgetRangeTypeMonthly :
101- // TODO: This is a simplification, not all months have 30 days.
102- return monthFirst (t ).Add (30 * 24 * time .Hour - 1 ), nil
64+ return utilstime .EndOfMonth (t ), nil
10365 case BudgetRangeTypeWeekly :
104- return weekMonday ( t ). Add ( 7 * 24 * time . Hour - 1 ), nil
66+ return utilstime . EndOfWeek ( t ), nil
10567 }
10668
10769 return time.Time {}, fmt .Errorf ("unknown budget range type: %q" , periodType )
0 commit comments