京东JavaDate类型,改为毫秒的timestamp类型兼容处理。

This commit is contained in:
gazebo
2019-11-15 15:18:29 +08:00
parent 74d40eb1fd
commit 57d00d1bc7
12 changed files with 161 additions and 32 deletions

View File

@@ -45,3 +45,7 @@ func (j *JavaDate) GoTime() time.Time {
}
return Timestamp2Time(j.Time / 1000)
}
func (j *JavaDate) String() string {
return Timestamp2Str(j.Time)
}

View File

@@ -17,6 +17,8 @@ import (
"github.com/gazeboxu/structs"
)
const MaxTimeSecond = 9573800254 // 正常最大的秒数
var (
DefaultTimeValue = Str2Time("1970-01-01 00:00:00")
ZeroTimeValue = time.Time{}
@@ -277,13 +279,14 @@ func Timestamp2Str(timestamp int64) string {
return Time2Str(Timestamp2Time(timestamp))
}
// timestamp为秒或毫秒
func Timestamp2Time(timestamp int64) time.Time {
const normalTimestamp = 1533709322
if timestamp > normalTimestamp*100 { // 传成毫秒了
baseapi.SugarLogger.Errorf("Timestamp2Time wrong timestamp:%d", timestamp)
timestamp = timestamp / 1000
nsec := int64(0)
if timestamp > MaxTimeSecond { // 传成毫秒了
nsec = (timestamp % 1000) * 1000000
timestamp /= 1000
}
return time.Unix(timestamp, 0)
return time.Unix(timestamp, nsec)
}
func Time2Str(t time.Time) string {
@@ -472,20 +475,24 @@ func Struct2FlatMap(obj interface{}) map[string]interface{} {
}
// !!! 此函数好像不支持struct是内嵌结构的
func Map2Struct(inObj interface{}, outObjAddr interface{}, weaklyTypedInput bool, tagName string) (err error) {
func Map2Struct(inObj interface{}, outObjAddr interface{}, weaklyTypedInput bool, tagName string, decodeHook interface{}) (err error) {
if tagName == "" {
tagName = "json"
}
if !weaklyTypedInput {
weaklyTypedInput = decodeHook != nil
}
decoder, _ := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
TagName: tagName,
Result: outObjAddr,
WeaklyTypedInput: weaklyTypedInput,
DecodeHook: decodeHook,
})
return decoder.Decode(inObj)
}
func Map2StructByJson(inObj interface{}, outObjAddr interface{}, weaklyTypedInput bool) (err error) {
return Map2Struct(inObj, outObjAddr, weaklyTypedInput, "")
return Map2Struct(inObj, outObjAddr, weaklyTypedInput, "", nil)
}
func Int64Slice2String(intList []int64) (outList []string) {