// Package clock is a simple clock without date handling.
package clock
import (
"strconv"
)
// Clock is simple clock.
type Clock struct {
hour int
min int
}
// New constructs a new Clock with proper hour and minute values.
func New(hour, minute int) Clock {
// handle hour turns because of mins
if minute >= 0 {
hour += minute / 60
} else {
hour += (minute+1)/60 - 1
}
// first normalize, then make it positive
minute = minute % 60
if minute < 0 {
minute += 60
}
hour %= 24
if hour < 0 {
hour += 24
}
return Clock{hour: hour, min: minute}
}
// String returns Clock in "hh:mm" format.
func (c Clock) String() string {
h := strconv.Itoa(c.hour)
if c.hour < 10 {
h = "0" + h
}
m := strconv.Itoa(c.min)
if c.min < 10 {
m = "0" + m
}
return h + ":" + m
}
// Add adds minutes to c.
func (c Clock) Add(minutes int) Clock {
return New(c.hour, c.min+minutes)
}
// Subtract subtracts minutes from c.
func (c Clock) Subtract(minutes int) Clock {
return New(c.hour, c.min-minutes)
}