Merge branch 'zct-v2-dengluyang' of https://code.hoteas.com/golang/hotime into zhoupengwei

This commit is contained in:
zhoupengwei 2022-05-13 15:34:53 +08:00
commit 6b57201e59
5 changed files with 112 additions and 0 deletions

View File

@ -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)

View File

@ -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]

View File

@ -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)

View File

@ -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...)

View File

@ -17,6 +17,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")