Skip to content

Commit

Permalink
Merge pull request #28 from gochore/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfogre authored Jan 15, 2021
2 parents 61d08b2 + c2c2494 commit 07f2dff
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 2 deletions.
4 changes: 2 additions & 2 deletions cmd/generate/gen_x_seq.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (v {{.Name}}) IsZero() bool {
return v.Value == 0 && v.Time.IsZero()
}

// IsZero return if time and value are both equal
// Equal return if time and value are both equal
func (v {{.Name}}) Equal(n {{.Name}}) bool {
return v.Value == n.Value && v.Time.Equal(n.Time)
}
Expand Down Expand Up @@ -206,7 +206,7 @@ func (s *{{.Name}}Seq) Max() {{.Name}} {
return max
}

// Max return the element with min value, return zero if empty
// Min return the element with min value, return zero if empty
func (s *{{.Name}}Seq) Min() {{.Name}} {
var min {{.Name}}
found := false
Expand Down
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 07f2dff

Please sign in to comment.