package utils

import (
	"strconv"
	"time"
)

func GetTodayTime(format string) time.Time {
	todaystr := time.Now().Format(format)
	//local := time.Local
	local, _ := time.LoadLocation("Asia/Chongqing")
	todayTime, _ := time.ParseInLocation(format, todaystr, local)
	return todayTime
}

func GetDate(t int64) time.Time {
	datestr := time.Unix(t, 0).Format("2006-01-02")
	local, _ := time.LoadLocation("Asia/Chongqing")
	date, _ := time.ParseInLocation("2006-01-02", datestr, local)
	return date
}

func GetDateByStrAndFormat(datestr string, format string) time.Time {
	local, _ := time.LoadLocation("Asia/Chongqing")
	date, _ := time.ParseInLocation(format, datestr, local)
	return date
}

//GetMonthStartAndEnd 获取月份的第一天和最后一天
func GetMonthStartAndEnd(myYear string, myMonth string) map[string]time.Time {
	// 数字月份必须前置补零
	if len(myMonth) == 1 {
		myMonth = "0" + myMonth
	}
	yInt, _ := strconv.Atoi(myYear)

	timeLayout := "2006-01-02 15:04:05"
	loc, _ := time.LoadLocation("Local")
	theTime, _ := time.ParseInLocation(timeLayout, myYear+"-"+myMonth+"-01 00:00:00", loc)
	newMonth := theTime.Month()

	t1 := time.Date(yInt, newMonth, 1, 0, 0, 0, 0, time.Local)
	t2 := time.Date(yInt, newMonth+1, 0, 0, 0, 0, 0, time.Local)
	result := map[string]time.Time{"start": t1, "end": t2}
	return result
}
