fix convert/seconds.go, add test to round

This commit is contained in:
MedzikUser 2021-08-12 22:47:54 +02:00
parent 80a69536a9
commit c87a921cb5
4 changed files with 102 additions and 64 deletions

View File

@ -1,6 +1,11 @@
package common
// TODO:
func Uptime() {
import (
"time"
"github.com/MedzikUser/go-utils/convert"
)
func Uptime(start time.Time) string {
return convert.Seconds(time.Since(start))
}

31
convert/round_test.go Normal file
View File

@ -0,0 +1,31 @@
package convert
import "testing"
func TestRound1(t *testing.T) {
r := Round(0.1)
if r != 0 {
t.Fatal(r)
}
}
func TestRound2(t *testing.T) {
r := Round(0.9)
if r != 0 {
t.Fatal(r)
}
}
func TestRound3(t *testing.T) {
r := Round(25.2)
if r != 25 {
t.Fatal(r)
}
}
func TestRound4(t *testing.T) {
r := Round(46.9998655)
if r != 46 {
t.Fatal(r)
}
}

View File

@ -11,73 +11,49 @@ func Seconds(s time.Duration) string {
minutes := Round(s.Minutes())
seconds := Round(s.Seconds())
hours, days := HoursToDays(hours)
days, months := DaysToMonths(days)
months, years := MonthsToYears(months)
var format string
if years > 0 {
format += strconv.Itoa(years) + " years "
months -= years * 12
}
if months > 0 {
format += strconv.Itoa(months) + " months "
days -= months * 30
}
if days > 0 {
format += strconv.Itoa(days) + " days "
hours -= days * 24
}
if hours > 0 {
format += strconv.Itoa(hours) + " hours "
minutes -= hours * 24
}
if minutes > 0 {
format += strconv.Itoa(minutes) + " minutes "
seconds -= minutes * 60
}
if seconds > 0 {
format += strconv.Itoa(seconds) + " seconds"
}
return format
}
func HoursToDays(hours int) (int, int) {
var days int
var (
days int
format string
)
for hours/24 > 0 {
days++
hours -= 24
}
return hours, days
}
func DaysToMonths(days int) (int, int) {
var months int
for days/30 > 0 {
months++
days -= 30
if days > 0 {
format += strconv.Itoa(days) + "d"
}
return days, months
}
if hours > 0 {
if len(format) >= 1 {
format += " "
}
func MonthsToYears(months int) (int, int) {
var years int
for months/12 > 0 {
years++
months -= 12
format += strconv.Itoa(hours) + "h"
} else if days > 0 {
hours = days * 24
}
return months, years
minutes -= Round(s.Hours()) * 60
if minutes > 0 {
if len(format) >= 1 {
format += " "
}
format += strconv.Itoa(minutes) + "m"
}
seconds -= Round(s.Minutes()) * 60
if seconds > 0 {
if len(format) >= 1 {
format += " "
}
format += strconv.Itoa(seconds) + "s"
}
return format
}

View File

@ -5,9 +5,35 @@ import (
"time"
)
func TestSeconds(t *testing.T) {
d, err := time.ParseDuration("48h")
t.Error("1", err)
func TestSeconds1(t *testing.T) {
d, err := time.ParseDuration("48h1m2s")
if err != nil {
t.Fatal(err)
}
out := Seconds(d)
t.Error("2", out)
if out != "2d 1m 2s" {
t.Fatal(out)
}
}
func TestSeconds2(t *testing.T) {
d, err := time.ParseDuration("48h5h1m2s")
if err != nil {
t.Fatal(err)
}
out := Seconds(d)
if out != "2d 5h 1m 2s" {
t.Fatal(out)
}
}
func TestSeconds3(t *testing.T) {
d, err := time.ParseDuration("48h5h60s")
if err != nil {
t.Fatal(err)
}
out := Seconds(d)
if out != "2d 5h 1m" {
t.Fatal(out)
}
}