package utils

import "time"

func GetMonthlyPeriod(t time.Time) (start, end time.Time) {
	year, month, _ := t.Date()
	start = time.Date(year, month, 1, 0, 0, 0, 0, time.UTC)
	end = start.AddDate(0, 1, 0).Add(-time.Second)
	return
}

func GetQuarterlyPeriod(t time.Time) (start, end time.Time) {
	year, month, _ := t.Date()

	quarterMonth := ((int(month)-1)/3)*3 + 1
	start = time.Date(year, time.Month(quarterMonth), 1, 0, 0, 0, 0, time.UTC)
	end = start.AddDate(0, 3, 0).Add(-time.Second)
	return
}

func GetYearlyPeriod(t time.Time) (start, end time.Time) {
	year, _, _ := t.Date()
	start = time.Date(year, 1, 1, 0, 0, 0, 0, time.UTC)
	end = start.AddDate(1, 0, 0).Add(-time.Second)
	return
}

func ParsePeriodString(period string) (start, end time.Time, err error) {
	t, err := time.Parse("2006-01", period)
	if err != nil {
		return
	}
	start, end = GetMonthlyPeriod(t)
	return
}
