✨ 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 == "" { | 	if date == "" { | ||||||
| 		return ErrorMissingDate | 		return ErrorMissingDate | ||||||
| 	} | 	} | ||||||
| 	d, e := time.Parse(time.RFC3339, date) | 	d, e := tools.ParseDate(date) | ||||||
| 	if e != nil { | 	if e != nil { | ||||||
| 		d, e = time.Parse(DateFormat, date) | 		return newParsingError(e) | ||||||
| 		if e != nil { |  | ||||||
| 			return newParsingError(e) |  | ||||||
| 		} |  | ||||||
| 	} | 	} | ||||||
| 	m.Date = d | 	m.Date = d | ||||||
| 
 | 
 | ||||||
|  | @ -270,7 +267,7 @@ func (e *Entry) UnmarshalJSON(in []byte) error { | ||||||
| 	if (!ok || dates == "") && !e.skipMissing { | 	if (!ok || dates == "") && !e.skipMissing { | ||||||
| 		return ErrorMissingDate | 		return ErrorMissingDate | ||||||
| 	} | 	} | ||||||
| 	date, err := time.Parse(time.RFC3339, dates) | 	date, err := tools.ParseDate(dates) | ||||||
| 	if err != nil && !e.skipMissing { | 	if err != nil && !e.skipMissing { | ||||||
| 		return newParsingError(err) | 		return newParsingError(err) | ||||||
| 	} | 	} | ||||||
|  | @ -280,7 +277,7 @@ func (e *Entry) UnmarshalJSON(in []byte) error { | ||||||
| 		if m.Key == "title" || m.Key == "date" { | 		if m.Key == "title" || m.Key == "date" { | ||||||
| 			continue | 			continue | ||||||
| 		} else if vs, ok := m.Value.(string); ok { | 		} 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 | 				m.Value = vd | ||||||
| 			} else { | 			} else { | ||||||
| 				m.Value = vs | 				m.Value = vs | ||||||
|  |  | ||||||
|  | @ -280,7 +280,7 @@ func TestEntryJsonUnmarshal(t *testing.T) { | ||||||
| 		}, | 		}, | ||||||
| 		{ | 		{ | ||||||
| 			"date-field", | 			"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", | 			"A Title", | ||||||
| 			when, | 			when, | ||||||
| 			[]Meta{{"posted", when.Add(-time.Hour)}}, | 			[]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)) | 		assert.Len(t, e.Fields, len(fields)) | ||||||
| 		for _, f := range fields { | 		for _, f := range fields { | ||||||
| 			got := false | 			got := false | ||||||
|  | 			fTime, isTime := f.Value.(time.Time) | ||||||
| 			for _, m := range e.Fields { | 			for _, m := range e.Fields { | ||||||
| 				var mVal any = m.Value | 				var mVal any = m.Value | ||||||
| 				var fVal any = f.Value | 				var fVal any = f.Value | ||||||
|  | @ -337,6 +338,13 @@ func getEntryJsonUnmarshalTestRunner(in, title string, date time.Time, fields [] | ||||||
| 					got = true | 					got = true | ||||||
| 					break | 					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) | 			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