Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Before() and After() methods to Date type. #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions date.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
// TODO(faulkner): technically this is incorrect since it'll match "2000-12-3456789"; useful if we want to trim the time from a datetime, but if we don't have that usecase then perhaps this should be more strict.
var datePattern = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}`)

var strictDatePattern = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}$`)

// Date represents a nil-able date encoded as ISO
type Date struct {
v string
Expand Down Expand Up @@ -63,6 +65,22 @@ func (v Date) DaysAgo() (Int64, error) {
return NewInt(int(time.Since(t).Hours() / 24)), nil
}

// Before reports whether the Date instance is before d.
func (v Date) Before(d Date) (bool, error) {
if !strictDatePattern.MatchString(v.String()) || !strictDatePattern.MatchString(d.String()) {
return false, errors.Errorf("malformed date - v: %v d: %v", v.v, d.v)
}
return v.String() < d.String(), nil
}

// After reports whether the Date instance is after d.
func (v Date) After(d Date) (bool, error) {
if !strictDatePattern.MatchString(v.String()) || !strictDatePattern.MatchString(d.String()) {
return false, errors.Errorf("malformed date - v %v d %v", v.v, d.v)
}
return v.String() > d.String(), nil
}

// String implements the fmt.Stringer interface
func (v Date) String() string {
return v.v
Expand Down
142 changes: 142 additions & 0 deletions date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,148 @@ func daysAgo(days int) string {
return newTime.Format("2006-01-02")
}

func TestDate_Before(t *testing.T) {
tests := []struct {
name string
this Date
other Date
want bool
}{
{
name: "this before other",
this: Date{v: "2022-06-20", present: true, initialized: true},
other: Date{v: "2023-05-22", present: true, initialized: true},
want: true,
},
{
name: "this not before other",
this: Date{v: "2023-05-30", present: true, initialized: true},
other: Date{v: "2023-05-22", present: true, initialized: true},
want: false,
},
{
name: "this equal other",
this: Date{v: "2023-05-30", present: true, initialized: true},
other: Date{v: "2023-05-30", present: true, initialized: true},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.this.Before(tt.other)
if err != nil {
t.Errorf("Date.Before(): error = %v", err)
}
if got != tt.want {
t.Errorf("Date.Before(): got = %v, want = %v", got, tt.want)
}
})
}
}

func TestDate_BeforeErr(t *testing.T) {
tests := []struct {
name string
this Date
other Date
}{
{
name: "nil",
this: Date{present: false, initialized: true},
other: Date{v: "2023-05-22", present: true, initialized: true},
},
{
name: "malformed this",
this: Date{v: "20230525", present: true, initialized: true},
other: Date{v: "2023-05-26", present: true, initialized: true},
},
{
name: "malformed other",
this: Date{v: "2023-05-25", present: true, initialized: true},
other: Date{v: "20230526", present: true, initialized: true},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := tt.this.Before(tt.other)
if err == nil {
t.Errorf("Date.Before(): got nil when expected error")
}
})
}
}

func TestDate_After(t *testing.T) {
tests := []struct {
name string
this Date
other Date
want bool
}{
{
name: "this after other",
this: Date{v: "2023-05-30", present: true, initialized: true},
other: Date{v: "2023-05-22", present: true, initialized: true},
want: true,
},
{
name: "this not after other",
this: Date{v: "2023-04-20", present: true, initialized: true},
other: Date{v: "2023-05-22", present: true, initialized: true},
want: false,
},
{
name: "this equal other",
this: Date{v: "2023-05-30", present: true, initialized: true},
other: Date{v: "2023-05-30", present: true, initialized: true},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.this.After(tt.other)
if err != nil {
t.Errorf("Date.After(): error = %v", err)
}
if got != tt.want {
t.Errorf("Date.After(): got = %v, want = %v", got, tt.want)
}
})
}
}

func TestDate_AfterErr(t *testing.T) {
tests := []struct {
name string
this Date
other Date
}{
{
name: "nil",
this: Date{present: false, initialized: true},
other: Date{v: "2023-05-22", present: true, initialized: true},
},
{
name: "malformed this",
this: Date{v: "20230525", present: true, initialized: true},
other: Date{v: "2023-05-26", present: true, initialized: true},
},
{
name: "malformed other",
this: Date{v: "2023-05-25", present: true, initialized: true},
other: Date{v: "20230526", present: true, initialized: true},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := tt.this.After(tt.other)
if err == nil {
t.Errorf("Date.After(): got nil when expected error")
}
})
}
}

func TestDate_String(t *testing.T) {
tests := []struct {
name string
Expand Down