Skip to content

Instantly share code, notes, and snippets.

@jreisinger
Last active September 29, 2021 17:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jreisinger/efcaf43d60cf29939098f48baff133ab to your computer and use it in GitHub Desktop.
Save jreisinger/efcaf43d60cf29939098f48baff133ab to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"time"
)
func main() {
start := time.Now()
go func() {
for {
elapsed := time.Since(start)
fmt.Printf("\r%s", fmtDuration(elapsed))
time.Sleep(time.Millisecond * 100)
}
}()
var enter string
fmt.Scanln(&enter)
}
func fmtDuration(d time.Duration) string {
d = d.Round(time.Millisecond)
h := d / time.Hour
d -= h * time.Hour
m := d / time.Minute
d -= m * time.Minute
s := d / time.Second
d -= s * time.Second
ms := d / time.Millisecond
return fmt.Sprintf("%02d:%02d:%02d.%03d", h, m, s, ms)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment