|
|
@ -4,38 +4,54 @@ import "fmt" |
|
|
|
|
|
|
|
const ( |
|
|
|
testVersion = 3 |
|
|
|
minutesPerHour = 60 |
|
|
|
hoursPerDay = 24 |
|
|
|
minutesPerHour = 60 |
|
|
|
minutesPerDay = 1440 |
|
|
|
) |
|
|
|
|
|
|
|
// A Clock stores a minute value representing a time of day.
|
|
|
|
type Clock struct { |
|
|
|
hour int |
|
|
|
minutes int |
|
|
|
} |
|
|
|
|
|
|
|
func New(hour, minute int) Clock { |
|
|
|
h, m := adjustTime(hour, minute) |
|
|
|
return Clock{h, m} |
|
|
|
} |
|
|
|
|
|
|
|
// String returns an hour:minute representation of the Clock.
|
|
|
|
func (c Clock) String() string { |
|
|
|
return fmt.Sprintf("%02d:%02d", c.hour, c.minutes) |
|
|
|
return fmt.Sprintf("%02d:%02d", c.hour(), c.mins()) |
|
|
|
} |
|
|
|
|
|
|
|
// Add returns a Clock adjusted by the number of minutes specified.
|
|
|
|
func (c Clock) Add(minutes int) Clock { |
|
|
|
min := c.minutes + minutes |
|
|
|
c.hour, c.minutes = adjustTime(c.hour, min) |
|
|
|
c.minutes = parseTime(0, c.minutes+minutes) |
|
|
|
return c |
|
|
|
} |
|
|
|
|
|
|
|
func adjustTime(h, m int) (int, int) { |
|
|
|
h = (h + (m / minutesPerHour)) % hoursPerDay |
|
|
|
if h < 0 { |
|
|
|
h += hoursPerDay |
|
|
|
} |
|
|
|
m = m % minutesPerHour |
|
|
|
if m < 0 { |
|
|
|
m += minutesPerHour |
|
|
|
// hour computes the hourly portion of the Clock.
|
|
|
|
func (c Clock) hour() int { |
|
|
|
return c.minutes / minutesPerHour |
|
|
|
} |
|
|
|
|
|
|
|
// mins computes the non-hourly remainder of the Clock.
|
|
|
|
func (c Clock) mins() int { |
|
|
|
return c.minutes % minutesPerHour |
|
|
|
} |
|
|
|
|
|
|
|
// New returns a Clock constructed from the given hour and minute values.
|
|
|
|
func New(hour, minutes int) Clock { |
|
|
|
return Clock{parseTime(hour, minutes)} |
|
|
|
} |
|
|
|
|
|
|
|
// parseTime computes the number of minutes that represent the time.
|
|
|
|
func parseTime(hour, minutes int) int { |
|
|
|
// normalize hours that are out of range [0-23]
|
|
|
|
hour = hour % hoursPerDay |
|
|
|
// normalize any out of range values [0-1439]
|
|
|
|
minutes = minutes % minutesPerDay |
|
|
|
// compute the total minutes
|
|
|
|
total := hour * minutesPerHour |
|
|
|
total += minutes |
|
|
|
// normalize any negative minute values
|
|
|
|
if total < 0 { |
|
|
|
total += minutesPerDay |
|
|
|
} |
|
|
|
return h, m |
|
|
|
return total |
|
|
|
} |