✨ Use ParseDate in Parse
Also limit to English, or we get lots of false positives
This commit is contained in:
		
					parent
					
						
							
								391452e3d9
							
						
					
				
			
			
				commit
				
					
						96c3b2ff30
					
				
			
		
					 3 changed files with 7 additions and 4 deletions
				
			
		|  | @ -5,7 +5,6 @@ import ( | ||||||
| 	"regexp" | 	"regexp" | ||||||
| 	"strconv" | 	"strconv" | ||||||
| 	"strings" | 	"strings" | ||||||
| 	"time" |  | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func ParseBytes(in []byte) any { | func ParseBytes(in []byte) any { | ||||||
|  | @ -34,7 +33,7 @@ func ParseString(in string) any { | ||||||
| 		return i | 		return i | ||||||
| 	} else if f, err := strconv.ParseFloat(s, 64); err == nil { | 	} else if f, err := strconv.ParseFloat(s, 64); err == nil { | ||||||
| 		return f | 		return f | ||||||
| 	} else if t, err := time.Parse(time.RFC3339, s); err == nil { | 	} else if t, err := ParseDate(s); err == nil { | ||||||
| 		return t | 		return t | ||||||
| 	} else if err := json.Unmarshal([]byte(s), &j); err == nil { | 	} else if err := json.Unmarshal([]byte(s), &j); err == nil { | ||||||
| 		return j | 		return j | ||||||
|  |  | ||||||
|  | @ -26,6 +26,7 @@ func ParseDate(in string) (t time.Time, err error) { | ||||||
| 	conf := dp.Configuration{ | 	conf := dp.Configuration{ | ||||||
| 		CurrentTime:        time.Now().Local(), | 		CurrentTime:        time.Now().Local(), | ||||||
| 		ReturnTimeAsPeriod: true, | 		ReturnTimeAsPeriod: true, | ||||||
|  | 		Languages:          []string{"en"}, | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	d, err := dp.Parse(&conf, in) | 	d, err := dp.Parse(&conf, in) | ||||||
|  |  | ||||||
|  | @ -10,6 +10,7 @@ import ( | ||||||
| 
 | 
 | ||||||
| func TestParse(t *testing.T) { | func TestParse(t *testing.T) { | ||||||
| 	when := time.Now() | 	when := time.Now() | ||||||
|  | 	now := when.Local().Truncate(time.Second) | ||||||
| 	tests := []struct { | 	tests := []struct { | ||||||
| 		name string | 		name string | ||||||
| 		in   string | 		in   string | ||||||
|  | @ -22,6 +23,8 @@ func TestParse(t *testing.T) { | ||||||
| 		{"false", "false", false}, | 		{"false", "false", false}, | ||||||
| 		{"nil", "nil", nil}, | 		{"nil", "nil", nil}, | ||||||
| 		{"time", when.Format(time.RFC3339), when}, | 		{"time", when.Format(time.RFC3339), when}, | ||||||
|  | 		{"now", "now", now}, | ||||||
|  | 		{"DateFormat", now.Format(DateFormat), now}, | ||||||
| 		{"json-obj", `{"foo":"bar","baz":"quux"}`, json.RawMessage(`{"foo":"bar","baz":"quux"}`)}, | 		{"json-obj", `{"foo":"bar","baz":"quux"}`, json.RawMessage(`{"foo":"bar","baz":"quux"}`)}, | ||||||
| 		{"json-arr", `["foo",42,"bar", null,"quux", true]`, json.RawMessage(`["foo",42,"bar", null,"quux", true]`)}, | 		{"json-arr", `["foo",42,"bar", null,"quux", true]`, json.RawMessage(`["foo",42,"bar", null,"quux", true]`)}, | ||||||
| 		{"empty", "", ""}, | 		{"empty", "", ""}, | ||||||
|  | @ -48,7 +51,7 @@ func getParseTestRunner(in string, exp any) func(*testing.T) { | ||||||
| 		if expT, ok := exp.(time.Time); ok { | 		if expT, ok := exp.(time.Time); ok { | ||||||
| 			ti, gotTime := out.(time.Time) | 			ti, gotTime := out.(time.Time) | ||||||
| 			if assert.True(t, gotTime, "Should have gotten a time.Time, but didn't") { | 			if assert.True(t, gotTime, "Should have gotten a time.Time, but didn't") { | ||||||
| 				assert.WithinRange(t, expT, ti.Add(-time.Second), ti.Add(time.Second)) | 				assert.WithinRange(t, expT, ti.Add(-2*time.Second), ti.Add(2*time.Second)) | ||||||
| 			} | 			} | ||||||
| 		} else { | 		} else { | ||||||
| 			assert.Equal(t, exp, out) | 			assert.Equal(t, exp, out) | ||||||
|  | @ -57,7 +60,7 @@ func getParseTestRunner(in string, exp any) func(*testing.T) { | ||||||
| 		if expT, ok := exp.(time.Time); ok { | 		if expT, ok := exp.(time.Time); ok { | ||||||
| 			ti, gotTime := out.(time.Time) | 			ti, gotTime := out.(time.Time) | ||||||
| 			if assert.True(t, gotTime, "Should have gotten a time.Time, but didn't") { | 			if assert.True(t, gotTime, "Should have gotten a time.Time, but didn't") { | ||||||
| 				assert.WithinRange(t, expT, ti.Add(-time.Second), ti.Add(time.Second)) | 				assert.WithinRange(t, expT, ti.Add(-2*time.Second), ti.Add(2*time.Second)) | ||||||
| 			} | 			} | ||||||
| 		} else { | 		} else { | ||||||
| 			assert.Equal(t, exp, out) | 			assert.Equal(t, exp, out) | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue