package util

import "time"

var TimeLayout = "2006-01-02 15:04:05" //转化所需模板

func DateTime2TimeStr(datatime string) int64 {
	loc, _ := time.LoadLocation("Local")                          //重要：获取时区
	theTime, _ := time.ParseInLocation(TimeLayout, datatime, loc) //使用模板在对应时区转化为time.time类型
	return theTime.Unix()
}

func TimeStr2DateTime(timestr int64, format string) string {
	return time.Unix(timestr, 0).Format(format)
}

// 获取相差时间(小时)
func GetHourDiffer(start_time, end_time string) int64 {
	var hour int64
	t1, err := time.ParseInLocation(TimeLayout, start_time, time.Local)
	t2, err := time.ParseInLocation(TimeLayout, end_time, time.Local)
	if err == nil && t1.Before(t2) {
		diff := t2.Unix() - t1.Unix()
		hour = diff / 3600
		return hour
	} else {
		return hour
	}
}

// 获取相差时间(秒)
func GetSecondDiffer(start_time, end_time string) int64 {
	var scond int64
	t1, err := time.ParseInLocation(TimeLayout, start_time, time.Local)
	t2, err := time.ParseInLocation(TimeLayout, end_time, time.Local)
	if err == nil && t1.Before(t2) {
		diff := t2.Unix() - t1.Unix()
		return diff
	} else {
		return scond
	}
}
