Exercism: Go version of the 'Clock' exercise.
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
// Package clock represents a dateless time of day.
package clock
import "fmt"
const ( testVersion = 3 minutesPerHour = 60 minutesPerDay = 1440 )
// A Clock stores a minute value representing a time of day.
type Clock int
// String returns an hour:minute representation of the Clock.
func (c Clock) String() string { return fmt.Sprintf("%02d:%02d", c/minutesPerHour, c%minutesPerHour) }
// Add returns a Clock adjusted by the number of minutes specified.
func (c Clock) Add(minutes int) Clock { return parseTime(0, int(c)+minutes) }
// New returns a Clock constructed from the given hour and minute values.
func New(hour, minutes int) Clock { return parseTime(hour, minutes) }
// parseTime computes the number of minutes that represent the time.
func parseTime(hour, minutes int) Clock { // Compute the total minutes, factoring away multiple day values.
total := (hour*minutesPerHour + minutes) % minutesPerDay // If total minutes is negative, roll forward one day.
if total < 0 { total += minutesPerDay } return Clock(total) }
// Benchmark versus previous iteration:
//
// benchmark old ns/op new ns/op delta
// BenchmarkAddMinutes-12 45.7 29.7 -35.01%
// BenchmarkCreateClocks-12 52.1 33.9 -34.93%
|