/
time.go
86 lines (76 loc) · 1.69 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package azuretpl
import (
"fmt"
"strconv"
"time"
)
// fromUnixtime converts unixtime to time.Time object
func fromUnixtime(val interface{}) (time.Time, error) {
var timestamp int64
switch v := val.(type) {
// int
case *int:
if v == nil {
return time.Unix(0, 0), fmt.Errorf(`cannot convert nil to time`)
}
timestamp = int64(*v)
case int:
timestamp = int64(v)
// int64
case *int64:
if v == nil {
return time.Unix(0, 0), fmt.Errorf(`cannot convert nil to time`)
}
timestamp = *v
case int64:
timestamp = int64(v)
// float32
case *float32:
if v == nil {
return time.Unix(0, 0), fmt.Errorf(`cannot convert nil to time`)
}
timestamp = int64(*v)
case float32:
timestamp = int64(v)
// float64
case *float64:
if v == nil {
return time.Unix(0, 0), fmt.Errorf(`cannot convert nil to time`)
}
timestamp = int64(*v)
case float64:
timestamp = int64(v)
// string
case *string:
if v == nil {
return time.Unix(0, 0), fmt.Errorf(`cannot convert nil to time`)
}
var err error
timestamp, err = strconv.ParseInt(*v, 10, 64)
if err != nil {
return time.Unix(0, 0), err
}
case string:
var err error
timestamp, err = strconv.ParseInt(v, 10, 64)
if err != nil {
return time.Unix(0, 0), err
}
// time.Time
case *time.Time:
if v != nil {
return *v, nil
}
return time.Unix(0, 0), fmt.Errorf(`cannot convert nil to time`)
case time.Time:
return v, nil
// default
default:
return time.Unix(0, 0), fmt.Errorf(`invalid unixtimestamp "%v" defined, must be int or float`, val)
}
return time.Unix(timestamp, 0), nil
}
// toRFC3339 converts time.Time object to RFC 3339 string
func toRFC3339(t time.Time) string {
return t.Format(time.RFC3339)
}