84 lines
1.8 KiB
Go
84 lines
1.8 KiB
Go
package util
|
|
|
|
import (
|
|
"strconv"
|
|
)
|
|
|
|
func FormatTime(seconds int64) string {
|
|
listedTime := make([]string, 0, 5)
|
|
if seconds > 365*24*60*60 {
|
|
listedTime = append(listedTime, FormatPlural(seconds/365/24/60/60, "year", "years"))
|
|
seconds %= 365 * 24 * 60 * 60
|
|
}
|
|
if seconds > 24*60*60 {
|
|
listedTime = append(listedTime, FormatPlural(seconds/24/60/60, "day", "days"))
|
|
seconds %= 24 * 60 * 60
|
|
}
|
|
if seconds > 60*60 {
|
|
listedTime = append(listedTime, FormatPlural(seconds/60/60, "hour", "hours"))
|
|
seconds %= 60 * 60
|
|
}
|
|
if seconds > 60 {
|
|
listedTime = append(listedTime, FormatPlural(seconds/60, "minute", "minutes"))
|
|
seconds %= 60
|
|
}
|
|
listedTime = append(listedTime, FormatPlural(seconds, "second", "seconds"))
|
|
return FormatCommaSeparatedList(listedTime)
|
|
}
|
|
|
|
func FormatCommaSeparatedList(list []string) string {
|
|
switch len(list) {
|
|
case 0:
|
|
return ""
|
|
case 1:
|
|
return list[0]
|
|
case 2:
|
|
return list[0] + " and " + list[1]
|
|
}
|
|
result := ""
|
|
for i := 0; i < len(list)-2; i++ {
|
|
result += list[i] + ", "
|
|
}
|
|
result += list[len(list)-2] + ", and "
|
|
result += list[len(list)-1]
|
|
return result
|
|
}
|
|
func FormatPlural(count int64, singular string, plural string) string {
|
|
if count == 0 || count == 1 {
|
|
return strconv.FormatInt(count, 10) + " " + singular
|
|
}
|
|
return strconv.FormatInt(count, 10) + " " + plural
|
|
}
|
|
|
|
func FormatRomanNumeral(number int) string {
|
|
if number < 1 || number > 3999 {
|
|
return strconv.Itoa(number)
|
|
}
|
|
conversions := []struct {
|
|
value int
|
|
digit string
|
|
}{
|
|
{1000, "M"},
|
|
{900, "CM"},
|
|
{500, "D"},
|
|
{400, "CD"},
|
|
{100, "C"},
|
|
{90, "XC"},
|
|
{50, "L"},
|
|
{40, "XL"},
|
|
{10, "X"},
|
|
{9, "IX"},
|
|
{5, "V"},
|
|
{4, "IV"},
|
|
{1, "I"},
|
|
}
|
|
roman := ""
|
|
for _, conversion := range conversions {
|
|
for number >= conversion.value {
|
|
roman += conversion.digit
|
|
number -= conversion.value
|
|
}
|
|
}
|
|
return roman
|
|
}
|