diff --git a/common/map.go b/common/map.go index 9b81377..ee21637 100644 --- a/common/map.go +++ b/common/map.go @@ -5,6 +5,7 @@ import ( "errors" "reflect" "sort" + "time" ) //hotime的常用map @@ -100,6 +101,14 @@ func (that Map) GetBool(key string, err ...*Error) bool { return v } + +func (that Map) GetTime(key string, err ...*Error) time.Time { + + v := ObjToTime((that)[key], err...) + return v + +} + func (that Map) RangeSort(callback func(k string, v interface{}) (isEnd bool)) { testQu := []string{} //testQuData:= qu[0].(Map) diff --git a/common/obj.go b/common/obj.go index 51025b4..c4dc941 100644 --- a/common/obj.go +++ b/common/obj.go @@ -1,5 +1,7 @@ package common +import "time" + //对象封装方便取用 type Obj struct { Data interface{} @@ -18,6 +20,13 @@ func (that *Obj) ToInt(err ...Error) int { return ObjToInt(that.Data, &that.Error) } +func (that *Obj) ToTime(err ...Error) time.Time { + if len(err) != 0 { + that.Error = err[0] + } + return ObjToTime(that.Data, &that.Error) +} + func (that *Obj) ToInt64(err ...Error) int64 { if len(err) != 0 { that.Error = err[0] diff --git a/common/objtoobj.go b/common/objtoobj.go index 87546a8..5fb9b54 100644 --- a/common/objtoobj.go +++ b/common/objtoobj.go @@ -5,6 +5,7 @@ import ( "errors" "math" "strconv" + "time" ) //仅限于hotime.Slice @@ -96,6 +97,65 @@ func ObjToSlice(obj interface{}, e ...*Error) Slice { return v } +func ObjToTime(obj interface{}, e ...*Error) time.Time { + + tInt := ObjToInt64(obj) + //字符串类型,只支持标准mysql datetime格式 + if tInt == 0 { + tStr := ObjToStr(obj) + + if len(tStr) > 18 { + t, e := time.Parse("2006-01-02 15:04:05", tStr) + if e == nil { + return t + } + } else if len(tStr) > 15 { + t, e := time.Parse("2006-01-02 15:04", tStr) + if e == nil { + return t + } + } else if len(tStr) > 12 { + t, e := time.Parse("2006-01-02 15", tStr) + if e == nil { + return t + } + } else if len(tStr) > 9 { + t, e := time.Parse("2006-01-02", tStr) + if e == nil { + return t + } + } else if len(tStr) > 6 { + t, e := time.Parse("2006-01", tStr) + if e == nil { + return t + } + } + + } + + //纳秒级别 + if len(ObjToStr(tInt)) > 16 { + + return time.Time{}.Add(time.Nanosecond * time.Duration(tInt)) + //微秒级别 + } else if len(ObjToStr(tInt)) > 13 { + return time.Time{}.Add(time.Microsecond * time.Duration(tInt)) + //毫秒级别 + } else if len(ObjToStr(tInt)) > 10 { + return time.Time{}.Add(time.Millisecond * time.Duration(tInt)) + //秒级别 + } else if len(ObjToStr(tInt)) > 9 { + return time.Time{}.Add(time.Second * time.Duration(tInt)) + } else if len(ObjToStr(tInt)) > 3 { + t, e := time.Parse("2006", ObjToStr(tInt)) + if e == nil { + return t + } + } + + return time.Time{} +} + func ObjToFloat64(obj interface{}, e ...*Error) float64 { var err error v := float64(0) diff --git a/common/slice.go b/common/slice.go index 05b1f95..dede608 100644 --- a/common/slice.go +++ b/common/slice.go @@ -2,6 +2,7 @@ package common import ( "errors" + "time" ) type Slice []interface{} @@ -14,6 +15,13 @@ func (that Slice) GetString(key int, err ...*Error) string { return ObjToStr((that)[key]) } +func (that Slice) GetTime(key int, err ...*Error) time.Time { + + v := ObjToTime((that)[key], err...) + return v + +} + // GetInt 获取Int func (that Slice) GetInt(key int, err ...*Error) int { v := ObjToInt((that)[key], err...) diff --git a/example/main.go b/example/main.go index 60700ce..10d7568 100644 --- a/example/main.go +++ b/example/main.go @@ -16,6 +16,32 @@ import ( func main() { date, _ := time.Parse("2006-01-02 15:04", time.Now().Format("2006-01-02")+" 14:00") fmt.Println(date, date.Unix()) + //fmt.Println("开始测试") + //t:=common.Map{"t":1652425167} + //fmt.Println(t.GetTime("t")) + //t["t"]=1652425167123 + //fmt.Println(t.GetTime("t")) + //t["t"]=1652425167123456 + //fmt.Println(t.GetTime("t")) + //t["t"]=1652425167123456789 + //fmt.Println(t.GetTime("t")) + //fmt.Println("开始测试2") + //t["t"]="2006-01-02 15:04:05" + //fmt.Println(t.GetTime("t")) + //t["t"]="2006-01-02 15" + //fmt.Println(t.GetTime("t")) + //t["t"]="2006-01-02" + //fmt.Println(t.GetTime("t")) + //t["t"]="2006-01" + //fmt.Println(t.GetTime("t")) + //t["t"]="2006-01" + //fmt.Println(t.GetTime("t")) + //t["t"]="2006" + //fmt.Println(t.GetTime("t")) + // + //t["t"]="06" + //fmt.Println(t.GetTime("t")) + //fmt.Println("0123456"[1:7]) appIns := Init("config/config.json")