package xtime import ( "fmt" "strings" "time" ) const DefaultTimeLayout = "2006-01-02 15:04:05" func LeaTime(t int64) string { leaSeconds := time.Now().Unix() - t mps := []struct { Seconds int64 Title string }{ {31536000, "years"}, {604800, "weeks"}, {86400, "days"}, {3600, "hours"}, {60, "min"}, {1, "sec"}, } res := []string{} for _, v := range mps { if leaSeconds > v.Seconds { if len(res) == 2 { break } n := leaSeconds / v.Seconds tt := v.Title if n == 1 { tt = strings.TrimRight(v.Title, "s") } res = append(res, fmt.Sprintf("%d %s", n, tt)) leaSeconds -= v.Seconds * n } } return strings.Join(res, " ") } func UnixToTime(unix int64) time.Time { return time.Unix(unix, 0) } func ParseTime(day string, layout string) int64 { if day == "" { return 0 } t, err := time.ParseInLocation(layout, day, time.Local) if err != nil { fmt.Println(err) return 0 } return t.Unix() } const DefaultLayout = "2006-01-02 15:04:05" func TimeTodayMinAndMax(ctime time.Time) (min, max time.Time) { start, _ := time.ParseInLocation(DefaultLayout, ctime.Format("2006-01-02")+" 00:00:00", time.Local) end, _ := time.ParseInLocation(DefaultLayout, ctime.Format("2006-01-02")+" 23:59:59", time.Local) return start, end } func UnixToStr(t int64) string { if t == 0 { return "" } return UnixToTime(t).Format(DefaultTimeLayout) }