2022-10-27 16:00:57 +02:00
|
|
|
package timeext
|
|
|
|
|
2022-10-27 16:48:26 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
2022-10-27 16:00:57 +02:00
|
|
|
|
|
|
|
func FromSeconds(v int) time.Duration {
|
|
|
|
return time.Duration(int64(v) * int64(time.Second))
|
|
|
|
}
|
|
|
|
|
|
|
|
func FromSecondsInt32(v int32) time.Duration {
|
|
|
|
return time.Duration(int64(v) * int64(time.Second))
|
|
|
|
}
|
|
|
|
|
|
|
|
func FromSecondsInt64(v int64) time.Duration {
|
|
|
|
return time.Duration(v * int64(time.Second))
|
|
|
|
}
|
|
|
|
|
|
|
|
func FromSecondsFloat32(v float32) time.Duration {
|
|
|
|
return time.Duration(int64(v * float32(time.Second)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func FromSecondsFloat64(v float64) time.Duration {
|
|
|
|
return time.Duration(int64(v * float64(time.Second)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func FromSecondsFloat(v float64) time.Duration {
|
|
|
|
return time.Duration(int64(v * float64(time.Second)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func FromMinutes(v int) time.Duration {
|
|
|
|
return time.Duration(int64(v) * int64(time.Minute))
|
|
|
|
}
|
|
|
|
|
|
|
|
func FromMinutesFloat(v float64) time.Duration {
|
|
|
|
return time.Duration(int64(v * float64(time.Minute)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func FromMinutesFloat64(v float64) time.Duration {
|
|
|
|
return time.Duration(int64(v * float64(time.Minute)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func FromHoursFloat64(v float64) time.Duration {
|
|
|
|
return time.Duration(int64(v * float64(time.Hour)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func FromDays(v int) time.Duration {
|
|
|
|
return time.Duration(int64(v) * int64(24) * int64(time.Hour))
|
|
|
|
}
|
|
|
|
|
|
|
|
func FromMilliseconds(v int) time.Duration {
|
|
|
|
return time.Duration(int64(v) * int64(time.Millisecond))
|
|
|
|
}
|
|
|
|
|
|
|
|
func FromMillisecondsFloat(v float64) time.Duration {
|
|
|
|
return time.Duration(int64(v * float64(time.Millisecond)))
|
|
|
|
}
|
2022-10-27 16:48:26 +02:00
|
|
|
|
|
|
|
func FormatNaturalDurationEnglish(iv time.Duration) string {
|
|
|
|
if sec := int64(iv.Seconds()); sec < 180 {
|
|
|
|
if sec == 1 {
|
|
|
|
return "1 second ago"
|
|
|
|
} else {
|
|
|
|
return fmt.Sprintf("%d seconds ago", sec)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if min := int64(iv.Minutes()); min < 180 {
|
|
|
|
return fmt.Sprintf("%d minutes ago", min)
|
|
|
|
}
|
|
|
|
|
|
|
|
if hours := int64(iv.Hours()); hours < 72 {
|
|
|
|
return fmt.Sprintf("%d hours ago", hours)
|
|
|
|
}
|
|
|
|
|
|
|
|
if days := int64(iv.Hours() / 24.0); days < 21 {
|
|
|
|
return fmt.Sprintf("%d days ago", days)
|
|
|
|
}
|
|
|
|
|
|
|
|
if weeks := int64(iv.Hours() / 24.0 / 7.0); weeks < 12 {
|
|
|
|
return fmt.Sprintf("%d weeks ago", weeks)
|
|
|
|
}
|
|
|
|
|
|
|
|
if months := int64(iv.Hours() / 24.0 / 7.0 / 30); months < 36 {
|
|
|
|
return fmt.Sprintf("%d months ago", months)
|
|
|
|
}
|
|
|
|
|
|
|
|
years := int64(iv.Hours() / 24.0 / 7.0 / 365)
|
|
|
|
return fmt.Sprintf("%d years ago", years)
|
|
|
|
}
|