From deab986caf9e72f2f677ceb96172b3ef58ef69fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Wed, 30 Nov 2022 22:09:54 +0100 Subject: [PATCH] v0.0.22 --- timeext/duration.go | 53 +++++++++++++-------------------------------- 1 file changed, 15 insertions(+), 38 deletions(-) diff --git a/timeext/duration.go b/timeext/duration.go index 8c2a32b..cbc2495 100644 --- a/timeext/duration.go +++ b/timeext/duration.go @@ -2,59 +2,36 @@ package timeext import ( "fmt" + "gogs.mikescher.com/BlackForestBytes/goext/langext" "time" ) -func FromSeconds(v int) time.Duration { - return time.Duration(int64(v) * int64(time.Second)) +func FromNanoseconds[T langext.NumberConstraint](v T) time.Duration { + return time.Duration(int64(float64(v) * float64(time.Nanosecond))) } -func FromSecondsInt32(v int32) time.Duration { - return time.Duration(int64(v) * int64(time.Second)) +func FromMicroseconds[T langext.NumberConstraint](v T) time.Duration { + return time.Duration(int64(float64(v) * float64(time.Microsecond))) } -func FromSecondsInt64(v int64) time.Duration { - return time.Duration(v * int64(time.Second)) +func FromMilliseconds[T langext.NumberConstraint](v T) time.Duration { + return time.Duration(int64(float64(v) * float64(time.Millisecond))) } -func FromSecondsFloat32(v float32) time.Duration { - return time.Duration(int64(v * float32(time.Second))) +func FromSeconds[T langext.NumberConstraint](v T) time.Duration { + return time.Duration(int64(float64(v) * float64(time.Second))) } -func FromSecondsFloat64(v float64) time.Duration { - return time.Duration(int64(v * float64(time.Second))) +func FromMinutes[T langext.NumberConstraint](v T) time.Duration { + return time.Duration(int64(float64(v) * float64(time.Minute))) } -func FromSecondsFloat(v float64) time.Duration { - return time.Duration(int64(v * float64(time.Second))) +func FromHours[T langext.NumberConstraint](v T) time.Duration { + return time.Duration(int64(float64(v) * float64(time.Hour))) } -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))) +func FromDays[T langext.NumberConstraint](v T) time.Duration { + return time.Duration(int64(float64(v) * float64(24) * float64(time.Hour))) } func FormatNaturalDurationEnglish(iv time.Duration) string {