Compare commits

...

13 Commits

Author SHA1 Message Date
52f7f6e690 v0.0.26 2022-11-30 23:38:42 +01:00
b1e3891256 v0.0.25 2022-11-30 23:35:47 +01:00
bdf5b53c20 v0.0.24 2022-11-30 23:34:16 +01:00
496c4e4f59 v0.0.23 2022-11-30 23:08:50 +01:00
deab986caf v0.0.22 2022-11-30 22:09:54 +01:00
9d9a6f1c6e v0.0.21 2022-11-19 16:58:18 +01:00
06a37f37b7 v0.0.20 2022-11-19 16:26:45 +01:00
b35d6ca0b0 v0.0.19 2022-11-19 13:34:21 +01:00
b643bded8a ArrFirstIndex, ArrLastIndex, ArrMap, ArrSum 2022-11-14 20:42:50 +01:00
93be4f9fdd added more supported GOOS 2022-10-29 15:44:51 +02:00
85b6d17df0 Tests && gomod 2022-10-29 15:34:40 +02:00
c7924cd9ff fix GOOS=windows / GOOS=darwin builds 2022-10-29 15:20:20 +02:00
07712aa08c fix timeext under 32bit compilation 2022-10-29 15:11:08 +02:00
21 changed files with 524 additions and 84 deletions

9
Makefile Normal file
View File

@@ -0,0 +1,9 @@
run:
echo "This is a library - can't be run" && false
test:
go test ./...
version:
_data/version.sh

27
_data/version.sh Executable file
View File

@@ -0,0 +1,27 @@
#!/bin/bash
set -o nounset # disallow usage of unset vars ( set -u )
set -o errexit # Exit immediately if a pipeline returns non-zero. ( set -e )
set -o errtrace # Allow the above trap be inherited by all functions in the script. ( set -E )
set -o pipefail # Return value of a pipeline is the value of the last (rightmost) command to exit with a non-zero status
IFS=$'\n\t' # Set $IFS to only newline and tab.
curr_vers=$(git describe --tags --abbrev=0 | sed 's/v//g')
next_ver=$(echo "$curr_vers" | awk -F. -v OFS=. 'NF==1{print ++$NF}; NF>1{if(length($NF+1)>length($NF))$(NF-1)++; $NF=sprintf("%0*d", length($NF), ($NF+1)%(10^length($NF))); print}')
echo ""
echo "> Current Version: ${curr_vers}"
echo "> Next Version: ${next_ver}"
echo ""
git add --verbose .
git commit -a -m "v${next_ver}"
git tag "v${next_ver}"
git push
git push --tags

35
dataext/merge.go Normal file
View File

@@ -0,0 +1,35 @@
package dataext
import (
"reflect"
)
func ObjectMerge[T1 any, T2 any](base T1, override T2) T1 {
reflBase := reflect.ValueOf(&base).Elem()
reflOvrd := reflect.ValueOf(&override).Elem()
for i := 0; i < reflBase.NumField(); i++ {
fieldBase := reflBase.Field(i)
fieldOvrd := reflOvrd.Field(i)
if fieldBase.Kind() != reflect.Ptr || fieldOvrd.Kind() != reflect.Ptr {
continue
}
kindBase := fieldBase.Type().Elem().Kind()
kindOvrd := fieldOvrd.Type().Elem().Kind()
if kindBase != kindOvrd {
continue
}
if !fieldOvrd.IsNil() {
fieldBase.Set(fieldOvrd.Elem().Addr())
}
}
return base
}

70
dataext/merge_test.go Normal file
View File

