Skip to content

Commit

Permalink
Conflate local_date and minutes into datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulrich Lissé committed Apr 8, 2019
1 parent 2f6a05c commit 5e22017
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 70 deletions.
31 changes: 30 additions & 1 deletion domain/local_date.go → domain/datetime.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package domain

import "time"
import (
"math"
"strings"
"time"
)

const ISO8601 = "2006-01-02"

Expand Down Expand Up @@ -32,3 +36,28 @@ func (d LocalDate) Add(years int, months int, days int) LocalDate {
func (d LocalDate) String() string {
return d.time.Format(ISO8601)
}

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.Round(time.Minute)}, nil
}

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

func (m Minutes) String() string {
return strings.TrimSuffix(m.duration.String(), "0s")
}
30 changes: 30 additions & 0 deletions domain/local_date_test.go → domain/datetime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,33 @@ func TestLocalDate_String(t *testing.T) {

assert.Equal(t, expected, actual)
}

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

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

actual, err = domain.ParseMinutes("22m33s")

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

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

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

assert.Equal(t, expected, actual)
}

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

assert.Equal(t, expected, actual)
}
32 changes: 0 additions & 32 deletions domain/minutes.go

This file was deleted.

37 changes: 0 additions & 37 deletions domain/minutes_test.go

This file was deleted.

0 comments on commit 5e22017

Please sign in to comment.