-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathparse_time.go
90 lines (80 loc) · 2.72 KB
/
parse_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
87
88
89
90
package utils
import (
"fmt"
"reflect"
"time"
timefmt "github.com/itchyny/timefmt-go"
)
const (
Iso8601 string = "iso8601"
Rfc2822 string = "rfc2822" // timezone name
Rfc2822z string = "rfc2822z" // explicit timezone
Rfc3339 string = "rfc3339"
TimestampSecs string = "unix_timestamp_secs"
TimestampMillis string = "unix_timestamp_millis"
TimestampMicros string = "unix_timestamp_micros"
TimestampNanos string = "unix_timestamp_nanos"
)
const Rfc2822Layout string = "%a, %d %b %Y %T %Z"
const Rfc2822zLayout string = "%a, %d %b %Y %T %z"
// Parses a value into Time given a timeOutputFormat. The conversion
// only works with float64 as this is what we get when parsing a response.
func ParseTime(value any, timeOutputFormat string) (time.Time, error) {
switch value.(type) {
case string:
value_string := value.(string)
switch timeOutputFormat {
case Iso8601, Rfc3339:
timeValue, err := time.Parse(time.RFC3339, value_string)
if err != nil {
return time.Time{}, err
}
return timeValue, nil
case Rfc2822:
// XXX: the time package's layout for RFC2822 is bogus, don't use that.
timeValue, err := timefmt.Parse(value_string, Rfc2822Layout)
if err != nil {
return time.Time{}, err
}
return timeValue, nil
case Rfc2822z:
// XXX: the time package's layout for RFC2822 is bogus, don't use that.
timeValue, err := timefmt.Parse(value_string, Rfc2822zLayout)
if err != nil {
return time.Time{}, err
}
return timeValue, nil
case TimestampSecs, TimestampMillis, TimestampMicros, TimestampNanos:
return time.Time{}, fmt.Errorf("ParseTime received incoherent inputs: timeOutputFormat: %s value: %s (%s)", timeOutputFormat, fmt.Sprint(value), reflect.TypeOf(value))
default:
timeValue, err := timefmt.Parse(value_string, timeOutputFormat)
if err != nil {
return time.Time{}, err
}
return timeValue, nil
}
default:
var value_i64 int64
switch value.(type) {
case int, int8, int16, int32, int64:
value_i64 = reflect.ValueOf(value).Int()
case float32, float64:
value_f64 := reflect.ValueOf(value).Float()
value_i64 = int64(value_f64)
default:
return time.Time{}, fmt.Errorf("ParseTime does not support values of type (%s)", reflect.TypeOf(value))
}
switch timeOutputFormat {
case TimestampSecs:
return time.Unix(value_i64, 0), nil
case TimestampMillis:
return time.Unix(0, value_i64*1_000_000), nil
case TimestampMicros:
return time.Unix(0, value_i64*1_000), nil
case TimestampNanos:
return time.Unix(0, value_i64), nil
default:
return time.Time{}, fmt.Errorf("ParseTime received incoherent inputs: timeOutputFormat: %s value: %s (%s)", timeOutputFormat, fmt.Sprint(value), reflect.TypeOf(value))
}
}
}