summaryrefslogtreecommitdiff
path: root/internal/reports/daterange.go
blob: 347861583e8885f1992d599773d09b440b5ba114 (plain)
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
package reports

import (
	"fmt"
	"strings"
	"time"
)

type DateRange struct {
	Start time.Time
	End   time.Time
}

func ParseDateRange(dateStr string) (DateRange, error) {
	dateStr = strings.TrimSpace(dateStr)
	now := time.Now().UTC()

	// Check for predefined ranges (case-insensitive)
	lowerDateStr := strings.ToLower(dateStr)
	switch lowerDateStr {
	case "last week":
		return getLastWeek(now), nil
	case "last month":
		return getLastMonth(now), nil
	}

	// Check for custom date range format: "YYYY-MM-DD to YYYY-MM-DD"
	if strings.Contains(dateStr, " to ") {
		return parseCustomDateRange(dateStr)
	}

	return DateRange{}, fmt.Errorf("unsupported date range: %s (supported: 'last week', 'last month', or 'YYYY-MM-DD to YYYY-MM-DD')", dateStr)
}

func parseCustomDateRange(dateStr string) (DateRange, error) {
	parts := strings.Split(dateStr, " to ")
	if len(parts) != 2 {
		return DateRange{}, fmt.Errorf("invalid date range format: expected 'YYYY-MM-DD to YYYY-MM-DD'")
	}

	startStr := strings.TrimSpace(parts[0])
	endStr := strings.TrimSpace(parts[1])

	// Parse start date
	startDate, err := time.Parse("2006-01-02", startStr)
	if err != nil {
		return DateRange{}, fmt.Errorf("invalid start date '%s': expected YYYY-MM-DD format", startStr)
	}

	// Parse end date
	endDate, err := time.Parse("2006-01-02", endStr)
	if err != nil {
		return DateRange{}, fmt.Errorf("invalid end date '%s': expected YYYY-MM-DD format", endStr)
	}

	// Validate that start date is before or equal to end date
	if startDate.After(endDate) {
		return DateRange{}, fmt.Errorf("start date '%s' must be before or equal to end date '%s'", startStr, endStr)
	}

	// Convert to UTC and set times appropriately
	// Start date: beginning of day (00:00:00)
	startUTC := time.Date(startDate.Year(), startDate.Month(), startDate.Day(), 0, 0, 0, 0, time.UTC)
	
	// End date: end of day (23:59:59.999999999)
	endUTC := time.Date(endDate.Year(), endDate.Month(), endDate.Day(), 23, 59, 59, 999999999, time.UTC)

	return DateRange{
		Start: startUTC,
		End:   endUTC,
	}, nil
}

func getLastWeek(now time.Time) DateRange {
	// Find the start of current week (Monday)
	weekday := int(now.Weekday())
	if weekday == 0 { // Sunday
		weekday = 7
	}
	
	// Start of current week
	currentWeekStart := now.AddDate(0, 0, -(weekday-1)).Truncate(24 * time.Hour)
	
	// Last week is the week before current week
	lastWeekStart := currentWeekStart.AddDate(0, 0, -7)
	lastWeekEnd := currentWeekStart.Add(-time.Nanosecond)
	
	return DateRange{
		Start: lastWeekStart,
		End:   lastWeekEnd,
	}
}

func getLastMonth(now time.Time) DateRange {
	// Start of current month
	currentMonthStart := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.UTC)
	
	// Last month start
	lastMonthStart := currentMonthStart.AddDate(0, -1, 0)
	
	// Last month end (last nanosecond of last month)
	lastMonthEnd := currentMonthStart.Add(-time.Nanosecond)
	
	return DateRange{
		Start: lastMonthStart,
		End:   lastMonthEnd,
	}
}