Skip to content

Commit 66fee99

Browse files
committed
/uptime and /whois relative timestamps made more precise
Replaced go-humanize dependency with our own logic. Fixes #259.
1 parent 14b380b commit 66fee99

File tree

5 files changed

+63
-12
lines changed

5 files changed

+63
-12
lines changed

Gopkg.lock

Lines changed: 1 addition & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

host.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"sync"
99
"time"
1010

11-
"github.com/dustin/go-humanize"
1211
"github.com/shazow/rateio"
1312
"github.com/shazow/ssh-chat/chat"
1413
"github.com/shazow/ssh-chat/chat/message"
@@ -382,7 +381,7 @@ func (h *Host) InitCommands(c *chat.Commands) {
382381
c.Add(chat.Command{
383382
Prefix: "/uptime",
384383
Handler: func(room *chat.Room, msg message.CommandMsg) error {
385-
room.Send(message.NewSystemMsg(humanize.Time(timeStarted), msg.From()))
384+
room.Send(message.NewSystemMsg(humanSince(time.Since(timeStarted)), msg.From()))
386385
return nil
387386
},
388387
})

humantime.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package sshchat
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
// humanSince returns a human-friendly relative time string
9+
func humanSince(d time.Duration) string {
10+
switch {
11+
case d < time.Minute*2:
12+
return fmt.Sprintf("%0.f seconds", d.Seconds())
13+
case d < time.Hour*2:
14+
return fmt.Sprintf("%0.f minutes", d.Minutes())
15+
case d < time.Hour*48:
16+
return fmt.Sprintf("%0.f hours", d.Hours())
17+
}
18+
return fmt.Sprintf("%0.f days", d.Hours()/24)
19+
}

humantime_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package sshchat
2+
3+
import (
4+
"testing"
5+
"time"
6+
)
7+
8+
func TestHumanSince(t *testing.T) {
9+
tests := []struct {
10+
input time.Duration
11+
expected string
12+
}{
13+
{
14+
time.Second * 42,
15+
"42 seconds",
16+
},
17+
{
18+
time.Second * 60 * 5,
19+
"5 minutes",
20+
},
21+
{
22+
time.Hour * 3,
23+
"3 hours",
24+
},
25+
{
26+
time.Hour * 49,
27+
"2 days",
28+
},
29+
{
30+
time.Hour * 24 * 900,
31+
"900 days",
32+
},
33+
}
34+
35+
for _, test := range tests {
36+
if actual, expected := humanSince(test.input), test.expected; actual != expected {
37+
t.Errorf("Got: %q; Expected: %q", actual, expected)
38+
}
39+
}
40+
}

identity.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"net"
55
"time"
66

7-
"github.com/dustin/go-humanize"
87
"github.com/shazow/ssh-chat/chat"
98
"github.com/shazow/ssh-chat/chat/message"
109
"github.com/shazow/ssh-chat/sshd"
@@ -51,7 +50,7 @@ func (i Identity) Whois() string {
5150
return "name: " + i.Name() + message.Newline +
5251
" > fingerprint: " + fingerprint + message.Newline +
5352
" > client: " + chat.SanitizeData(string(i.ClientVersion())) + message.Newline +
54-
" > joined: " + humanize.Time(i.created)
53+
" > joined: " + humanSince(time.Since(i.created)) + " ago"
5554
}
5655

5756
// WhoisAdmin returns a whois description for admin users.
@@ -65,5 +64,5 @@ func (i Identity) WhoisAdmin() string {
6564
" > ip: " + ip + message.Newline +
6665
" > fingerprint: " + fingerprint + message.Newline +
6766
" > client: " + chat.SanitizeData(string(i.ClientVersion())) + message.Newline +
68-
" > joined: " + humanize.Time(i.created)
67+
" > joined: " + humanSince(time.Since(i.created)) + " ago"
6968
}

0 commit comments

Comments
 (0)