@@ -24,7 +24,7 @@ import (
2424type TimeRangeRequest struct {
2525 From * time.Time `json:"from" schema:"from"`
2626 To * time.Time `json:"to" schema:"to"`
27- TimeWindow * TimeWindows `json:"timeWindow" schema:"timeWindow" validate:"omitempty,oneof=today yesterday week month quarter lastWeek lastMonth"`
27+ TimeWindow * TimeWindows `json:"timeWindow" schema:"timeWindow" validate:"omitempty,oneof=today yesterday week month quarter lastWeek lastMonth lastQuarter "`
2828}
2929
3030func NewTimeRangeRequest (from * time.Time , to * time.Time ) * TimeRangeRequest {
@@ -49,13 +49,14 @@ func (timeRange TimeWindows) String() string {
4949
5050// Define constants for different time windows
5151const (
52- Today TimeWindows = "today"
53- Yesterday TimeWindows = "yesterday"
54- Week TimeWindows = "week"
55- Month TimeWindows = "month"
56- Quarter TimeWindows = "quarter"
57- LastWeek TimeWindows = "lastWeek"
58- LastMonth TimeWindows = "lastMonth"
52+ Today TimeWindows = "today"
53+ Yesterday TimeWindows = "yesterday"
54+ Week TimeWindows = "week"
55+ Month TimeWindows = "month"
56+ Quarter TimeWindows = "quarter"
57+ LastWeek TimeWindows = "lastWeek"
58+ LastMonth TimeWindows = "lastMonth"
59+ LastQuarter TimeWindows = "lastQuarter"
5960)
6061
6162func (timeRange * TimeRangeRequest ) ParseAndValidateTimeRange () (* TimeRangeRequest , error ) {
@@ -103,6 +104,37 @@ func (timeRange *TimeRangeRequest) ParseAndValidateTimeRange() (*TimeRangeReques
103104 lastMonthStart := thisMonthStart .AddDate (0 , - 1 , 0 )
104105 lastMonthEnd := thisMonthStart .Add (- time .Second )
105106 return NewTimeRangeRequest (& lastMonthStart , & lastMonthEnd ), nil
107+ case LastQuarter :
108+ // Calculate current quarter
109+ currentQuarter := ((int (now .Month ()) - 1 ) / 3 ) + 1
110+
111+ // Calculate previous quarter
112+ var prevQuarter int
113+ var prevYear int
114+ if currentQuarter == 1 {
115+ // If current quarter is Q1, previous quarter is Q4 of previous year
116+ prevQuarter = 4
117+ prevYear = now .Year () - 1
118+ } else {
119+ // Otherwise, previous quarter is in the same year
120+ prevQuarter = currentQuarter - 1
121+ prevYear = now .Year ()
122+ }
123+
124+ // Calculate start and end of previous quarter
125+ prevQuarterStartMonth := time .Month ((prevQuarter - 1 )* 3 + 1 )
126+ prevQuarterStart := time .Date (prevYear , prevQuarterStartMonth , 1 , 0 , 0 , 0 , 0 , now .Location ())
127+
128+ // End of previous quarter is the start of current quarter minus 1 second
129+ currentQuarterStartMonth := time .Month ((currentQuarter - 1 )* 3 + 1 )
130+ currentQuarterStart := time .Date (now .Year (), currentQuarterStartMonth , 1 , 0 , 0 , 0 , 0 , now .Location ())
131+ if currentQuarter == 1 {
132+ // If current quarter is Q1, we need to calculate Q4 end of previous year
133+ currentQuarterStart = time .Date (now .Year (), time .January , 1 , 0 , 0 , 0 , 0 , now .Location ())
134+ }
135+ prevQuarterEnd := currentQuarterStart .Add (- time .Second )
136+
137+ return NewTimeRangeRequest (& prevQuarterStart , & prevQuarterEnd ), nil
106138 default :
107139 return NewTimeRangeRequest (& time.Time {}, & time.Time {}), fmt .Errorf ("unsupported time window: %q" , * timeRange .TimeWindow )
108140 }
0 commit comments