summaryrefslogtreecommitdiffstats
path: root/utils.go
diff options
context:
space:
mode:
authorThibaut Horel <thibaut.horel@gmail.com>2017-06-04 19:30:25 -0400
committerThibaut Horel <thibaut.horel@gmail.com>2017-06-04 19:30:25 -0400
commit81522cbae16c6aadb1b789f3f875dc50f10cc005 (patch)
treeab95db0a843783d4cfcd328e091a01597e241ee1 /utils.go
parentf154ae1ec88146017abf3de9d14d119facb5fc4c (diff)
downloadlastfm-api-81522cbae16c6aadb1b789f3f875dc50f10cc005.tar.gz
Clean up
Diffstat (limited to 'utils.go')
-rw-r--r--utils.go17
1 files changed, 17 insertions, 0 deletions
diff --git a/utils.go b/utils.go
index 49fdc5b..2a47905 100644
--- a/utils.go
+++ b/utils.go
@@ -4,7 +4,9 @@ import (
"crypto/md5"
"crypto/rand"
"encoding/hex"
+ "fmt"
"net/http"
+ "time"
)
func randomToken(length int) string {
@@ -43,3 +45,18 @@ func (app *App) SetCookie(w http.ResponseWriter, name string, v interface{}, exp
}
http.SetCookie(w, cookie)
}
+
+func ago(t time.Time) string {
+ delta := time.Since(t)
+ if delta < time.Minute {
+ return fmt.Sprintf("%ds ago", int(delta/time.Second))
+ } else if delta < time.Hour {
+ return fmt.Sprintf("%dm ago", int(delta/time.Minute))
+ } else if delta < 24*time.Hour {
+ return fmt.Sprintf("%dh ago", int(delta/time.Hour))
+ } else if delta < 5*24*time.Hour {
+ return fmt.Sprintf("%dd ago", int(delta/(24*time.Hour)))
+ } else {
+ return t.Format("Jan 2")
+ }
+}