@ -0,0 +1,43 @@ | |||||
package common | |||||
import ( | |||||
"strings" | |||||
"time" | |||||
"github.com/pkg/errors" | |||||
) | |||||
// TimeLayout helps to parse a date string of the format YYYY-MM-DD | |||||
// Intended to be used with the following function: | |||||
// time.Parse(TimeLayout, date) | |||||
var TimeLayout = "2006-01-02" //this represents YYYY-MM-DD | |||||
// ParseDateRange parses a date range string of the format start:end | |||||
// where the start and end date are of the format YYYY-MM-DD. | |||||
// The parsed dates are time.Time and will return the zero time for | |||||
// unbounded dates, ex: | |||||
// unbounded start: :2000-12-31 | |||||
// unbounded end: 2000-12-31: | |||||
func ParseDateRange(dateRange string) (startDate, endDate time.Time, err error) { | |||||
dates := strings.Split(dateRange, ":") | |||||
if len(dates) != 2 { | |||||
err = errors.New("bad date range, must be in format date:date") | |||||
return | |||||
} | |||||
parseDate := func(date string) (out time.Time, err error) { | |||||
if len(date) == 0 { | |||||
return | |||||
} | |||||
out, err = time.Parse(TimeLayout, date) | |||||
return | |||||
} | |||||
startDate, err = parseDate(dates[0]) | |||||
if err != nil { | |||||
return | |||||
} | |||||
endDate, err = parseDate(dates[1]) | |||||
if err != nil { | |||||
return | |||||
} | |||||
return | |||||
} |
@ -0,0 +1,46 @@ | |||||
package common | |||||
import ( | |||||
"testing" | |||||
"time" | |||||
"github.com/stretchr/testify/assert" | |||||
) | |||||
var ( | |||||
date = time.Date(2015, time.Month(12), 31, 0, 0, 0, 0, time.UTC) | |||||
date2 = time.Date(2016, time.Month(12), 31, 0, 0, 0, 0, time.UTC) | |||||
zero time.Time | |||||
) | |||||
func TestParseDateRange(t *testing.T) { | |||||
assert := assert.New(t) | |||||
var testDates = []struct { | |||||
dateStr string | |||||
start time.Time | |||||
end time.Time | |||||
errNil bool | |||||
}{ | |||||
{"2015-12-31:2016-12-31", date, date2, true}, | |||||
{"2015-12-31:", date, zero, true}, | |||||
{":2016-12-31", zero, date2, true}, | |||||
{"2016-12-31", zero, zero, false}, | |||||
{"2016-31-12:", zero, zero, false}, | |||||
{":2016-31-12", zero, zero, false}, | |||||
} | |||||
for _, test := range testDates { | |||||
start, end, err := ParseDateRange(test.dateStr) | |||||
if test.errNil { | |||||
assert.Nil(err) | |||||
testPtr := func(want, have time.Time) { | |||||
assert.True(have.Equal(want)) | |||||
} | |||||
testPtr(test.start, start) | |||||
testPtr(test.end, end) | |||||
} else { | |||||
assert.NotNil(err) | |||||
} | |||||
} | |||||
} |