@@ -0,0 +1,70 @@
package dataext
import (
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"testing"
)
func TestObjectMerge(t *testing.T) {
type A struct {
Field1 *int
Field2 *string
Field3 *float64
Field4 *bool
OnlyA int64
DiffType int
}
type B struct {
Field1 *int
Field2 *string
Field3 *float64
Field4 *bool
OnlyB int64
DiffType string
}
valueA := A{
Field1: nil,
Field2: langext.Ptr("99"),
Field3: langext.Ptr(12.2),
Field4: nil,
OnlyA: 1,
DiffType: 2,
}
valueB := B{
Field1: langext.Ptr(12),
Field2: nil,
Field3: langext.Ptr(13.2),
Field4: nil,
OnlyB: 1,
DiffType: "X",
}
valueMerge := ObjectMerge(valueA, valueB)
assertPtrEqual(t, "Field1", valueMerge.Field1, valueB.Field1)
assertPtrEqual(t, "Field2", valueMerge.Field2, valueA.Field2)
assertPtrEqual(t, "Field3", valueMerge.Field3, valueB.Field3)
assertPtrEqual(t, "Field4", valueMerge.Field4, nil)
}
func assertPtrEqual[T1 comparable](t *testing.T, ident string, actual *T1, expected *T1) {
if actual == nil && expected == nil {
return
}
if actual != nil && expected != nil {
if *actual != *expected {
t.Errorf("[%s] values differ: Actual: '%v', Expected: '%v'", ident, *actual, *expected)
} else {
return
}
}
if actual == nil && expected != nil {
t.Errorf("[%s] values differ: Actual: nil, Expected: not-nil", ident)
}
if actual != nil && expected == nil {
t.Errorf("[%s] values differ: Actual: not-nil, Expected: nil", ident)
}
}

3
go.mod
View File

@@ -3,7 +3,6 @@ module gogs.mikescher.com/BlackForestBytes/goext
go 1.19
require (
golang.org/x/sys v0.1.0
golang.org/x/term v0.1.0
)
require golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect

43
go.sum
View File

@@ -1,43 +1,4 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
github.com/xdg-go/scram v1.1.1/go.mod h1:RaEWvsqvNKKvBPvcKeFjrG2cJqOkHTiyTpzz23ni57g=
github.com/xdg-go/stringprep v1.0.3/go.mod h1:W3f5j4i+9rC0kuIEJL0ky1VpHXQU3ocBgklLGvcBnW8=
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
go.mongodb.org/mongo-driver v1.10.3 h1:XDQEvmh6z1EUsXuIkXE9TaVeqHw6SwS1uf93jFs0HBA=
go.mongodb.org/mongo-driver v1.10.3/go.mod h1:z4XpeoU6w+9Vht+jAFyLgVrD+jGSQQe0+CBWFHNiHt8=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.1.0 h1:g6Z6vPFA9dYBAF7DWcH6sCcOntplXsDKcliusYijMlw=
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@@ -163,6 +163,25 @@ func ArrLast[T comparable](arr []T, comp func(v T) bool) (T, bool) {
return result, found
}
func ArrFirstIndex[T comparable](arr []T, needle T) int {
for i, v := range arr {
if v == needle {
return i
}
}
return -1
}
func ArrLastIndex[T comparable](arr []T, needle T) int {
result := -1
for i, v := range arr {
if v == needle {
result = i
}
}
return result
}
func AddToSet[T comparable](set []T, add T) []T {
for _, v := range set {
if v == add {
@@ -171,3 +190,19 @@ func AddToSet[T comparable](set []T, add T) []T {
}
return append(set, add)
}
func ArrMap[T1 any, T2 any](arr []T1, conv func(v T1) T2) []T2 {
r := make([]T2, len(arr))
for i, v := range arr {
r[i] = conv(v)
}
return r
}
func ArrSum[T NumberConstraint](arr []T) T {
var r T = 0
for _, v := range arr {
r += v
}
return r
}

View File

@@ -7,3 +7,11 @@ func FormatBool(v bool, strTrue string, strFalse string) string {
return strFalse
}
}
func Conditional[T any](v bool, resTrue T, resFalse T) T {
if v {
return resTrue
} else {
return resFalse
}
}

View File

@@ -29,3 +29,21 @@ func Min[T langext.OrderedConstraint](v1 T, v2 T) T {
return v2
}
}
func Abs[T langext.NumberConstraint](v T) T {
if v < 0 {
return -v
} else {
return v
}
}
func Clamp[T langext.NumberConstraint](v T, min T, max T) T {
if v < min {
return min
} else if v > max {
return max
} else {
return v
}
}

104
syncext/atomic.go Normal file
View File

