The challenge

Clock shows h hours, m minutes and s seconds after midnight.

Your task is to write a function that returns the time since midnight in milliseconds.

Example:

h = 0
m = 1
s = 1

result = 61000

Input constraints:

  • 0 <= h <= 23
  • 0 <= m <= 59
  • 0 <= s <= 59

The solution in Golang

Option 1:

package solution
func Past(h, m, s int) int {
    return (h*3600000 + m*60000 + s*1000)    
}

Option 2:

package solution
func Past(h, m, s int) int {
    return (h*60*60+m*60+s)*1000
}

Option 3:

package solution
import "time"
func Past(h, m, s int) (ms int) {
  now := time.Unix(0, 0)
  now = now.Add(time.Duration(h) * time.Hour)
  now = now.Add(time.Duration(m) * time.Minute)
  now = now.Add(time.Duration(s) * time.Second)
  return int(now.Sub(time.Unix(0, 0)) / 1000000)
}

Test cases to validate our solution

package solution_test
import (
  . "github.com/onsi/ginkgo"
  . "github.com/onsi/gomega"
)
var _ = Describe("Basic tests", func() {
    It("Past(0, 1, 1)", func() { Expect(Past(0, 1, 1)).To(Equal(61000)) })
    It("Past(1, 1, 1)", func() { Expect(Past(1, 1, 1)).To(Equal(3661000)) })
    It("Past(0, 0, 0)", func() { Expect(Past(0, 0, 0)).To(Equal(0)) })
    It("Past(1, 0, 1)", func() { Expect(Past(1, 0, 1)).To(Equal(3601000)) })
    It("Past(1, 0, 0)", func() { Expect(Past(1, 0, 0)).To(Equal(3600000)) })
})