golang 记录time包的小方法

获取今天星期几

//获取今天星期几
func GetWeek() string {
	t := time.Now()
	week := t.Weekday().String()
	switch week {
	case "Monday":
		return "星期一"
	case "Tuesday":
		return "星期二"
	case "Wednesday":
		return "星期三"
	case "Thursday":
		return "星期四"
	case "Friday":
		return "星期五"
	case "Saturday":
		return "星期六"
	default:
		return "星期日"
	}
}

时间戳转换时间

//时间戳转换时间
//@param            num         时间戳
//@param            option         时间戳转换的格式
//@return            args        格式化时间
func GetStampToFormat(num int64,option string) string  {
	format := make(map[string]interface{})
	format["YmdHis"] = "2006-01-02 15:04:05"
	switch option {
	case "YmdHis":
		return time.Unix(num,0).Format("2006-01-02 15:04:05")
	case "YmdHi":
		return time.Unix(num,0).Format("2006-01-02 15:04")
	case "Ymd":
		return time.Unix(num,0).Format("2006-01-02")
	case "ANSIC":
		return time.Unix(num,0).Format("Mon Jan _2 15:04:05 2006")
	case "UnixDate":
		return time.Unix(num,0).Format("Mon Jan _2 15:04:05 MST 2006")
	case "RFC822Z":
		return time.Unix(num,0).Format("02 Jan 06 15:04 -0700")
	case "RFC850":
		return time.Unix(num,0).Format("Monday, 02-Jan-06 15:04:05 MST")
	case "RFC1123":
		return time.Unix(num,0).Format("Mon, 02 Jan 2006 15:04:05 MST")
	case "RFC1123Z":
		return time.Unix(num,0).Format("Mon, 02 Jan 2006 15:04:05 -0700")
	case "RFC3339":
		return time.Unix(num,0).Format("2006-01-02T15:04:05Z07:00")
	case "RFC3339Nano":
		return time.Unix(num,0).Format("2006-01-02T15:04:05.999999999Z07:00")
	case "Kitchen":
		return time.Unix(num,0).Format("3:04PM")
	case "Stamp":
		return time.Unix(num,0).Format("Jan _2 15:04:05")
	case "StampMilli":
		return time.Unix(num,0).Format("Jan _2 15:04:05.000")
	case "StampMicro":
		return time.Unix(num,0).Format("Jan _2 15:04:05.000000")
	case "StampNano":
		return time.Unix(num,0).Format("Jan _2 15:04:05.000000000")
	default:
		return time.Unix(num,0).Format("2006-01-02 15:04:05")
	}

}

获取指定时间的unix时间戳 最大支持到天

//获取19位unix时间戳
//@param            option         单位
//@param            unix         UnixNano时间戳
//@param            num         增加或减少的数量
//@return            args        19位时间戳
func GetTimeStamp(option string,unix int64,num int) int64  {
	Nanosecond   := 1 //毫微秒 十亿分之一秒
	Microsecond          := 1000 * Nanosecond //微秒 百万分之一秒
	Millisecond          := 1000 * Microsecond //毫秒
	Second               := 1000 * Millisecond
	Minute               := 60 * Second
	Hour                 := 60 * Minute
	Day                 := 60 * Hour
	switch option {
	case "h":
		return unix + int64(Hour * num)
	case "m":
		return unix + int64(Minute * num)
	case "s":
		return unix + int64(Second * num)
	case "hs":
		return unix + int64(Millisecond * num)
	case "ws":
		return unix + int64(Microsecond * num)
	case "hws":
		return unix + int64(Nanosecond * num)
	case "d":
		return unix + int64(Nanosecond * Day)
	default:
		return unix + int64(Second * num)
	}
}

获取10位的unix时间戳

//获取10位unix时间戳
//@param            option         单位
//@param            unix         UnixNano时间戳
//@param            num         增加或减少的数量
//@return            args        10位时间戳
func GettimeUnixStapm(option string,unix int64,num int)  int64{
	return GetTimeStamp(option,unix,num) / 1000000000
}

md5加密方法

//字符串md5加密
func StringMd5(s string) string  {
	data := []byte(s)
	has := md5.Sum(data)
	return fmt.Sprintf("%x", has) //将[]byte转成16进制
}

你可能感兴趣的:(goalng)