/uptime and /whois relative timestamps made more precise

Replaced go-humanize dependency with our own logic.

Fixes #259.
This commit is contained in:
Andrey Petrov 2018-01-19 12:34:15 -05:00
parent 14b380b473
commit 66fee99a81
5 changed files with 63 additions and 12 deletions

8
Gopkg.lock generated
View File

@ -7,12 +7,6 @@
packages = [".","golog"]
revision = "61e686294e58a8698a9e1091268bb4ac1116bd5e"
[[projects]]
branch = "master"
name = "github.com/dustin/go-humanize"
packages = ["."]
revision = "bb3d318650d48840a39aa21a027c6630e198e626"
[[projects]]
branch = "master"
name = "github.com/howeyc/gopass"
@ -46,6 +40,6 @@
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "36fa3b8ab2269908ef899a2f5b7320cb792d3a1039cb68afc8b51becde57f850"
inputs-digest = "48a7f7477a28e61efdd4256fe7f426bfaf93df53b5731e905088c0e9c2f10d3b"
solver-name = "gps-cdcl"
solver-version = 1

View File

@ -8,7 +8,6 @@ import (
"sync"
"time"
"github.com/dustin/go-humanize"
"github.com/shazow/rateio"
"github.com/shazow/ssh-chat/chat"
"github.com/shazow/ssh-chat/chat/message"
@ -382,7 +381,7 @@ func (h *Host) InitCommands(c *chat.Commands) {
c.Add(chat.Command{
Prefix: "/uptime",
Handler: func(room *chat.Room, msg message.CommandMsg) error {
room.Send(message.NewSystemMsg(humanize.Time(timeStarted), msg.From()))
room.Send(message.NewSystemMsg(humanSince(time.Since(timeStarted)), msg.From()))
return nil
},
})

19
humantime.go Normal file
View File

@ -0,0 +1,19 @@
package sshchat
import (
"fmt"
"time"
)
// humanSince returns a human-friendly relative time string
func humanSince(d time.Duration) string {
switch {
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.f days", d.Hours()/24)
}

40
humantime_test.go Normal file
View File

@ -0,0 +1,40 @@
package sshchat
import (
"testing"
"time"
)
func TestHumanSince(t *testing.T) {
tests := []struct {
input time.Duration
expected string
}{
{
time.Second * 42,
"42 seconds",
},
{
time.Second * 60 * 5,
"5 minutes",
},
{
time.Hour * 3,
"3 hours",
},
{
time.Hour * 49,
"2 days",
},
{
time.Hour * 24 * 900,
"900 days",
},
}
for _, test := range tests {
if actual, expected := humanSince(test.input), test.expected; actual != expected {
t.Errorf("Got: %q; Expected: %q", actual, expected)
}
}
}

View File

@ -4,7 +4,6 @@ import (
"net"
"time"
"github.com/dustin/go-humanize"
"github.com/shazow/ssh-chat/chat"
"github.com/shazow/ssh-chat/chat/message"
"github.com/shazow/ssh-chat/sshd"
@ -51,7 +50,7 @@ func (i Identity) Whois() string {
return "name: " + i.Name() + message.Newline +
" > fingerprint: " + fingerprint + message.Newline +
" > client: " + chat.SanitizeData(string(i.ClientVersion())) + message.Newline +
" > joined: " + humanize.Time(i.created)
" > joined: " + humanSince(time.Since(i.created)) + " ago"
}
// WhoisAdmin returns a whois description for admin users.
@ -65,5 +64,5 @@ func (i Identity) WhoisAdmin() string {
" > ip: " + ip + message.Newline +
" > fingerprint: " + fingerprint + message.Newline +
" > client: " + chat.SanitizeData(string(i.ClientVersion())) + message.Newline +
" > joined: " + humanize.Time(i.created)
" > joined: " + humanSince(time.Since(i.created)) + " ago"
}