✨ Use ParseDate in Entry
This commit is contained in:
		
					parent
					
						
							
								96c3b2ff30
							
						
					
				
			
			
				commit
				
					
						c1b1ceb283
					
				
			
		
					 2 changed files with 13 additions and 8 deletions
				
			
		|  | @ -109,12 +109,9 @@ func (m *Entry) UnmarshalText(in []byte) error { | |||
| 	if date == "" { | ||||
| 		return ErrorMissingDate | ||||
| 	} | ||||
| 	d, e := time.Parse(time.RFC3339, date) | ||||
| 	d, e := tools.ParseDate(date) | ||||
| 	if e != nil { | ||||
| 		d, e = time.Parse(DateFormat, date) | ||||
| 		if e != nil { | ||||
| 			return newParsingError(e) | ||||
| 		} | ||||
| 		return newParsingError(e) | ||||
| 	} | ||||
| 	m.Date = d | ||||
| 
 | ||||
|  | @ -270,7 +267,7 @@ func (e *Entry) UnmarshalJSON(in []byte) error { | |||
| 	if (!ok || dates == "") && !e.skipMissing { | ||||
| 		return ErrorMissingDate | ||||
| 	} | ||||
| 	date, err := time.Parse(time.RFC3339, dates) | ||||
| 	date, err := tools.ParseDate(dates) | ||||
| 	if err != nil && !e.skipMissing { | ||||
| 		return newParsingError(err) | ||||
| 	} | ||||
|  | @ -280,7 +277,7 @@ func (e *Entry) UnmarshalJSON(in []byte) error { | |||
| 		if m.Key == "title" || m.Key == "date" { | ||||
| 			continue | ||||
| 		} else if vs, ok := m.Value.(string); ok { | ||||
| 			if vd, err := time.Parse(time.RFC3339, vs); err == nil { | ||||
| 			if vd, err := tools.ParseDate(vs); err == nil { | ||||
| 				m.Value = vd | ||||
| 			} else { | ||||
| 				m.Value = vs | ||||
|  |  | |||
|  | @ -280,7 +280,7 @@ func TestEntryJsonUnmarshal(t *testing.T) { | |||
| 		}, | ||||
| 		{ | ||||
| 			"date-field", | ||||
| 			`{"title":"A Title","date":"` + whens + `","posted":"` + when.Add(-time.Hour).Format(time.RFC3339) + `"}`, | ||||
| 			`{"title":"A Title","date":"` + whens + `","posted":"` + when.Add(-time.Hour).In(time.UTC).Format(time.RFC3339) + `"}`, | ||||
| 			"A Title", | ||||
| 			when, | ||||
| 			[]Meta{{"posted", when.Add(-time.Hour)}}, | ||||
|  | @ -324,6 +324,7 @@ func getEntryJsonUnmarshalTestRunner(in, title string, date time.Time, fields [] | |||
| 		assert.Len(t, e.Fields, len(fields)) | ||||
| 		for _, f := range fields { | ||||
| 			got := false | ||||
| 			fTime, isTime := f.Value.(time.Time) | ||||
| 			for _, m := range e.Fields { | ||||
| 				var mVal any = m.Value | ||||
| 				var fVal any = f.Value | ||||
|  | @ -337,6 +338,13 @@ func getEntryJsonUnmarshalTestRunner(in, title string, date time.Time, fields [] | |||
| 					got = true | ||||
| 					break | ||||
| 				} | ||||
| 				if isTime && m.Key == f.Key { | ||||
| 					mTime, _ := mVal.(time.Time) | ||||
| 					if assert.WithinRange(t, mTime, fTime.Add(-2*time.Second), fTime.Add(2*time.Second)) { | ||||
| 						got = true | ||||
| 						break | ||||
| 					} | ||||
| 				} | ||||
| 			} | ||||
| 			assert.Truef(t, got, "Couldn't find field %+v. We have %+v", f, e.Fields) | ||||
| 		} | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue