chat, internal/humantime: Tweak departure message

This commit is contained in:
Andrey Petrov 2019-03-17 15:01:41 -04:00
parent fa516ba085
commit 078fbbe828
3 changed files with 10 additions and 12 deletions

View File

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

View File

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

View File

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