Skip to content

Instantly share code, notes, and snippets.

@fuzzy
Created July 23, 2016 19:23
Show Gist options
  • Save fuzzy/0b6beb5483c990bf0f562cedf220d9d8 to your computer and use it in GitHub Desktop.
Save fuzzy/0b6beb5483c990bf0f562cedf220d9d8 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"fmt"
"math"
"time"
)
func Strappend(p string, a string) string {
b := bytes.NewBufferString(p)
for i := 0; i < len(a); i++ {
b.WriteByte(a[i])
}
return b.String()
}
func HumanTime(s int) string {
tdesc := map[string]int{
"s": 1,
"m": 60,
"h": 60 * 60,
"d": (60 * 60) * 24,
"w": ((60 * 60) * 24) * 7,
"M": ((60 * 60) * 24) * 30,
"y": ((60 * 60) * 24) * 365,
}
keys := []string{"y", "M", "w", "d", "h", "m", "s"}
retv := ""
for _, t := range keys {
if s > tdesc[t] {
retv = Strappend(retv, fmt.Sprintf("%02d%s", s/tdesc[t], t))
s = int(math.Remainder(float64(s), float64(tdesc[t])))
}
}
return retv
}
// Starting things rolling down the hill
func main() {
for i := 0; i <= 160; i++ {
fmt.Printf(" %d == %s\n", i, HumanTime(i))
time.Sleep(300 * time.Millisecond)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment