From d203dac0789c358557d5be93b7d4c1b1b3b7c86e Mon Sep 17 00:00:00 2001 From: sneak Date: Mon, 30 Mar 2020 15:43:56 -0700 Subject: [PATCH] factors out duration formatting to sneak/goutil --- go.mod | 1 + go.sum | 2 ++ hn/handlers.go | 13 +++++++------ hn/misc.go | 20 -------------------- 4 files changed, 10 insertions(+), 26 deletions(-) delete mode 100644 hn/misc.go diff --git a/go.mod b/go.mod index 3a98b2a..6d7e82b 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module git.eeqj.de/sneak/orangesite go 1.14 require ( + git.eeqj.de/sneak/goutil v0.0.0-20200330224009-e54964f792cd github.com/flosch/pongo2 v0.0.0-20190707114632-bbf5a6c351f4 github.com/hako/durafmt v0.0.0-20191009132224-3f39dc1ed9f4 github.com/jinzhu/gorm v1.9.12 diff --git a/go.sum b/go.sum index d754068..07b7d27 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +git.eeqj.de/sneak/goutil v0.0.0-20200330224009-e54964f792cd h1:LlQYjhr5NA5WK9f/s0QnnxHZE3YbnAhSkqQ/sJCdHk8= +git.eeqj.de/sneak/goutil v0.0.0-20200330224009-e54964f792cd/go.mod h1:eczIi5zp8IZnFLQbMF0Xufw6to+UMCbOxA4M4Hp7ORw= github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= diff --git a/hn/handlers.go b/hn/handlers.go index f478913..443d892 100644 --- a/hn/handlers.go +++ b/hn/handlers.go @@ -4,6 +4,7 @@ import ( "net/http" "time" + u "git.eeqj.de/sneak/goutil" "github.com/flosch/pongo2" "github.com/jinzhu/gorm" "github.com/labstack/echo" @@ -41,15 +42,15 @@ func (r *RequestHandlerSet) indexHandler(c echo.Context) error { for _, item := range fpi { fprows = append(fprows, fprow{ - Duration: timeDiffHuman(item.Disappeared, item.Appeared), - DurationSecs: timeDiffAbsSeconds(item.Disappeared, item.Appeared), + Duration: u.TimeDiffHuman(item.Disappeared, item.Appeared), + DurationSecs: u.TimeDiffAbsSeconds(item.Disappeared, item.Appeared), URL: item.URL, HNID: item.HNID, Score: item.Score, Title: item.Title, HighestRank: item.HighestRank, - TimeGone: timeDiffHuman(time.Now(), item.Disappeared), - TimeGoneSecs: timeDiffAbsSeconds(time.Now(), item.Disappeared), + TimeGone: u.TimeDiffHuman(time.Now(), item.Disappeared), + TimeGoneSecs: u.TimeDiffAbsSeconds(time.Now(), item.Disappeared), }) } @@ -70,8 +71,8 @@ func (r *RequestHandlerSet) indexHandler(c echo.Context) error { for _, item := range cur { currentfp = append(currentfp, rowtwo{ - Duration: timeDiffHuman(time.Now(), item.Appeared), - DurationSecs: timeDiffAbsSeconds(time.Now(), item.Appeared), + Duration: u.TimeDiffHuman(time.Now(), item.Appeared), + DurationSecs: u.TimeDiffAbsSeconds(time.Now(), item.Appeared), URL: item.URL, HNID: item.HNID, Score: item.Score, diff --git a/hn/misc.go b/hn/misc.go deleted file mode 100644 index 1e2095e..0000000 --- a/hn/misc.go +++ /dev/null @@ -1,20 +0,0 @@ -package hn - -import ( - "math" - "time" - - "github.com/hako/durafmt" -) - -func timeDiffHuman(first time.Time, second time.Time) string { - if first.Before(second) { - return durafmt.ParseShort(second.Sub(first)).String() - } else { - return durafmt.ParseShort(first.Sub(second)).String() - } -} - -func timeDiffAbsSeconds(first time.Time, second time.Time) uint { - return uint(math.Abs(first.Sub(second).Truncate(time.Second).Seconds())) -}