-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ulrich Lissé
committed
Apr 5, 2019
1 parent
8fbb190
commit 9c693da
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |