Skip to content

Commit a0292a5

Browse files
committed
bug fix
1 parent 94eedd4 commit a0292a5

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

copier.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func copySlice(src, dst reflect.Value) error {
223223
}
224224

225225
func setTimeField(src, dst reflect.Value) (bool, error) {
226-
const format = "2006-01-02T15:04:05Z07:00"
226+
const format = time.RFC3339Nano
227227

228228
switch t := src.Interface().(type) {
229229
case time.Time:
@@ -302,10 +302,11 @@ func setTimeField(src, dst reflect.Value) (bool, error) {
302302

303303
case string:
304304
// string -> time.Time or *time.Time
305-
v, err := time.Parse(t, format)
305+
v, err := time.Parse(format, t)
306306
if err != nil {
307307
return true, nil
308308
}
309+
309310
switch dst.Interface().(type) {
310311
case time.Time:
311312
dst.Set(reflect.ValueOf(v))
@@ -322,7 +323,7 @@ func setTimeField(src, dst reflect.Value) (bool, error) {
322323
return true, nil
323324
}
324325
// *string -> time.Time or *time.Time
325-
v, err := time.Parse(*t, format)
326+
v, err := time.Parse(format, *t)
326327
if err != nil {
327328
return true, nil
328329
}

copier_test.go

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,20 @@ func TestDeepCopy_time(t *testing.T) {
231231
ReplacedAt *int64
232232
}
233233

234+
type ModelE struct {
235+
CreatedAt string
236+
UpdatedAt *string
237+
DeletedAt string
238+
ReplacedAt *string
239+
}
240+
234241
type args struct {
235242
src interface{}
236243
dest interface{}
237244
}
238245

239-
//dt := time.Now()
240-
//now := time.Unix(dt.Unix(), 0)
241246
now := time.Now()
247+
const format = time.RFC3339Nano
242248

243249
tests := []struct {
244250
name string
@@ -299,6 +305,44 @@ func TestDeepCopy_time(t *testing.T) {
299305
},
300306
err: nil,
301307
},
308+
{
309+
name: "time.Time to string",
310+
in: args{
311+
src: ModelC{
312+
CreatedAt: now,
313+
UpdatedAt: now,
314+
DeletedAt: &now,
315+
ReplacedAt: &now,
316+
},
317+
dest: &ModelE{},
318+
},
319+
want: &ModelE{
320+
CreatedAt: now.Format(format),
321+
UpdatedAt: xgo.ToPtr(now.Format(format)),
322+
DeletedAt: now.Format(format),
323+
ReplacedAt: xgo.ToPtr(now.Format(format)),
324+
},
325+
err: nil,
326+
},
327+
{
328+
name: "string to time.Time",
329+
in: args{
330+
src: ModelE{
331+
CreatedAt: now.Format(format),
332+
UpdatedAt: xgo.ToPtr(now.Format(format)),
333+
DeletedAt: now.Format(format),
334+
ReplacedAt: xgo.ToPtr(now.Format(format)),
335+
},
336+
dest: &ModelC{},
337+
},
338+
want: &ModelC{
339+
CreatedAt: now,
340+
UpdatedAt: now,
341+
DeletedAt: &now,
342+
ReplacedAt: &now,
343+
},
344+
err: nil,
345+
},
302346
}
303347

304348
for _, tt := range tests {

0 commit comments

Comments
 (0)