1
1
package utils
2
2
3
3
import (
4
- "errors"
5
4
"fmt"
6
5
"reflect"
7
6
"time"
@@ -26,33 +25,44 @@ const Rfc2822zLayout string = "%a, %d %b %Y %T %z"
26
25
// Parses a value into Time given a timeOutputFormat. The conversion
27
26
// only works with float64 as this is what we get when parsing a response.
28
27
func ParseTime (value any , timeOutputFormat string ) (time.Time , error ) {
29
- switch timeOutputFormat {
30
- case Iso8601 , Rfc3339 :
28
+ switch value .( type ) {
29
+ case string :
31
30
value_string := value .(string )
32
- timeValue , err := time .Parse (time .RFC3339 , value_string )
33
- if err != nil {
34
- return time.Time {}, err
35
- }
36
- return timeValue , nil
31
+ switch timeOutputFormat {
32
+ case Iso8601 , Rfc3339 :
33
+ timeValue , err := time .Parse (time .RFC3339 , value_string )
34
+ if err != nil {
35
+ return time.Time {}, err
36
+ }
37
+ return timeValue , nil
37
38
38
- case Rfc2822 :
39
- // XXX: the time package's layout for RFC2822 is bogus, don't use that.
40
- value_string := value .(string )
41
- timeValue , err := timefmt .Parse (value_string , Rfc2822Layout )
42
- if err != nil {
43
- return time.Time {}, err
44
- }
45
- return timeValue , nil
46
- case Rfc2822z :
47
- // XXX: the time package's layout for RFC2822 is bogus, don't use that.
48
- value_string := value .(string )
49
- timeValue , err := timefmt .Parse (value_string , Rfc2822zLayout )
50
- if err != nil {
51
- return time.Time {}, err
52
- }
53
- return timeValue , nil
39
+ case Rfc2822 :
40
+ // XXX: the time package's layout for RFC2822 is bogus, don't use that.
41
+ timeValue , err := timefmt .Parse (value_string , Rfc2822Layout )
42
+ if err != nil {
43
+ return time.Time {}, err
44
+ }
45
+ return timeValue , nil
46
+
47
+ case Rfc2822z :
48
+ // XXX: the time package's layout for RFC2822 is bogus, don't use that.
49
+ timeValue , err := timefmt .Parse (value_string , Rfc2822zLayout )
50
+ if err != nil {
51
+ return time.Time {}, err
52
+ }
53
+ return timeValue , nil
54
+
55
+ case TimestampSecs , TimestampMillis , TimestampMicros , TimestampNanos :
56
+ return time.Time {}, fmt .Errorf ("ParseTime received incoherent inputs: timeOutputFormat: %s value: %s (%s)" , timeOutputFormat , fmt .Sprint (value ), reflect .TypeOf (value ))
54
57
55
- case TimestampSecs , TimestampMillis , TimestampMicros , TimestampNanos :
58
+ default :
59
+ timeValue , err := timefmt .Parse (value_string , timeOutputFormat )
60
+ if err != nil {
61
+ return time.Time {}, err
62
+ }
63
+ return timeValue , nil
64
+ }
65
+ default :
56
66
var value_i64 int64
57
67
switch value .(type ) {
58
68
case int , int8 , int16 , int32 , int64 :
@@ -61,25 +71,20 @@ func ParseTime(value any, timeOutputFormat string) (time.Time, error) {
61
71
value_f64 := reflect .ValueOf (value ).Float ()
62
72
value_i64 = int64 (value_f64 )
63
73
default :
64
- return time.Time {}, errors . New ( "parseTime only accepts float64 or int64 values with timestamp based formats" )
74
+ return time.Time {}, fmt . Errorf ( "ParseTime does not support values of type (%s)" , reflect . TypeOf ( value ) )
65
75
}
66
76
67
- if timeOutputFormat == TimestampSecs {
77
+ switch timeOutputFormat {
78
+ case TimestampSecs :
68
79
return time .Unix (value_i64 , 0 ), nil
69
- } else if timeOutputFormat == TimestampMillis {
80
+ case TimestampMillis :
70
81
return time .Unix (0 , value_i64 * 1_000_000 ), nil
71
- } else if timeOutputFormat == TimestampMicros {
82
+ case TimestampMicros :
72
83
return time .Unix (0 , value_i64 * 1_000 ), nil
73
- } else if timeOutputFormat == TimestampNanos {
84
+ case TimestampNanos :
74
85
return time .Unix (0 , value_i64 ), nil
86
+ default :
87
+ return time.Time {}, fmt .Errorf ("ParseTime received incoherent inputs: timeOutputFormat: %s value: %s (%s)" , timeOutputFormat , fmt .Sprint (value ), reflect .TypeOf (value ))
75
88
}
76
- default :
77
- value_string := value .(string )
78
- timeValue , err := timefmt .Parse (value_string , timeOutputFormat )
79
- if err != nil {
80
- return time.Time {}, err
81
- }
82
- return timeValue , nil
83
89
}
84
- return time.Time {}, fmt .Errorf ("timeOutputFormat not supported yet %s" , timeOutputFormat )
85
90
}
0 commit comments