Skip to content

Commit

Permalink
feat: implement Stringer for Interval
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfogre committed Jan 15, 2021
1 parent 340b11f commit c2c2494
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
17 changes: 17 additions & 0 deletions timeseq.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,23 @@ func (i Interval) Contain(t time.Time) bool {
return true
}

// String returns the interval formatted using the RFC3339 format string
func (i *Interval) String() string {
return i.Format(time.RFC3339)
}

// Format returns a textual representation of the time value formatted according to layout
func (i *Interval) Format(layout string) string {
notBefore, notAfter := "nil", "nil"
if i.NotBefore != nil {
notBefore = i.NotBefore.Format(layout)
}
if i.NotAfter != nil {
notAfter = i.NotAfter.Format(layout)
}
return notBefore + "~" + notAfter
}

// BeginAt is alias of AfterOrEqual
func (i Interval) BeginAt(t time.Time) Interval {
return i.AfterOrEqual(t)
Expand Down
82 changes: 82 additions & 0 deletions timeseq_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package timeseq

import (
"fmt"
"testing"
"time"
)
Expand Down Expand Up @@ -152,3 +153,84 @@ func Test_timeKey_Time(t *testing.T) {
})
}
}

func TestInterval_String(t *testing.T) {
now := time.Now()

tests := []struct {
name string
interval Interval
want string
}{
{
name: "regular",
interval: Interval{}.BeginAt(now).EndAt(now.Add(time.Hour)),
want: fmt.Sprintf("%v~%v", now.Format(time.RFC3339), now.Add(time.Hour).Format(time.RFC3339)),
},
{
name: "miss begin",
interval: Interval{}.EndAt(now),
want: fmt.Sprintf("nil~%v", now.Format(time.RFC3339)),
},
{
name: "miss end",
interval: Interval{}.BeginAt(now),
want: fmt.Sprintf("%v~nil", now.Format(time.RFC3339)),
},
{
name: "miss all",
interval: Interval{},
want: "nil~nil",
},
{
name: "after",
interval: Interval{}.After(now),
want: fmt.Sprintf("%v~nil", now.Format(time.RFC3339)),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.interval.String(); got != tt.want {
t.Errorf("String() = %v, want %v", got, tt.want)
}
})
}
}

func TestInterval_Format(t *testing.T) {
now := time.Now()

type args struct {
layout string
}
tests := []struct {
name string
interval Interval
args args
want string
}{
{
name: "regular",
interval: Interval{}.BeginAt(now).EndAt(now.Add(time.Hour)),
args: args{
layout: time.RFC3339,
},
want: fmt.Sprintf("%v~%v", now.Format(time.RFC3339), now.Add(time.Hour).Format(time.RFC3339)),
},
{
name: "nano",
interval: Interval{}.After(now),
args: args{
layout: time.RFC3339Nano,
},
want: fmt.Sprintf("%v~nil", now.Add(1).Format(time.RFC3339Nano)),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.interval.Format(tt.args.layout); got != tt.want {
t.Errorf("Format() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit c2c2494

Please sign in to comment.