Skip to content

Instantly share code, notes, and snippets.

@fuzzy
Last active July 23, 2016 20:00
Show Gist options
  • Save fuzzy/4a57f917a37250da9785989413335aef to your computer and use it in GitHub Desktop.
Save fuzzy/4a57f917a37250da9785989413335aef to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"fmt"
"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 {
val := (s / tdesc[t])
tgt := (val * tdesc[t])
if s >= tdesc[t] {
retv = Strappend(retv, fmt.Sprintf("%02d%s", val, t))
s = (s - tgt)
}
}
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(100 * time.Millisecond)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment