Skip to content

Instantly share code, notes, and snippets.

@gabesullice
Created September 30, 2014 22:54
Show Gist options
  • Save gabesullice/d46712d378d54ef3d994 to your computer and use it in GitHub Desktop.
Save gabesullice/d46712d378d54ef3d994 to your computer and use it in GitHub Desktop.
type Clock struct {
h, m int
}
func (c *Clock) set(h, m int) {
var dh int
dh, c.m = c.decompose(m, 60)
_, c.h = c.decompose(h+dh, 24)
}
func (c Clock) String() string {
return fmt.Sprintf("%02d:%02d", c.h, c.m)
}
func (c *Clock) Add(m int) *Clock {
c.set(c.h, c.m+m)
return c
}
func (c *Clock) decompose(quot int, div int) (h, m int) {
h = quot / div
m = quot % div
if m < 0 {
m = div + m
h = h - 1
}
return
}
func New(h, m int) *Clock {
c := new(Clock)
c.set(h, m)
return c
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment