From 66fee99a81504adb147836534880a24862efbcdf Mon Sep 17 00:00:00 2001 From: Andrey Petrov Date: Fri, 19 Jan 2018 12:34:15 -0500 Subject: [PATCH] /uptime and /whois relative timestamps made more precise Replaced go-humanize dependency with our own logic. Fixes #259. --- Gopkg.lock | 8 +------- host.go | 3 +-- humantime.go | 19 +++++++++++++++++++ humantime_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ identity.go | 5 ++--- 5 files changed, 63 insertions(+), 12 deletions(-) create mode 100644 humantime.go create mode 100644 humantime_test.go diff --git a/Gopkg.lock b/Gopkg.lock index 86fb77c..6e88124 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -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 diff --git a/host.go b/host.go index 752cf2f..34a19df 100644 --- a/host.go +++ b/host.go @@ -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 }, }) diff --git a/humantime.go b/humantime.go new file mode 100644 index 0000000..4b361f9 --- /dev/null +++ b/humantime.go @@ -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) +} diff --git a/humantime_test.go b/humantime_test.go new file mode 100644 index 0000000..ca6c929 --- /dev/null +++ b/humantime_test.go @@ -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) + } + } +} diff --git a/identity.go b/identity.go index 50cee7e..9609785 100644 --- a/identity.go +++ b/identity.go @@ -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" }