exercism/go/clock/clock.go

35 lines
827 B
Go

package clock
import "fmt"
// TestVersion is for exercism
const TestVersion = 2
// Clock is an int representing the time of day
type Clock int
// Time returns a Clock representing the given hour & minute
func Time(hour, minute int) Clock {
return TimeFromMinutes(hour*60 + minute)
}
// String returns a string representation of Clock
func (c Clock) String() string {
return fmt.Sprintf("%02d:%02d", c/60, c%60)
}
// Add adds minutes to the clock
func (c Clock) Add(minutes int) Clock {
return TimeFromMinutes(int(c) + minutes)
}
// TimeFromMinutes takes a minutes integer value and returns a Clock
func TimeFromMinutes(minutes int) Clock {
for minutes < 0 {
// Keep adding 24 hours to the minutes value until it's positive
minutes = minutes + (24 * 60)
}
minutes = minutes % (24 * 60)
return Clock(minutes)
}