/*
 * @Author: immortal
 * @Date: 2022-03-11 20:55:38
 * @LastEditors: immortal
 * @LastEditTime: 2022-03-12 14:26:42
 * @Description:
 * @FilePath: \monitor_env\utils\simpletime\timeTranslate.go
 */
/**
 * @Author Puzzle
 * @Date 2021/11/18 1:36 下午
 **/

package stime

import (
	"time"
)

func GetTimestampMillisecond() int64 {
	now := time.Now()
	return now.UnixNano() / 1e6
}

func StringToTime(strTime string) (*time.Time, error) {
	const TIME_LAYOUT = "2006-01-02 15:04:05" //此时间不可更改
	timeobj, err := time.ParseInLocation(TIME_LAYOUT, strTime, Loc.Shanghai())
	return &timeobj, err
}

func StringToTimeWithFormat(strTime string, timeFormat string) (*time.Time, error) {
	timeobj, err := time.ParseInLocation(timeFormat, strTime, Loc.Shanghai())
	return &timeobj, err
}

// 去除精确时间后面的小数点
func NowTimeToTime(layout string) *time.Time {
	otime := time.Now().Format(layout) //"2006-01-02 15:04:05" and so on
	tt, _ := StringToTime(otime)
	return tt
}

// 时间之间的转换
func TimeStampToString(timestamp int64, format string) string {
	t := time.Unix(timestamp, 0)
	return t.Format(format)
}

func GetAge(birthday time.Time) int {
	if birthday.IsZero() {
		return 0
	}
	now := time.Now()
	year, month, day := now.Date()
	if year == 0 || month == 0 || day == 0 {
		return 0
	}
	age := year - birthday.Year() - 1
	//判断年龄
	if birthday.Month() < month || birthday.Month() == month && birthday.Day() <= day {
		age++
	}
	return age
}