Skip to content

Commit

Permalink
Merge formatDuration and printPrettyDuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkaeriit committed Aug 27, 2024
1 parent 7945c99 commit 6a8ba8e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 31 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ func newUser(s ssh.Session) *User {
u.writeln(Devbot, "**You are banned**. If you feel this was a mistake, please reach out to the server admin. Include the following information: [ID "+u.id+"]")
if banInfo.UseTime {
when := time.Until(banInfo.UnbanTime)
u.writeln(Devbot, "You will be unbaned in "+formatDuration(when)+".")
u.writeln(Devbot, "You will be unbaned in "+printPrettyDuration(when)+".")
}
s.Close()
return nil
Expand Down
50 changes: 20 additions & 30 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,12 +418,27 @@ func holidaysCheck(u *User) {
}

func printPrettyDuration(d time.Duration) string {
s := d.Round(time.Minute).String()
s = s[:len(s)-2] // cut off "0s" at the end
if s == "" { // we cut off the seconds so if there's nothing in the string it means it was made of only seconds.
s = "< 1m"
seconds := int(d.Seconds())
minutes := seconds / 60
seconds = seconds % 60

Check failure on line 423 in util.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to seconds (ineffassign)
if minutes == 0 {
return "< 1 minute"
}
return s

hours := minutes / 60
minutes = minutes % 60
ret := fmt.Sprintf("%v minutes", minutes)
if hours == 0 {
return ret
}

days := hours / 24
hours = hours % 24
ret = fmt.Sprintf("%v hours, and %v", hours, ret)
if days == 0 {
return ret
}
return fmt.Sprintf("%v days, %v", days, ret)
}

func fmtTime(u *User, lastStamp time.Time) string {
Expand Down Expand Up @@ -487,28 +502,3 @@ func genKey() (ed25519.PrivateKey, ssh.PublicKey, error) {
}
return priv, sshPubKey, nil
}

func formatDuration(d time.Duration) string {
seconds := int(d.Seconds())
minutes := seconds / 60
seconds = seconds % 60
ret := fmt.Sprintf("%v seconds", seconds)
if minutes == 0 {
return ret
}

hours := minutes / 60
minutes = minutes % 60
ret = fmt.Sprintf("%v minutes, and %v", minutes, ret)
if hours == 0 {
return ret
}

days := hours / 24
hours = hours % 24
ret = fmt.Sprintf("%v hours, %v", hours, ret)
if days == 0 {
return ret
}
return fmt.Sprintf("%v days, %v", days, ret)
}

0 comments on commit 6a8ba8e

Please sign in to comment.