Browse Source

changelog and PR changes

pull/1842/head
rigel rozanski 8 years ago
parent
commit
cc364b14e2
3 changed files with 34 additions and 57 deletions
  1. +7
    -0
      CHANGELOG.md
  2. +14
    -14
      common/date.go
  3. +13
    -43
      common/date_test.go

+ 7
- 0
CHANGELOG.md View File

@ -1,5 +1,12 @@
# Changelog
## Develop-Branch changes (unreleased)
FEATURES:
- [common] Date range parsing from string (ex. "2015-12-31:2017-12-31")
## 0.2.1 (June 2, 2017)
FEATURES:


+ 14
- 14
common/date.go View File

@ -7,37 +7,37 @@ import (
"github.com/pkg/errors"
)
// ParseDate parses a date string of the format YYYY-MM-DD
func ParseDate(date string) (t time.Time, err error) {
layout := "2006-01-02" //this represents YYYY-MM-DD
return time.Parse(layout, date)
}
// 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 nil pointers for
// 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) {
func ParseDateRange(dateRange string) (startDate, endDate time.Time, err error) {
dates := strings.Split(dateRange, ":")
if len(dates) != 2 {
return nil, nil, errors.New("bad date range, must be in format date:date")
err = errors.New("bad date range, must be in format date:date")
return
}
parseDate := func(date string) (*time.Time, error) {
parseDate := func(date string) (out time.Time, err error) {
if len(date) == 0 {
return nil, nil
return
}
d, err := ParseDate(date)
return &d, err
out, err = time.Parse(TimeLayout, date)
return
}
startDate, err = parseDate(dates[0])
if err != nil {
return nil, nil, err
return
}
endDate, err = parseDate(dates[1])
if err != nil {
return nil, nil, err
return
}
return
}

+ 13
- 43
common/date_test.go View File

@ -10,66 +10,36 @@ import (
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 TestParseDate(t *testing.T) {
assert := assert.New(t)
var testDates = []struct {
dateStr string
date time.Time
errNil bool
}{
{"2015-12-31", date, true},
{"2015-31-12", date, false},
{"12-31-2015", date, false},
{"31-12-2015", date, false},
}
for _, test := range testDates {
parsed, err := ParseDate(test.dateStr)
switch test.errNil {
case true:
assert.Nil(err)
assert.True(parsed.Equal(test.date), "parsed: %v, want %v", parsed, test.date)
case false:
assert.NotNil(err, "parsed %v, expected err %v", parsed, err)
}
}
}
func TestParseDateRange(t *testing.T) {
assert := assert.New(t)
var testDates = []struct {
dateStr string
start *time.Time
end *time.Time
start time.Time
end time.Time
errNil bool
}{
{"2015-12-31:2016-12-31", &date, &date2, true},
{"2015-12-31:", &date, nil, true},
{":2016-12-31", nil, &date2, true},
{"2016-12-31", nil, nil, false},
{"2016-31-12:", nil, nil, false},
{":2016-31-12", nil, nil, false},
{"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)
switch test.errNil {
case true:
if test.errNil {
assert.Nil(err)
testPtr := func(want, have *time.Time) {
if want == nil {
assert.Nil(have)
} else {
assert.True((*have).Equal(*want))
}
testPtr := func(want, have time.Time) {
assert.True(have.Equal(want))
}
testPtr(test.start, start)
testPtr(test.end, end)
case false:
} else {
assert.NotNil(err)
}
}


Loading…
Cancel
Save