Skip to content

Commit

Permalink
Add minutes value object
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulrich Lissé committed Apr 5, 2019
1 parent 8fbb190 commit 9c693da
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
31 changes: 31 additions & 0 deletions date/minutes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package date

import (
"math"
"time"
)

type Minutes struct {
duration time.Duration
}

func NewMinutes(i int) Minutes {
return Minutes{duration: time.Duration(i) * time.Minute}
}

func ParseMinutes(s string) (Minutes, error) {
d, err := time.ParseDuration(s)
if err != nil {
return Minutes{}, err
}

return Minutes{duration: d.Truncate(time.Minute)}, nil
}

func (m Minutes) Value() int {
return int(math.Min(m.duration.Minutes(), math.MaxInt32))
}

func (m Minutes) String() string {
return m.duration.String()
}
37 changes: 37 additions & 0 deletions date/minutes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package date_test

import (
"github.com/leanovate/mite-go/date"
"github.com/stretchr/testify/assert"
"testing"
)

func Test_ParseMinutes(t *testing.T) {
expected := date.NewMinutes(23)
actual, err := date.ParseMinutes("23m")

assert.Nil(t, err)
assert.Equal(t, expected, actual)

actual, err = date.ParseMinutes("23m11s")

assert.Nil(t, err)
assert.Equal(t, expected, actual)

_, err = date.ParseMinutes("1970-01-01")
assert.NotNil(t, err)
}

func TestMinutes_Value(t *testing.T) {
expected := 23
actual := date.NewMinutes(23).Value()

assert.Equal(t, expected, actual)
}

func TestMinutes_String(t *testing.T) {
expected := "23m0s"
actual := date.NewMinutes(23).String()

assert.Equal(t, expected, actual)
}

0 comments on commit 9c693da

Please sign in to comment.