diff --git a/clock/clock.go b/clock/clock.go index e8f0b36..52f34cd 100644 --- a/clock/clock.go +++ b/clock/clock.go @@ -22,11 +22,11 @@ type Clock struct { wall uint64 } -func abs[Int int | uint64](wall Int) uint64 { +func abs[Int int | float64 | uint64](wall Int) uint64 { for wall < 0 { wall += secondsPerDay } - return uint64(wall % secondsPerDay) + return uint64(int64(wall) % secondsPerDay) } func New(hour, min, sec int) Clock { @@ -134,7 +134,7 @@ func (c Clock) Compare(u Clock) int { } func (c Clock) Add(d time.Duration) Clock { - return Clock{abs(c.wall + uint64(d.Seconds()))} + return Clock{abs(float64(c.wall) + d.Seconds())} } func (c Clock) Sub(u Clock) time.Duration { diff --git a/clock/clock_test.go b/clock/clock_test.go index 028e504..29e6893 100644 --- a/clock/clock_test.go +++ b/clock/clock_test.go @@ -74,6 +74,22 @@ func TestSeconds(t *testing.T) { } } +func TestAdd(t *testing.T) { + for i, tc := range []struct { + c Clock + d time.Duration + u Clock + }{ + {Clock{}, 0, Clock{}}, + {Clock{}, time.Second, New(0, 0, 1)}, + {New(0, 0, 1), -time.Second, Clock{}}, + } { + if got := tc.c.Add(tc.d); got != tc.u { + t.Errorf("#%d: got %s; want %s", i, got, tc.u) + } + } +} + func TestSub(t *testing.T) { for i, tc := range []struct { c Clock @@ -88,7 +104,7 @@ func TestSub(t *testing.T) { {New(12, 0, 0), New(12, 30, 0), -30 * time.Minute}, } { if got := tc.c.Sub(tc.u); got != tc.d { - t.Errorf("#%d: Sub(%v, %v): got %v; want %v", i, tc.c, tc.u, got, tc.d) + t.Errorf("#%d: got %v; want %v", i, got, tc.d) } } }