-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
39 lines (36 loc) · 881 Bytes
/
time.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package tox
import (
"reflect"
"time"
)
// ToTime converts data types to time.Time structures. 'int' or 'int64' are treated as unix time, strings are treated
// as RFC3330Nano timestamps. If the conversion fails, an empty time.Time{} is returned.
func ToTime(v interface{}) time.Time {
switch v := v.(type) {
case int:
return time.Unix(int64(v), 0)
case int64:
return time.Unix(v, 0)
case time.Time:
return v
case string:
t, err := time.Parse(time.RFC3339Nano, v)
if err != nil {
return time.Time{}
}
return t
default:
if reflect.TypeOf(v).Kind() == reflect.Int64 && reflect.TypeOf(v).Name() == "DateTime" {
// special case for mongodb primitive.DateTime
return time.Unix(reflect.ValueOf(v).Int()/1000, 0)
}
return time.Time{}
}
}
func ToTimePtr(v interface{}) *time.Time {
if v == nil {
return nil
}
ret := ToTime(v)
return &ret
}