@@ -0,0 +1,104 @@
package syncext
import (
"context"
"sync/atomic"
"time"
)
type AtomicBool struct {
v int32
waiter chan bool // unbuffered
}
func NewAtomicBool(value bool) *AtomicBool {
if value {
return &AtomicBool{v: 0, waiter: make(chan bool)}
} else {
return &AtomicBool{v: 1, waiter: make(chan bool)}
}
}
func (a *AtomicBool) Get() bool {
return atomic.LoadInt32(&a.v) == 1
}
func (a *AtomicBool) Set(value bool) {
if value {
atomic.StoreInt32(&a.v, 1)
} else {
atomic.StoreInt32(&a.v, 0)
}
select {
case a.waiter <- value:
// message sent
default:
// no receiver on channel
}
}
func (a *AtomicBool) Wait(waitFor bool) {
if a.Get() == waitFor {
return
}
for {
if v, ok := ReadChannelWithTimeout(a.waiter, 128*time.Millisecond); ok {
if v == waitFor {
return
}
} else {
if a.Get() == waitFor {
return
}
}
}
}
func (a *AtomicBool) WaitWithTimeout(timeout time.Duration, waitFor bool) error {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
return a.WaitWithContext(ctx, waitFor)
}
func (a *AtomicBool) WaitWithContext(ctx context.Context, waitFor bool) error {
if err := ctx.Err(); err != nil {
return err
}
if a.Get() == waitFor {
return nil
}
for {
if err := ctx.Err(); err != nil {
return err
}
timeOut := 128 * time.Millisecond
if dl, ok := ctx.Deadline(); ok {
timeOutMax := dl.Sub(time.Now())
if timeOutMax <= 0 {
timeOut = 0
} else if 0 < timeOutMax && timeOutMax < timeOut {
timeOut = timeOutMax
}
}
if v, ok := ReadChannelWithTimeout(a.waiter, timeOut); ok {
if v == waitFor {
return nil
}
} else {
if err := ctx.Err(); err != nil {
return err
}
if a.Get() == waitFor {
return nil
}
}
}
}

14
syncext/channel.go Normal file
View File

@@ -0,0 +1,14 @@
package syncext
import "time"
func ReadChannelWithTimeout[T any](c chan T, timeout time.Duration) (T, bool) {
afterCh := time.After(timeout)
select {
case rv := <-c:
return rv, true
case <-afterCh:
return *new(T), false
}
}

121
syncext/channel_test.go Normal file
View File

@@ -0,0 +1,121 @@
package syncext
import (
"testing"
"time"
)
func TestTimeoutReadBuffered(t *testing.T) {
c := make(chan int, 1)
go func() {
time.Sleep(200 * time.Millisecond)
c <- 112
}()
_, ok := ReadChannelWithTimeout(c, 100*time.Millisecond)
if ok {
t.Error("Read success, but should timeout")
}
}
func TestTimeoutReadBigBuffered(t *testing.T) {
c := make(chan int, 128)
go func() {
time.Sleep(200 * time.Millisecond)
c <- 112
}()
_, ok := ReadChannelWithTimeout(c, 100*time.Millisecond)
if ok {
t.Error("Read success, but should timeout")
}
}
func TestTimeoutReadUnbuffered(t *testing.T) {
c := make(chan int)
go func() {
time.Sleep(200 * time.Millisecond)
c <- 112
}()
_, ok := ReadChannelWithTimeout(c, 100*time.Millisecond)
if ok {
t.Error("Read success, but should timeout")
}
}
func TestNoTimeoutAfterStartReadBuffered(t *testing.T) {
c := make(chan int, 1)
go func() {
time.Sleep(10 * time.Millisecond)
c <- 112
}()
_, ok := ReadChannelWithTimeout(c, 100*time.Millisecond)
if !ok {
t.Error("Read timeout, but should have succeeded")
}
}
func TestNoTimeoutAfterStartReadBigBuffered(t *testing.T) {
c := make(chan int, 128)
go func() {
time.Sleep(10 * time.Millisecond)
c <- 112
}()
_, ok := ReadChannelWithTimeout(c, 100*time.Millisecond)
if !ok {
t.Error("Read timeout, but should have succeeded")
}
}
func TestNoTimeoutAfterStartReadUnbuffered(t *testing.T) {
c := make(chan int)
go func() {
time.Sleep(10 * time.Millisecond)
c <- 112
}()
_, ok := ReadChannelWithTimeout(c, 100*time.Millisecond)
if !ok {
t.Error("Read timeout, but should have succeeded")
}
}
func TestNoTimeoutBeforeStartReadBuffered(t *testing.T) {
c := make(chan int, 1)
c <- 112
_, ok := ReadChannelWithTimeout(c, 10*time.Millisecond)
if !ok {
t.Error("Read timeout, but should have succeeded")
}
}
func TestNoTimeoutBeforeStartReadBigBuffered(t *testing.T) {
c := make(chan int, 128)
c <- 112
_, ok := ReadChannelWithTimeout(c, 10*time.Millisecond)
if !ok {
t.Error("Read timeout, but should have succeeded")
}
}

