Skip to content

Commit

Permalink
Added several new methods to VDate: IsYesterday, IsToday, IsTomorrow,…
Browse files Browse the repository at this point in the history
… Date
  • Loading branch information
Rick committed Aug 31, 2017
1 parent ca6968f commit 511bb2b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 17 deletions.
37 changes: 20 additions & 17 deletions view/vdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@ func NewVDate(d date.Date) VDate {
return VDate{d, DefaultFormat}
}

// Date returns the underlying date.
func (v VDate) Date() date.Date {
return v.d
}

// IsYesterday returns true if the date is yesterday's date.
func (v VDate) IsYesterday() bool {
return v.d.DaysSinceEpoch() + 1 == date.Today().DaysSinceEpoch()
}

// IsToday returns true if the date is today's date.
func (v VDate) IsToday() bool {
return v.d.DaysSinceEpoch() == date.Today().DaysSinceEpoch()
}

// IsTomorrow returns true if the date is tomorrow's date.
func (v VDate) IsTomorrow() bool {
return v.d.DaysSinceEpoch() - 1 == date.Today().DaysSinceEpoch()
}

// String formats the date in basic ISO8601 format YYYY-MM-DD.
func (v VDate) String() string {
return v.d.String()
Expand Down Expand Up @@ -111,23 +131,6 @@ func (v VDate) Previous() VDateDelta {
// Only lossy transcoding is supported here because the intention is that data exchange should be
// via the main Date type; VDate is only intended for output through view layers.

// MarshalJSON implements the json.Marshaler interface.
//func (v VDate) MarshalJSON() ([]byte, error) {
// return v.v.MarshalJSON()
//}

// UnmarshalJSON implements the json.Unmarshaler interface.
// Note that the format value gets lost.
//func (v *VDate) UnmarshalJSON(data []byte) (err error) {
// u := &date.Date{}
// err = u.UnmarshalJSON(data)
// if err == nil {
// v.v = *u
// v.f = DefaultFormat
// }
// return err
//}

// MarshalText implements the encoding.TextMarshaler interface.
func (v VDate) MarshalText() ([]byte, error) {
return v.d.MarshalText()
Expand Down
39 changes: 39 additions & 0 deletions view/vdate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,44 @@ func TestBasicFormatting(t *testing.T) {
is(t, d.Year(), "2016")
}

func TestDate(t *testing.T) {
d := date.New(2016, 2, 7)
vd := NewVDate(d)
if vd.Date() != d {
t.Errorf("%v != %v", vd.Date(), d)
}
}

func TestIsToday(t *testing.T) {
today := date.Today()

cases := []struct {
value VDate
expectYesterday bool
expectToday bool
expectTomorrow bool
}{
{NewVDate(date.New(2012, time.June, 25)), false, false, false},
{NewVDate(today.Add(-2)), false, false, false},
{NewVDate(today.Add(-1)), true, false, false},
{NewVDate(today.Add(0)), false, true, false},
{NewVDate(today.Add(1)), false, false, true},
{NewVDate(today.Add(2)), false, false, false},
}
for _, c := range cases {
if c.value.IsYesterday() != c.expectYesterday {
t.Errorf("%s should be 'yesterday': %v", c.value, c.expectYesterday)
}
if c.value.IsToday() != c.expectToday {
t.Errorf("%s should be 'today': %v", c.value, c.expectToday)
}
if c.value.IsTomorrow() != c.expectTomorrow {
t.Errorf("%s should be 'tomorrow': %v", c.value, c.expectTomorrow)
}
}

}

func TestNext(t *testing.T) {
d := NewVDate(date.New(2016, 2, 7))
is(t, d.Next().Day().String(), "2016-02-08")
Expand All @@ -45,6 +83,7 @@ func TestPrevious(t *testing.T) {
}

func is(t *testing.T, s1, s2 string) {
t.Helper()
if s1 != s2 {
t.Errorf("%s != %s", s1, s2)
}
Expand Down

0 comments on commit 511bb2b

Please sign in to comment.