package time_utils

import "time"

func GetFirstDateOfWeek(now time.Time) time.Time {
	offset := int(time.Monday - now.Weekday())
	if offset > 0 {
		offset = -6
	}

	weekStartDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
	return weekStartDate
}

func GetLastWeekFirstDate(now time.Time) time.Time {
	thisWeekMonday := GetFirstDateOfWeek(now)
	lastWeekMonday := thisWeekMonday.AddDate(0, 0, -7)
	return lastWeekMonday
}

func GetLastDateOfWeek(now time.Time) time.Time {
	thisWeekMonday := GetFirstDateOfWeek(now)
	lastWeekMonday := thisWeekMonday.AddDate(0, 0, 6)
	return lastWeekMonday
}

//获取传入的时间所在月份的第一天，即某月第一天的0点。如传入time.Now(), 返回当前月份的第一天0点时间。
func GetFirstDateOfMonth(d time.Time) time.Time {
	d = d.AddDate(0, 0, -d.Day()+1)
	return d
}

//获取传入的时间所在月份的最后一天，即某月最后一天的0点。如传入time.Now(), 返回当前月份的最后一天0点时间。
func GetLastDateOfMonth(d time.Time) time.Time {
	return GetFirstDateOfMonth(d).AddDate(0, 1, -1)
}

func GetZeroTime(d time.Time) time.Time {
	return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location())
}

func GetFormatTime(d time.Time) string {
	return d.Format("2006-01-02")
}

func GetFormatTimeFull(d time.Time) string {
	return d.Format("2006-01-02 15:04:05")
}

func TimeNowInCN() time.Time {
	var cstZone = time.FixedZone("CST", 8*3600)
	return time.Now().In(cstZone)
}
func GetCNZone() *time.Location {
	return time.FixedZone("UTC-8", 8*3600)
}
