package timeutil

import "time"

func FirstSecondInPeriod(since time.Time, period string) (_ time.Time) {
	y, m, d := since.Date()

	switch period {
	case "h":
		h := since.Hour()
		return time.Date(y, m, d, h, 0, 0, 0, time.Local)
	case "d":
		return time.Date(y, m, d, 0, 0, 0, 0, time.Local)
	case "m":
		return time.Date(y, m, 1, 0, 0, 0, 0, time.Local)
	default:
		return since
	}
}

func LastSecondInPeriod(until time.Time, period string) (_ time.Time) {
	y, m, d := until.Date()

	switch period {
	case "h":
		h := until.Hour()
		return time.Date(y, m, d, h, 59, 59, 0, time.Local)
	case "d":
		return time.Date(y, m, d, 23, 59, 59, 0, time.Local)
	case "m":
		tm := time.Date(y, m, 1, 23, 59, 59, 0, time.Local)
		// Додаю місяць
		tm = tm.AddDate(0, 1, 0)
		// Віднімаю день
		return tm.AddDate(0, 0, -1)
	default:
		return until
	}
}
