factors out duration formatting to sneak/goutil
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2020-03-30 15:43:56 -07:00
parent 4b745db52e
commit d203dac078
4 changed files with 10 additions and 26 deletions

View File

@@ -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,

View File

@@ -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()))
}