Created
September 30, 2014 22:54
-
-
Save gabesullice/d46712d378d54ef3d994 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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