From 078fbbe82877439d04fd7e0cbb335d7ea7cac6fd Mon Sep 17 00:00:00 2001 From: Andrey Petrov Date: Sun, 17 Mar 2019 15:01:41 -0400 Subject: [PATCH] chat, internal/humantime: Tweak departure message --- chat/room.go | 2 +- internal/humantime/humantime.go | 10 ++++------ internal/humantime/humantime_test.go | 10 +++++----- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/chat/room.go b/chat/room.go index dffde7d..f387990 100644 --- a/chat/room.go +++ b/chat/room.go @@ -156,7 +156,7 @@ func (r *Room) Leave(u *message.User) error { if err != nil { return err } - s := fmt.Sprintf("%s left. (Connected %s)", u.Name(), humantime.Since(u.Joined())) + s := fmt.Sprintf("%s left. (After %s)", u.Name(), humantime.Since(u.Joined())) r.Send(message.NewAnnounceMsg(s)) return nil } diff --git a/internal/humantime/humantime.go b/internal/humantime/humantime.go index 8c28d84..4a40607 100644 --- a/internal/humantime/humantime.go +++ b/internal/humantime/humantime.go @@ -5,19 +5,17 @@ import ( "time" ) -// humanSince returns a human-friendly relative time string +// since returns a human-friendly relative time string func Since(t time.Time) string { d := time.Since(t) switch { - case d < time.Second*2: - //e.g. "516.971µs", "535.412009ms", "1.880689686s" - return d.String() case d < time.Minute*2: return fmt.Sprintf("%0.f seconds", d.Seconds()) case d < time.Hour*2: return fmt.Sprintf("%0.f minutes", d.Minutes()) case d < time.Hour*48: - return fmt.Sprintf("%0.f hours", d.Hours()) + return fmt.Sprintf("%0.1f hours", d.Minutes()/60) } - return fmt.Sprintf("%0.f days", d.Hours()/24) + days := d.Minutes() / (24 * 60) + return fmt.Sprintf("%0.1f days", days) } diff --git a/internal/humantime/humantime_test.go b/internal/humantime/humantime_test.go index d5f14cd..8ac99fd 100644 --- a/internal/humantime/humantime_test.go +++ b/internal/humantime/humantime_test.go @@ -19,16 +19,16 @@ func TestHumanSince(t *testing.T) { "5 minutes", }, { - time.Hour * 3, - "3 hours", + time.Minute * 185, + "3.1 hours", }, { time.Hour * 49, - "2 days", + "2.0 days", }, { - time.Hour * 24 * 900, - "900 days", + time.Hour * (24*900 + 12), + "900.5 days", }, }