Skip to content

Commit

Permalink
clock: Add IsZero and Seconds (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunshineplan committed Jan 15, 2024
1 parent 75780cc commit 37dc592
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions clock/clock.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ func (c Clock) Second() int {
return int(c.wall % secondsPerMinute)
}

func (c Clock) Seconds() int {
return int(c.wall)
}

func (c Clock) String() string {
return fmt.Sprintf("%d:%02d:%02d", c.Hour(), c.Minute(), c.Second())
}
Expand All @@ -101,6 +105,10 @@ func (c *Clock) UnmarshalText(text []byte) error {
return nil
}

func (c Clock) IsZero() bool {
return c.wall == 0
}

func (c Clock) After(u Clock) bool {
return c.wall > u.wall
}
Expand Down
16 changes: 16 additions & 0 deletions clock/clock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ func TestParse(t *testing.T) {
}
}

func TestSeconds(t *testing.T) {
for i, testcase := range []struct {
c Clock
expected int
}{
{New(0, 0, 0), 0},
{New(7, 1, 2), 7*secondsPerHour + 1*secondsPerMinute + 2},
{New(19, 4, 30), 19*secondsPerHour + 4*secondsPerMinute + 30},
{New(25, 4, 30), secondsPerHour + 4*secondsPerMinute + 30},
} {
if res := testcase.c.Seconds(); res != testcase.expected {
t.Errorf("#%d: expected %d; got %d", i, testcase.expected, res)
}
}
}

func TestSub(t *testing.T) {
for i, tc := range []struct {
c Clock
Expand Down

0 comments on commit 37dc592

Please sign in to comment.