You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

113 lines
2.6 KiB

package xtime
import (
"fmt"
"strconv"
"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)
}
func GetToday(tim time.Time) int64 {
today, _ := strconv.ParseInt(tim.Format("20060102"), 10, 64)
return today
}
func Unix() int64 {
return time.Now().Unix()
}
func UnixTime(unix int64) time.Time {
return time.Unix(unix, 0)
}
func DayStartToTime(day int64) time.Time {
start, _ := time.ParseInLocation("20060102 15:04:05", fmt.Sprintf("%d 00:00:00", day), time.Local)
return start
}
func DayEndToTime(day int64) time.Time {
start, _ := time.ParseInLocation("20060102 15:04:05", fmt.Sprintf("%d 23:59:59", day), time.Local)
return start
}
func MinAndMax(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 GetMonday(tt time.Time) time.Time {
return tt.AddDate(0, 0, -(int(tt.Weekday()) - 1))
}
func GetSunDay(tt time.Time) time.Time {
return tt.AddDate(0, 0, 7-(int(tt.Weekday())))
}
func Rang(start time.Time, end time.Time, fun func(ctime time.Time)) {
ctime := start
for ctime.Before(end) {
fun(ctime)
ctime = ctime.AddDate(0, 0, 1)
}
}
func EsTime(t time.Time) string {
return t.Format(time.RFC3339)
}