File tree 2 files changed +29
-11
lines changed
2 files changed +29
-11
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package entity
3
3
import (
4
4
"encoding/json"
5
5
"errors"
6
+ "fmt"
6
7
"net"
7
8
"strconv"
8
9
"time"
@@ -134,14 +135,22 @@ func (t *MAASTime) UnmarshalJSON(data []byte) error {
134
135
return err
135
136
}
136
137
137
- temp , err := time .Parse ("2006-01-02T15:04:05.999" , str )
138
- if err != nil {
139
- return err
138
+ formats := []string {
139
+ "2006-01-02T15:04:05.999+00:00" ,
140
+ "2006-01-02T15:04:05.999Z" ,
141
+ "2006-01-02T15:04:05.999" ,
140
142
}
141
143
142
- * t = MAASTime (temp )
144
+ for _ , format := range formats {
145
+ foundTime , err := time .Parse (format , str )
146
+ if err == nil {
147
+ * t = MAASTime (foundTime )
143
148
144
- return nil
149
+ return nil
150
+ }
151
+ }
152
+
153
+ return fmt .Errorf ("unsupported time format for %q" , str )
145
154
}
146
155
147
156
// String returns MAASTime in RFC3339Nano format
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package entity
2
2
3
3
import (
4
4
"encoding/json"
5
+ "fmt"
5
6
"testing"
6
7
7
8
"github.com/canonical/gomaasclient/test/helper"
@@ -93,14 +94,22 @@ func TestMAASTimeUnmarshalJSON(t *testing.T) {
93
94
Time MAASTime
94
95
}{}
95
96
96
- data := []byte (`{"Time": "2023-05-11T21:15:04.208"}` )
97
- if err := json .Unmarshal (data , & temp ); err != nil {
98
- t .Fatal (err )
97
+ formats := []string {
98
+ "2023-05-11T21:15:04.208+00:00" ,
99
+ "2023-05-11T21:15:04.208Z" ,
100
+ "2023-05-11T21:15:04.208" ,
99
101
}
100
102
101
- expected := "2023-05-11T21:15:04.208Z"
102
- if temp .Time .String () != expected {
103
- t .Fatalf ("expected %v, got %v" , expected , temp .Time .String ())
103
+ for _ , format := range formats {
104
+ data := []byte (fmt .Sprintf (`{"Time": "%s"}` , format ))
105
+ if err := json .Unmarshal (data , & temp ); err != nil {
106
+ t .Fatal (err )
107
+ }
108
+
109
+ expected := "2023-05-11T21:15:04.208Z"
110
+ if temp .Time .String () != expected {
111
+ t .Fatalf ("expected %v, got %v" , expected , temp .Time .String ())
112
+ }
104
113
}
105
114
}
106
115
You can’t perform that action at this time.
0 commit comments