gitee.com/go-spring2/spring-base@v1.1.3/cast/time.go (about) 1 /* 2 * Copyright 2012-2019 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * https://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package cast 18 19 import ( 20 "fmt" 21 "time" 22 ) 23 24 var unitMap = map[string]int64{ 25 "ns": int64(time.Nanosecond), 26 "μs": int64(time.Microsecond), 27 "ms": int64(time.Millisecond), 28 "s": int64(time.Second), 29 "m": int64(time.Minute), 30 "h": int64(time.Hour), 31 } 32 33 // ToTime casts an interface{} to a time.Time. 34 // When type is clear, it is recommended to use standard library functions. 35 func ToTime(i interface{}, format ...string) time.Time { 36 v, _ := ToTimeE(i, format...) 37 return v 38 } 39 40 // ToTimeE casts an interface{} to a time.Time. 41 // When type is clear, it is recommended to use standard library functions. 42 func ToTimeE(i interface{}, format ...string) (time.Time, error) { 43 switch v := i.(type) { 44 case nil: 45 return time.Time{}, nil 46 case int: 47 return parseIntTimestamp(int64(v), format...), nil 48 case int8: 49 return parseIntTimestamp(int64(v), format...), nil 50 case int16: 51 return parseIntTimestamp(int64(v), format...), nil 52 case int32: 53 return parseIntTimestamp(int64(v), format...), nil 54 case int64: 55 return parseIntTimestamp(v, format...), nil 56 case *int: 57 return parseIntTimestamp(int64(*v), format...), nil 58 case *int8: 59 return parseIntTimestamp(int64(*v), format...), nil 60 case *int16: 61 return parseIntTimestamp(int64(*v), format...), nil 62 case *int32: 63 return parseIntTimestamp(int64(*v), format...), nil 64 case *int64: 65 return parseIntTimestamp(*v, format...), nil 66 case uint: 67 return parseIntTimestamp(int64(v), format...), nil 68 case uint8: 69 return parseIntTimestamp(int64(v), format...), nil 70 case uint16: 71 return parseIntTimestamp(int64(v), format...), nil 72 case uint32: 73 return parseIntTimestamp(int64(v), format...), nil 74 case uint64: 75 return parseIntTimestamp(int64(v), format...), nil 76 case *uint: 77 return parseIntTimestamp(int64(*v), format...), nil 78 case *uint8: 79 return parseIntTimestamp(int64(*v), format...), nil 80 case *uint16: 81 return parseIntTimestamp(int64(*v), format...), nil 82 case *uint32: 83 return parseIntTimestamp(int64(*v), format...), nil 84 case *uint64: 85 return parseIntTimestamp(int64(*v), format...), nil 86 case float32: 87 return parseFloatTimestamp(float64(v), format...), nil 88 case float64: 89 return parseFloatTimestamp(v, format...), nil 90 case *float32: 91 return parseFloatTimestamp(float64(*v), format...), nil 92 case *float64: 93 return parseFloatTimestamp(*v, format...), nil 94 case string: 95 return parseFormatTime(v, format...) 96 case *string: 97 return parseFormatTime(*v, format...) 98 case time.Time: 99 return v, nil 100 case *time.Time: 101 return *v, nil 102 default: 103 return time.Time{}, fmt.Errorf("unable to cast type (%T) to Time", i) 104 } 105 } 106 107 func parseFormatTime(v string, format ...string) (time.Time, error) { 108 if d, err := time.ParseDuration(v); err == nil { 109 return time.Unix(int64(d/time.Second), int64(d%time.Second)), nil 110 } 111 layout := "2006-01-02 15:04:05 -0700" 112 if len(format) > 0 { 113 layout = format[0] 114 } 115 return time.Parse(layout, v) 116 } 117 118 func parseIntTimestamp(v int64, format ...string) time.Time { 119 unitN := int64(time.Nanosecond) 120 if len(format) > 0 { 121 unitN, _ = unitMap[format[0]] 122 } 123 v = v * unitN 124 return time.Unix(v/int64(time.Second), v%int64(time.Second)) 125 } 126 127 func parseFloatTimestamp(v float64, format ...string) time.Time { 128 unitN := int64(time.Nanosecond) 129 if len(format) > 0 { 130 unitN, _ = unitMap[format[0]] 131 } 132 i := int64(v * float64(unitN)) 133 return time.Unix(i/int64(time.Second), i%int64(time.Second)) 134 }