🐛 Only truncate minutes and seconds
Also ensure we're getting date.Time for specific times
This commit is contained in:
		
					parent
					
						
							
								70b82761c2
							
						
					
				
			
			
				commit
				
					
						44e79916d3
					
				
			
		
					 2 changed files with 33 additions and 14 deletions
				
			
		|  | @ -7,10 +7,6 @@ import ( | |||
| 	"github.com/markusmobius/go-dateparser/date" | ||||
| ) | ||||
| 
 | ||||
| const ( | ||||
| 	day = time.Hour * 24 | ||||
| ) | ||||
| 
 | ||||
| // These are somewhat arbitrary, but reasonably useful min and max times | ||||
| var ( | ||||
| 	MinTime = time.Unix(-2208988800, 0) // Jan 1, 1900 | ||||
|  | @ -25,21 +21,30 @@ func ParseDate(in string) (t time.Time, err error) { | |||
| 		return MaxTime, nil | ||||
| 	} | ||||
| 
 | ||||
| 	d, err := dp.Parse(nil, in) | ||||
| 	d, err := dp.Parse(&dp.Configuration{ | ||||
| 		CurrentTime:        time.Now().Local(), | ||||
| 		ReturnTimeAsPeriod: true, | ||||
| 	}, in) | ||||
| 	t = d.Time | ||||
| 	if err != nil { | ||||
| 		return | ||||
| 	} | ||||
| 	t = d.Time.Local() | ||||
| 	trunc := time.Second | ||||
| 
 | ||||
| 	y, mon, day, h, loc := t.Year(), t.Month(), t.Day(), t.Hour(), t.Location() | ||||
| 	switch d.Period { | ||||
| 	case date.Second: | ||||
| 		t = t.Truncate(time.Second) | ||||
| 	case date.Minute: | ||||
| 		trunc = time.Minute | ||||
| 		t = t.Truncate(time.Minute) | ||||
| 	case date.Hour: | ||||
| 		trunc = time.Hour | ||||
| 		t = time.Date(y, mon, day, h, 0, 0, 0, loc) | ||||
| 	case date.Day: | ||||
| 		trunc = day | ||||
| 		// @todo Handle other cases separately | ||||
| 		t = time.Date(y, mon, day, 0, 0, 0, 0, loc) | ||||
| 	case date.Month: | ||||
| 		t = time.Date(y, mon, 1, 0, 0, 0, 0, loc) | ||||
| 	case date.Year: | ||||
| 		t = time.Date(y, 1, 1, 0, 0, 0, 0, loc) | ||||
| 	} | ||||
| 	t = t.Truncate(trunc) | ||||
| 
 | ||||
| 	return | ||||
| } | ||||
|  |  | |||
|  | @ -9,14 +9,24 @@ import ( | |||
| 	"github.com/stretchr/testify/require" | ||||
| ) | ||||
| 
 | ||||
| const day = time.Hour * 24 | ||||
| 
 | ||||
| func TestParseDate(t *testing.T) { | ||||
| 	now := time.Now().Local() | ||||
| 	y, mon, d, h, loc := now.Year(), now.Month(), now.Day(), now.Hour(), now.Location() | ||||
| 	sec := now.Truncate(time.Second) | ||||
| 	today := now.Truncate(day) | ||||
| 	today := time.Date(y, mon, d, 0, 0, 0, 0, loc) | ||||
| 	tomorrow := today.Add(day) | ||||
| 	yesterday := today.Add(-day) | ||||
| 	twoMin := now.Add(2 * time.Minute).Truncate(time.Minute) | ||||
| 	twoHour := now.Add(2 * time.Hour).Truncate(time.Hour) | ||||
| 	twoHour := time.Date(y, mon, d, h+2, 0, 0, 0, loc) | ||||
| 	firstMonth := time.Date(y, mon, 1, 0, 0, 0, 0, loc) | ||||
| 	firstYear := time.Date(y, 1, 1, 0, 0, 0, 0, loc) | ||||
| 	exact := "2075-02-12T12:13:54.536-02:00" | ||||
| 	exactd, _ := time.ParseInLocation(time.RFC3339, exact, time.FixedZone("UTC-02:00", -2*60*60)) | ||||
| 	var ts int64 = 1708876012 | ||||
| 	tsd := time.Unix(ts, 0) | ||||
| 
 | ||||
| 	tests := []struct { | ||||
| 		name string | ||||
| 		exp  time.Time | ||||
|  | @ -28,8 +38,12 @@ func TestParseDate(t *testing.T) { | |||
| 		{"yesterday", yesterday, ""}, | ||||
| 		{"in two minutes", twoMin, ""}, | ||||
| 		{"in two hours", twoHour, ""}, | ||||
| 		{"this month", firstMonth, ""}, | ||||
| 		{"this year", firstYear, ""}, | ||||
| 		{"min", MinTime, ""}, | ||||
| 		{"max", MaxTime, ""}, | ||||
| 		{exact, exactd, ""}, | ||||
| 		{fmt.Sprint(ts), tsd, ""}, | ||||
| 		{"not a date", now, fmt.Sprintf(`failed to parse "%s": unknown format`, "not a date")}, | ||||
| 	} | ||||
| 	for _, tt := range tests { | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue