Use stdlib time functions to print time estimation (Closes #18)

This commit is contained in:
Sacha Ligthert 2025-01-27 19:20:49 +01:00
parent 0e3aa42b78
commit fc555fa581
2 changed files with 7 additions and 46 deletions

View File

@ -146,7 +146,13 @@ func (solver *Solver) tracker() {
if rate_diff == 0 {
est_fin = "N/A"
} else {
est_fin = solver.secondsToHuman((solver.iter - solver.counter.Load()) / rate_avg)
duration_int := (solver.iter - solver.counter.Load()) / rate_avg
duration_string := strconv.Itoa(int(duration_int)) + "s"
est, err := time.ParseDuration(duration_string)
if err != nil {
est_fin = "parse error"
}
est_fin = est.String()
}
// Printing the progress

View File

@ -2,8 +2,6 @@ package solver
import (
"log"
"math"
"strconv"
"time"
)
@ -11,46 +9,3 @@ func (solver *Solver) timeTrack(start time.Time, msg string) {
elapsed := time.Since(start)
log.Printf("%s (%s)", msg, elapsed)
}
// Stolen from https://socketloop.com/tutorials/golang-convert-seconds-to-human-readable-time-format-example
func (solver *Solver) plural(count int, singular string) (result string) {
if (count == 1) || (count == 0) {
result = strconv.Itoa(count) + " " + singular + " "
} else {
result = strconv.Itoa(count) + " " + singular + "s "
}
return
}
func (solver *Solver) secondsToHuman(input int64) (result string) {
years := math.Floor(float64(input) / 60 / 60 / 24 / 7 / 30 / 12)
seconds := input % (60 * 60 * 24 * 7 * 30 * 12)
months := math.Floor(float64(seconds) / 60 / 60 / 24 / 7 / 30)
seconds = input % (60 * 60 * 24 * 7 * 30)
weeks := math.Floor(float64(seconds) / 60 / 60 / 24 / 7)
seconds = input % (60 * 60 * 24 * 7)
days := math.Floor(float64(seconds) / 60 / 60 / 24)
seconds = input % (60 * 60 * 24)
hours := math.Floor(float64(seconds) / 60 / 60)
seconds = input % (60 * 60)
minutes := math.Floor(float64(seconds) / 60)
seconds = input % 60
if years > 0 {
result = solver.plural(int(years), "year") + solver.plural(int(months), "month") + solver.plural(int(weeks), "week") + solver.plural(int(days), "day") + solver.plural(int(hours), "hour") + solver.plural(int(minutes), "minute") + solver.plural(int(seconds), "second")
} else if months > 0 {
result = solver.plural(int(months), "month") + solver.plural(int(weeks), "week") + solver.plural(int(days), "day") + solver.plural(int(hours), "hour") + solver.plural(int(minutes), "minute") + solver.plural(int(seconds), "second")
} else if weeks > 0 {
result = solver.plural(int(weeks), "week") + solver.plural(int(days), "day") + solver.plural(int(hours), "hour") + solver.plural(int(minutes), "minute") + solver.plural(int(seconds), "second")
} else if days > 0 {
result = solver.plural(int(days), "day") + solver.plural(int(hours), "hour") + solver.plural(int(minutes), "minute") + solver.plural(int(seconds), "second")
} else if hours > 0 {
result = solver.plural(int(hours), "hour") + solver.plural(int(minutes), "minute") + solver.plural(int(seconds), "second")
} else if minutes > 0 {
result = solver.plural(int(minutes), "minute") + solver.plural(int(seconds), "second")
} else {
result = solver.plural(int(seconds), "second")
}
return
}