feat: store last seen date for users and present in stats and user lists, closes #9007
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"regexp"
|
||||
"time"
|
||||
)
|
||||
|
||||
func StringsFallback2(val1 string, val2 string) string {
|
||||
@@ -28,3 +31,34 @@ func SplitString(str string) []string {
|
||||
|
||||
return regexp.MustCompile("[, ]+").Split(str, -1)
|
||||
}
|
||||
|
||||
func GetAgeString(t time.Time) string {
|
||||
if t.IsZero() {
|
||||
return "?"
|
||||
}
|
||||
|
||||
sinceNow := time.Since(t)
|
||||
minutes := sinceNow.Minutes()
|
||||
years := int(math.Floor(minutes / 525600))
|
||||
months := int(math.Floor(minutes / 43800))
|
||||
days := int(math.Floor(minutes / 1440))
|
||||
hours := int(math.Floor(minutes / 60))
|
||||
|
||||
if years > 0 {
|
||||
return fmt.Sprintf("%dy", years)
|
||||
}
|
||||
if months > 0 {
|
||||
return fmt.Sprintf("%dM", months)
|
||||
}
|
||||
if days > 0 {
|
||||
return fmt.Sprintf("%dd", days)
|
||||
}
|
||||
if hours > 0 {
|
||||
return fmt.Sprintf("%dh", hours)
|
||||
}
|
||||
if int(minutes) > 0 {
|
||||
return fmt.Sprintf("%dm", int(minutes))
|
||||
}
|
||||
|
||||
return "< 1m"
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package util
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
@@ -24,3 +25,15 @@ func TestSplitString(t *testing.T) {
|
||||
So(SplitString("test1 , test2 test3"), ShouldResemble, []string{"test1", "test2", "test3"})
|
||||
})
|
||||
}
|
||||
|
||||
func TestDateAge(t *testing.T) {
|
||||
Convey("GetAgeString", t, func() {
|
||||
So(GetAgeString(time.Time{}), ShouldEqual, "?")
|
||||
So(GetAgeString(time.Now().Add(-time.Second*2)), ShouldEqual, "< 1m")
|
||||
So(GetAgeString(time.Now().Add(-time.Minute*2)), ShouldEqual, "2m")
|
||||
So(GetAgeString(time.Now().Add(-time.Hour*2)), ShouldEqual, "2h")
|
||||
So(GetAgeString(time.Now().Add(-time.Hour*24*3)), ShouldEqual, "3d")
|
||||
So(GetAgeString(time.Now().Add(-time.Hour*24*67)), ShouldEqual, "2M")
|
||||
So(GetAgeString(time.Now().Add(-time.Hour*24*409)), ShouldEqual, "1y")
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user