View File

@@ -0,0 +1,5 @@
package termext
func enableColor() bool {
return true
}

5
termext/osutil_linux.go Normal file
View File

@@ -0,0 +1,5 @@
package termext
func enableColor() bool {
return true
}

5
termext/osutil_netbsd.go Normal file
View File

@@ -0,0 +1,5 @@
package termext
func enableColor() bool {
return true
}

View File

@@ -0,0 +1,5 @@
package termext
func enableColor() bool {
return true
}

View File

@@ -1,5 +1,7 @@
package termext
import "golang.org/x/sys/windows"
func enableColor() bool {
handle, err := windows.GetStdHandle(windows.STD_OUTPUT_HANDLE)
if err != nil {

40
termext/termcolor_test.go Normal file
View File

@@ -0,0 +1,40 @@
package termext
import (
"math/rand"
"testing"
)
func init() {
rand.Seed(0)
}
func TestSupportsColors(t *testing.T) {
SupportsColors() // should not error
}
func TestColor(t *testing.T) {
assertEqual(t, Red("test"), "\033[31mtest\u001B[0m")
assertEqual(t, Green("test"), "\033[32mtest\u001B[0m")
assertEqual(t, Yellow("test"), "\033[33mtest\u001B[0m")
assertEqual(t, Blue("test"), "\033[34mtest\u001B[0m")
assertEqual(t, Purple("test"), "\033[35mtest\u001B[0m")
assertEqual(t, Cyan("test"), "\033[36mtest\u001B[0m")
assertEqual(t, Gray("test"), "\033[37mtest\u001B[0m")
assertEqual(t, White("test"), "\033[97mtest\u001B[0m")
assertEqual(t, CleanString(Red("test")), "test")
assertEqual(t, CleanString(Green("test")), "test")
assertEqual(t, CleanString(Yellow("test")), "test")
assertEqual(t, CleanString(Blue("test")), "test")
assertEqual(t, CleanString(Purple("test")), "test")
assertEqual(t, CleanString(Cyan("test")), "test")
assertEqual(t, CleanString(Gray("test")), "test")
assertEqual(t, CleanString(White("test")), "test")
}
func assertEqual(t *testing.T, actual string, expected string) {
if actual != expected {
t.Errorf("values differ: Actual: '%v', Expected: '%v'", actual, expected)
}
}

View File

@@ -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 {

View File

@@ -84,7 +84,7 @@ func IsDatePartEqual(a time.Time, b time.Time, tz *time.Location) bool {
func WithTimePart(base time.Time, hour, minute, second int) time.Time {
datepart := TimeToDatePart(base, base.Location())
delta := time.Duration(hour*int(time.Hour) + minute*int(time.Minute) + second*int(time.Second))
delta := time.Duration(int64(hour)*int64(time.Hour) + int64(minute)*int64(time.Minute) + int64(second)*int64(time.Second))
return datepart.Add(delta)
}
@@ -94,7 +94,7 @@ func WithTimePart(base time.Time, hour, minute, second int) time.Time {
func CombineDateAndTime(d time.Time, t time.Time) time.Time {
datepart := TimeToDatePart(d, d.Location())
delta := time.Duration(t.Hour()*int(time.Hour) + t.Minute()*int(time.Minute) + t.Second()*int(time.Second) + t.Nanosecond()*int(time.Nanosecond))
delta := time.Duration(int64(t.Hour())*int64(time.Hour) + int64(t.Minute())*int64(time.Minute) + int64(t.Second())*int64(time.Second) + int64(t.Nanosecond())*int64(time.Nanosecond))
return datepart.Add(delta)
}
@@ -108,7 +108,7 @@ func IsSunday(t time.Time, tz *time.Location) bool {
}
func DurationFromTime(hours int, minutes int, seconds int) time.Duration {
return time.Duration(hours*int(time.Hour) + minutes*int(time.Minute) + seconds*int(time.Second))
return time.Duration(int64(hours)*int64(time.Hour) + int64(minutes)*int64(time.Minute) + int64(seconds)*int64(time.Second))
}
func Min(a time.Time, b time.Time) time.Time {