Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert to days,h,... duration over 24h #23

Merged
merged 3 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions bot.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"strconv"
"strings"
"time"

Expand All @@ -18,12 +19,12 @@ var BotToken string
// State for edit to replies.
var msgSet *fixedmap.FixedMap[string, string]

var startTime time.Time
var botStartTime time.Time

const Unknown = "unknown"

func Run(maxHistoryLength int) {
startTime = time.Now()
botStartTime = time.Now()
msgSet = fixedmap.NewFixedMap[string, string](maxHistoryLength)
// create a session
session, err := discordgo.New("Bot " + BotToken)
Expand Down Expand Up @@ -85,6 +86,25 @@ func removeTripleBackticks(s string) string {
return s
}

func UptimeString(startTime time.Time) string {
return DurationString(time.Since(startTime))
}

// DurationString returns a human readable string for a duration.
// Expressed in days, hours, minutes, seconds and 10th of second.
// days, hours etc are omitted if they are 0.
func DurationString(d time.Duration) string {
rounded := d.Round(100 * time.Millisecond)
// get number of days out:
oneDay := 24 * time.Hour
days := int(rounded / oneDay)
if days == 0 {
return rounded.String()
}
rounded -= time.Duration(days) * oneDay
return strconv.Itoa(days) + "d" + rounded.String()
}

func eval(input string) string {
var res string
input = strings.TrimSpace(input) // we do it again so " !grol help" works
Expand All @@ -106,7 +126,7 @@ func eval(input string) string {
fallthrough
case "version":
res = "📦 Grol bot version: " + cli.ShortVersion + ", `grol` language version " + growlVersion +
" ⏰ Uptime: " + time.Since(startTime).Round(100*time.Millisecond).String()
" ⏰ Uptime: " + UptimeString(botStartTime)
case "buildinfo":
res = "📦ℹ️```" + cli.FullVersion + "```"
case "bug":
Expand Down
30 changes: 30 additions & 0 deletions bot_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main_test

import (
"testing"
"time"

bot "grol.io/grol-discord-bot"
)

func TestUptime(t *testing.T) {
delta := 100*time.Millisecond + 26*time.Hour + 42*time.Minute
startTime := time.Now().Add(-delta)
actual := bot.UptimeString(startTime)
expected := "1d2h42m0.1s"
if actual != expected {
t.Errorf("Expected %v, but got %v", expected, actual)
}
delta = 23*time.Hour + 5*time.Minute
actual = bot.DurationString(delta)
expected = "23h5m0s"
if actual != expected {
t.Errorf("Expected %v, but got %v", expected, actual)
}
delta = 96*time.Hour - 100*time.Millisecond
actual = bot.DurationString(delta)
expected = "3d23h59m59.9s"
if actual != expected {
t.Errorf("Expected %v, but got %v", expected, actual)
}
}