2015-07-18 10:39:12 -05:00
|
|
|
package util
|
|
|
|
|
2017-04-25 02:14:29 -05:00
|
|
|
import (
|
2017-08-09 03:36:41 -05:00
|
|
|
"fmt"
|
|
|
|
"math"
|
2017-04-25 02:14:29 -05:00
|
|
|
"regexp"
|
2017-08-09 03:36:41 -05:00
|
|
|
"time"
|
2017-04-25 02:14:29 -05:00
|
|
|
)
|
|
|
|
|
2019-01-28 15:09:40 -06:00
|
|
|
// StringsFallback2 returns the first of two not empty strings.
|
2015-07-18 10:39:12 -05:00
|
|
|
func StringsFallback2(val1 string, val2 string) string {
|
2016-06-01 23:46:18 -05:00
|
|
|
return stringsFallback(val1, val2)
|
2015-07-18 10:39:12 -05:00
|
|
|
}
|
2015-08-10 06:46:59 -05:00
|
|
|
|
2019-01-28 15:09:40 -06:00
|
|
|
// StringsFallback3 returns the first of three not empty strings.
|
2015-08-10 06:46:59 -05:00
|
|
|
func StringsFallback3(val1 string, val2 string, val3 string) string {
|
2016-06-01 23:46:18 -05:00
|
|
|
return stringsFallback(val1, val2, val3)
|
|
|
|
}
|
|
|
|
|
|
|
|
func stringsFallback(vals ...string) string {
|
|
|
|
for _, v := range vals {
|
2016-06-03 08:06:54 -05:00
|
|
|
if v != "" {
|
|
|
|
return v
|
|
|
|
}
|
2015-08-10 06:46:59 -05:00
|
|
|
}
|
2016-06-01 23:46:18 -05:00
|
|
|
return ""
|
2015-08-10 06:46:59 -05:00
|
|
|
}
|
2017-04-25 02:14:29 -05:00
|
|
|
|
2019-01-28 15:09:40 -06:00
|
|
|
// SplitString splits a string by commas or empty spaces.
|
2017-04-25 02:14:29 -05:00
|
|
|
func SplitString(str string) []string {
|
|
|
|
if len(str) == 0 {
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return regexp.MustCompile("[, ]+").Split(str, -1)
|
|
|
|
}
|
2017-08-09 03:36:41 -05:00
|
|
|
|
2019-01-28 15:09:40 -06:00
|
|
|
// GetAgeString returns a string representing certain time from years to minutes.
|
2017-08-09 03:36:41 -05:00
|
|
|
func GetAgeString(t time.Time) string {
|
|
|
|
if t.IsZero() {
|
|
|
|
return "?"
|
|
|
|
}
|
|
|
|
|
|
|
|
sinceNow := time.Since(t)
|
|
|
|
minutes := sinceNow.Minutes()
|
|
|
|
years := int(math.Floor(minutes / 525600))
|
|
|
|
months := int(math.Floor(minutes / 43800))
|
|
|
|
days := int(math.Floor(minutes / 1440))
|
|
|
|
hours := int(math.Floor(minutes / 60))
|
|
|
|
|
|
|
|
if years > 0 {
|
|
|
|
return fmt.Sprintf("%dy", years)
|
|
|
|
}
|
|
|
|
if months > 0 {
|
|
|
|
return fmt.Sprintf("%dM", months)
|
|
|
|
}
|
|
|
|
if days > 0 {
|
|
|
|
return fmt.Sprintf("%dd", days)
|
|
|
|
}
|
|
|
|
if hours > 0 {
|
|
|
|
return fmt.Sprintf("%dh", hours)
|
|
|
|
}
|
|
|
|
if int(minutes) > 0 {
|
|
|
|
return fmt.Sprintf("%dm", int(minutes))
|
|
|
|
}
|
|
|
|
|
|
|
|
return "< 1m"
|
|
|
|
}
|