Compare commits
22 Commits
Author | SHA1 | Date | |
---|---|---|---|
6a304b875a
|
|||
d12bf23b46
|
|||
52f7f6e690
|
|||
b1e3891256
|
|||
bdf5b53c20
|
|||
496c4e4f59
|
|||
deab986caf
|
|||
9d9a6f1c6e
|
|||
06a37f37b7
|
|||
b35d6ca0b0
|
|||
b643bded8a
|
|||
93be4f9fdd
|
|||
85b6d17df0
|
|||
c7924cd9ff
|
|||
07712aa08c
|
|||
f5243503db
|
|||
21ae9c70d2
|
|||
c223e2f0fa
|
|||
a694f36f46
|
|||
e61682b24c
|
|||
5dc9e98f6b
|
|||
4dd1c08e77
|
9
Makefile
Normal file
9
Makefile
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
|
||||||
|
run:
|
||||||
|
echo "This is a library - can't be run" && false
|
||||||
|
|
||||||
|
test:
|
||||||
|
go test ./...
|
||||||
|
|
||||||
|
version:
|
||||||
|
_data/version.sh
|
@@ -3,6 +3,6 @@ BFB goext library
|
|||||||
|
|
||||||
A collection of general & useful library methods
|
A collection of general & useful library methods
|
||||||
|
|
||||||
Every subfolder is a seperate dependency and can be imported individually
|
This should not have any heavy dependencies (gin, mongo, etc) and add missing basic language features...
|
||||||
|
|
||||||
Potentially needs `export GOPRIVATE="gogs.mikescher.com"`
|
Potentially needs `export GOPRIVATE="gogs.mikescher.com"`
|
27
_data/version.sh
Executable file
27
_data/version.sh
Executable 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
35
dataext/merge.go
Normal 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
70
dataext/merge_test.go
Normal 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
3
go.mod
@@ -3,7 +3,6 @@ module gogs.mikescher.com/BlackForestBytes/goext
|
|||||||
go 1.19
|
go 1.19
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
golang.org/x/sys v0.1.0
|
||||||
golang.org/x/term v0.1.0
|
golang.org/x/term v0.1.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
|
|
||||||
|
43
go.sum
43
go.sum
@@ -1,43 +1,4 @@
|
|||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
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/term v0.1.0 h1:g6Z6vPFA9dYBAF7DWcH6sCcOntplXsDKcliusYijMlw=
|
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/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=
|
|
||||||
|
@@ -4,6 +4,24 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func BoolCount(arr ...bool) int {
|
||||||
|
c := 0
|
||||||
|
for _, v := range arr {
|
||||||
|
if v {
|
||||||
|
c++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func Range[T IntegerConstraint](start T, end T) []T {
|
||||||
|
r := make([]T, 0, end-start)
|
||||||
|
for i := start; i < end; i++ {
|
||||||
|
r = append(r, i)
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
func ForceArray[T any](v []T) []T {
|
func ForceArray[T any](v []T) []T {
|
||||||
if v == nil {
|
if v == nil {
|
||||||
return make([]T, 0)
|
return make([]T, 0)
|
||||||
@@ -124,6 +142,46 @@ func ArrAnyErr(arr interface{}, fn func(int) (bool, error)) (bool, error) {
|
|||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ArrFirst[T comparable](arr []T, comp func(v T) bool) (T, bool) {
|
||||||
|
for _, v := range arr {
|
||||||
|
if comp(v) {
|
||||||
|
return v, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return *new(T), false
|
||||||
|
}
|
||||||
|
|
||||||
|
func ArrLast[T comparable](arr []T, comp func(v T) bool) (T, bool) {
|
||||||
|
found := false
|
||||||
|
result := *new(T)
|
||||||
|
for _, v := range arr {
|
||||||
|
if comp(v) {
|
||||||
|
found = true
|
||||||
|
result = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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 {
|
func AddToSet[T comparable](set []T, add T) []T {
|
||||||
for _, v := range set {
|
for _, v := range set {
|
||||||
if v == add {
|
if v == add {
|
||||||
@@ -132,3 +190,19 @@ func AddToSet[T comparable](set []T, add T) []T {
|
|||||||
}
|
}
|
||||||
return append(set, add)
|
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
|
||||||
|
}
|
||||||
|
74
langext/base62.go
Normal file
74
langext/base62.go
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
package langext
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"errors"
|
||||||
|
"math"
|
||||||
|
"math/big"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
base62Base = uint64(62)
|
||||||
|
base62CharacterSet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
||||||
|
)
|
||||||
|
|
||||||
|
func RandBase62(rlen int) string {
|
||||||
|
bi52 := big.NewInt(int64(len(base62CharacterSet)))
|
||||||
|
|
||||||
|
randMax := big.NewInt(math.MaxInt64)
|
||||||
|
|
||||||
|
r := ""
|
||||||
|
|
||||||
|
for i := 0; i < rlen; i++ {
|
||||||
|
v, err := rand.Int(rand.Reader, randMax)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
r += string(base62CharacterSet[v.Mod(v, bi52).Int64()])
|
||||||
|
}
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func EncodeBase62(num uint64) string {
|
||||||
|
if num == 0 {
|
||||||
|
return "0"
|
||||||
|
}
|
||||||
|
|
||||||
|
b := make([]byte, 0)
|
||||||
|
|
||||||
|
// loop as long the num is bigger than zero
|
||||||
|
for num > 0 {
|
||||||
|
r := num % base62Base
|
||||||
|
|
||||||
|
num -= r
|
||||||
|
num /= base62Base
|
||||||
|
|
||||||
|
b = append([]byte{base62CharacterSet[int(r)]}, b...)
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func DecodeBase62(str string) (uint64, error) {
|
||||||
|
if str == "" {
|
||||||
|
return 0, errors.New("empty string")
|
||||||
|
}
|
||||||
|
|
||||||
|
result := uint64(0)
|
||||||
|
|
||||||
|
for _, v := range str {
|
||||||
|
result *= base62Base
|
||||||
|
|
||||||
|
pos := strings.IndexRune(base62CharacterSet, v)
|
||||||
|
if pos == -1 {
|
||||||
|
return 0, errors.New("invalid character: " + string(v))
|
||||||
|
}
|
||||||
|
|
||||||
|
result += uint64(pos)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
@@ -7,3 +7,11 @@ func FormatBool(v bool, strTrue string, strFalse string) string {
|
|||||||
return strFalse
|
return strFalse
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Conditional[T any](v bool, resTrue T, resFalse T) T {
|
||||||
|
if v {
|
||||||
|
return resTrue
|
||||||
|
} else {
|
||||||
|
return resFalse
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@@ -30,3 +30,34 @@ func CompareIntArr(arr1 []int, arr2 []int) bool {
|
|||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CompareArr[T OrderedConstraint](arr1 []T, arr2 []T) bool {
|
||||||
|
|
||||||
|
for i := 0; i < len(arr1) || i < len(arr2); i++ {
|
||||||
|
|
||||||
|
if i < len(arr1) && i < len(arr2) {
|
||||||
|
|
||||||
|
if arr1[i] < arr2[i] {
|
||||||
|
return true
|
||||||
|
} else if arr1[i] > arr2[i] {
|
||||||
|
return false
|
||||||
|
} else {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if i < len(arr1) {
|
||||||
|
|
||||||
|
return true
|
||||||
|
|
||||||
|
} else { // if i < len(arr2)
|
||||||
|
|
||||||
|
return false
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
21
langext/coords.go
Normal file
21
langext/coords.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package langext
|
||||||
|
|
||||||
|
import "math"
|
||||||
|
|
||||||
|
func DegToRad(deg float64) float64 {
|
||||||
|
return deg * (math.Pi / 180.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func RadToDeg(rad float64) float64 {
|
||||||
|
return rad / (math.Pi * 180.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GeoDistance(lon1 float64, lat1 float64, lon2 float64, lat2 float64) float64 {
|
||||||
|
var d1 = DegToRad(lat1)
|
||||||
|
var num1 = DegToRad(lon1)
|
||||||
|
var d2 = DegToRad(lat2)
|
||||||
|
var num2 = DegToRad(lon2) - num1
|
||||||
|
var d3 = math.Pow(math.Sin((d2-d1)/2.0), 2.0) + math.Cos(d1)*math.Cos(d2)*math.Pow(math.Sin(num2/2.0), 2.0)
|
||||||
|
|
||||||
|
return 6376500.0 * (2.0 * math.Atan2(math.Sqrt(d3), math.Sqrt(1.0-d3)))
|
||||||
|
}
|
@@ -3,8 +3,6 @@ package langext
|
|||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"io"
|
"io"
|
||||||
"math"
|
|
||||||
"math/big"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func RandBytes(size int) []byte {
|
func RandBytes(size int) []byte {
|
||||||
@@ -15,24 +13,3 @@ func RandBytes(size int) []byte {
|
|||||||
}
|
}
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func RandBase62(rlen int) string {
|
|
||||||
ecs := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
|
||||||
|
|
||||||
bi52 := big.NewInt(int64(len(ecs)))
|
|
||||||
|
|
||||||
randMax := big.NewInt(math.MaxInt64)
|
|
||||||
|
|
||||||
r := ""
|
|
||||||
|
|
||||||
for i := 0; i < rlen; i++ {
|
|
||||||
v, err := rand.Int(rand.Reader, randMax)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
r += string(ecs[v.Mod(v, bi52).Int64()])
|
|
||||||
}
|
|
||||||
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
|
@@ -29,3 +29,21 @@ func Min[T langext.OrderedConstraint](v1 T, v2 T) T {
|
|||||||
return v2
|
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
104
syncext/atomic.go
Normal 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: 1, waiter: make(chan bool)}
|
||||||
|
} else {
|
||||||
|
return &AtomicBool{v: 0, 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
45
syncext/channel.go
Normal file
45
syncext/channel.go
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package syncext
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// https://gobyexample.com/non-blocking-channel-operations
|
||||||
|
// https://gobyexample.com/timeouts
|
||||||
|
// https://groups.google.com/g/golang-nuts/c/Oth9CmJPoqo
|
||||||
|
|
||||||
|
func ReadChannelWithTimeout[T any](c chan T, timeout time.Duration) (T, bool) {
|
||||||
|
select {
|
||||||
|
case msg := <-c:
|
||||||
|
return msg, true
|
||||||
|
case <-time.After(timeout):
|
||||||
|
return *new(T), false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WriteChannelWithTimeout[T any](c chan T, msg T, timeout time.Duration) bool {
|
||||||
|
select {
|
||||||
|
case c <- msg:
|
||||||
|
return true
|
||||||
|
case <-time.After(timeout):
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadNonBlocking[T any](c chan T) (T, bool) {
|
||||||
|
select {
|
||||||
|
case msg := <-c:
|
||||||
|
return msg, true
|
||||||
|
default:
|
||||||
|
return *new(T), false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WriteNonBlocking[T any](c chan T, msg T) bool {
|
||||||
|
select {
|
||||||
|
case c <- msg:
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
121
syncext/channel_test.go
Normal file
121
syncext/channel_test.go
Normal 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")
|
||||||
|
}
|
||||||
|
}
|
@@ -1,5 +1,7 @@
|
|||||||
package termext
|
package termext
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
colorReset = "\033[0m"
|
colorReset = "\033[0m"
|
||||||
colorRed = "\033[31m"
|
colorRed = "\033[31m"
|
||||||
@@ -43,3 +45,17 @@ func Gray(v string) string {
|
|||||||
func White(v string) string {
|
func White(v string) string {
|
||||||
return colorWhite + v + colorReset
|
return colorWhite + v + colorReset
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CleanString(v string) string {
|
||||||
|
v = strings.ReplaceAll(v, colorReset, "")
|
||||||
|
v = strings.ReplaceAll(v, colorRed, "")
|
||||||
|
v = strings.ReplaceAll(v, colorGreen, "")
|
||||||
|
v = strings.ReplaceAll(v, colorYellow, "")
|
||||||
|
v = strings.ReplaceAll(v, colorBlue, "")
|
||||||
|
v = strings.ReplaceAll(v, colorPurple, "")
|
||||||
|
v = strings.ReplaceAll(v, colorCyan, "")
|
||||||
|
v = strings.ReplaceAll(v, colorGray, "")
|
||||||
|
v = strings.ReplaceAll(v, colorWhite, "")
|
||||||
|
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
5
termext/osutil_freebsd.go
Normal file
5
termext/osutil_freebsd.go
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package termext
|
||||||
|
|
||||||
|
func enableColor() bool {
|
||||||
|
return true
|
||||||
|
}
|
5
termext/osutil_linux.go
Normal file
5
termext/osutil_linux.go
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package termext
|
||||||
|
|
||||||
|
func enableColor() bool {
|
||||||
|
return true
|
||||||
|
}
|
5
termext/osutil_netbsd.go
Normal file
5
termext/osutil_netbsd.go
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package termext
|
||||||
|
|
||||||
|
func enableColor() bool {
|
||||||
|
return true
|
||||||
|
}
|
5
termext/osutil_openbsd.go
Normal file
5
termext/osutil_openbsd.go
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package termext
|
||||||
|
|
||||||
|
func enableColor() bool {
|
||||||
|
return true
|
||||||
|
}
|
@@ -1,5 +1,7 @@
|
|||||||
package termext
|
package termext
|
||||||
|
|
||||||
|
import "golang.org/x/sys/windows"
|
||||||
|
|
||||||
func enableColor() bool {
|
func enableColor() bool {
|
||||||
handle, err := windows.GetStdHandle(windows.STD_OUTPUT_HANDLE)
|
handle, err := windows.GetStdHandle(windows.STD_OUTPUT_HANDLE)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
40
termext/termcolor_test.go
Normal file
40
termext/termcolor_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
@@ -2,59 +2,36 @@ package timeext
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func FromSeconds(v int) time.Duration {
|
func FromNanoseconds[T langext.NumberConstraint](v T) time.Duration {
|
||||||
return time.Duration(int64(v) * int64(time.Second))
|
return time.Duration(int64(float64(v) * float64(time.Nanosecond)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func FromSecondsInt32(v int32) time.Duration {
|
func FromMicroseconds[T langext.NumberConstraint](v T) time.Duration {
|
||||||
return time.Duration(int64(v) * int64(time.Second))
|
return time.Duration(int64(float64(v) * float64(time.Microsecond)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func FromSecondsInt64(v int64) time.Duration {
|
func FromMilliseconds[T langext.NumberConstraint](v T) time.Duration {
|
||||||
return time.Duration(v * int64(time.Second))
|
return time.Duration(int64(float64(v) * float64(time.Millisecond)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func FromSecondsFloat32(v float32) time.Duration {
|
func FromSeconds[T langext.NumberConstraint](v T) time.Duration {
|
||||||
return time.Duration(int64(v * float32(time.Second)))
|
return time.Duration(int64(float64(v) * float64(time.Second)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func FromSecondsFloat64(v float64) time.Duration {
|
func FromMinutes[T langext.NumberConstraint](v T) time.Duration {
|
||||||
return time.Duration(int64(v * float64(time.Second)))
|
return time.Duration(int64(float64(v) * float64(time.Minute)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func FromSecondsFloat(v float64) time.Duration {
|
func FromHours[T langext.NumberConstraint](v T) time.Duration {
|
||||||
return time.Duration(int64(v * float64(time.Second)))
|
return time.Duration(int64(float64(v) * float64(time.Hour)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func FromMinutes(v int) time.Duration {
|
func FromDays[T langext.NumberConstraint](v T) time.Duration {
|
||||||
return time.Duration(int64(v) * int64(time.Minute))
|
return time.Duration(int64(float64(v) * float64(24) * float64(time.Hour)))
|
||||||
}
|
|
||||||
|
|
||||||
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 FormatNaturalDurationEnglish(iv time.Duration) string {
|
func FormatNaturalDurationEnglish(iv time.Duration) string {
|
||||||
|
@@ -17,14 +17,14 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TimeToDatePart returns a timestamp at the start of the day which contains t (= 00:00:00)
|
// TimeToDatePart returns a timestamp at the start of the day which contains t (= 00:00:00)
|
||||||
func TimeToDatePart(t time.Time) time.Time {
|
func TimeToDatePart(t time.Time, tz *time.Location) time.Time {
|
||||||
t = t.In(TimezoneBerlin)
|
t = t.In(tz)
|
||||||
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
|
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
|
||||||
}
|
}
|
||||||
|
|
||||||
// TimeToWeekStart returns a timestamp at the start of the week which contains t (= Monday 00:00:00)
|
// TimeToWeekStart returns a timestamp at the start of the week which contains t (= Monday 00:00:00)
|
||||||
func TimeToWeekStart(t time.Time) time.Time {
|
func TimeToWeekStart(t time.Time, tz *time.Location) time.Time {
|
||||||
t = TimeToDatePart(t)
|
t = TimeToDatePart(t, tz)
|
||||||
|
|
||||||
delta := time.Duration(((int64(t.Weekday()) + 6) % 7) * 24 * int64(time.Hour))
|
delta := time.Duration(((int64(t.Weekday()) + 6) % 7) * 24 * int64(time.Hour))
|
||||||
t = t.Add(-1 * delta)
|
t = t.Add(-1 * delta)
|
||||||
@@ -33,32 +33,32 @@ func TimeToWeekStart(t time.Time) time.Time {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TimeToMonthStart returns a timestamp at the start of the month which contains t (= yyyy-MM-00 00:00:00)
|
// TimeToMonthStart returns a timestamp at the start of the month which contains t (= yyyy-MM-00 00:00:00)
|
||||||
func TimeToMonthStart(t time.Time) time.Time {
|
func TimeToMonthStart(t time.Time, tz *time.Location) time.Time {
|
||||||
t = t.In(TimezoneBerlin)
|
t = t.In(tz)
|
||||||
return time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location())
|
return time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location())
|
||||||
}
|
}
|
||||||
|
|
||||||
// TimeToMonthEnd returns a timestamp at the end of the month which contains t (= yyyy-MM-31 23:59:59.999999999)
|
// TimeToMonthEnd returns a timestamp at the end of the month which contains t (= yyyy-MM-31 23:59:59.999999999)
|
||||||
func TimeToMonthEnd(t time.Time) time.Time {
|
func TimeToMonthEnd(t time.Time, tz *time.Location) time.Time {
|
||||||
return TimeToMonthStart(t).AddDate(0, 1, 0).Add(-1)
|
return TimeToMonthStart(t, tz).AddDate(0, 1, 0).Add(-1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TimeToYearStart returns a timestamp at the start of the year which contains t (= yyyy-01-01 00:00:00)
|
// TimeToYearStart returns a timestamp at the start of the year which contains t (= yyyy-01-01 00:00:00)
|
||||||
func TimeToYearStart(t time.Time) time.Time {
|
func TimeToYearStart(t time.Time, tz *time.Location) time.Time {
|
||||||
t = t.In(TimezoneBerlin)
|
t = t.In(tz)
|
||||||
return time.Date(t.Year(), 1, 1, 0, 0, 0, 0, t.Location())
|
return time.Date(t.Year(), 1, 1, 0, 0, 0, 0, t.Location())
|
||||||
}
|
}
|
||||||
|
|
||||||
// TimeToYearEnd returns a timestamp at the end of the month which contains t (= yyyy-12-31 23:59:59.999999999)
|
// TimeToYearEnd returns a timestamp at the end of the month which contains t (= yyyy-12-31 23:59:59.999999999)
|
||||||
func TimeToYearEnd(t time.Time) time.Time {
|
func TimeToYearEnd(t time.Time, tz *time.Location) time.Time {
|
||||||
return TimeToYearStart(t).AddDate(1, 0, 0).Add(-1)
|
return TimeToYearStart(t, tz).AddDate(1, 0, 0).Add(-1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsSameDayIncludingDateBoundaries returns true if t1 and t2 are part of the same day (TZ/Berlin), the boundaries of the day are
|
// IsSameDayIncludingDateBoundaries returns true if t1 and t2 are part of the same day (TZ/Berlin), the boundaries of the day are
|
||||||
// inclusive, this means 2021-09-15T00:00:00 is still part of the day 2021-09-14
|
// inclusive, this means 2021-09-15T00:00:00 is still part of the day 2021-09-14
|
||||||
func IsSameDayIncludingDateBoundaries(t1 time.Time, t2 time.Time) bool {
|
func IsSameDayIncludingDateBoundaries(t1 time.Time, t2 time.Time, tz *time.Location) bool {
|
||||||
dp1 := TimeToDatePart(t1)
|
dp1 := TimeToDatePart(t1, tz)
|
||||||
dp2 := TimeToDatePart(t2)
|
dp2 := TimeToDatePart(t2, tz)
|
||||||
|
|
||||||
if dp1.Equal(dp2) {
|
if dp1.Equal(dp2) {
|
||||||
return true
|
return true
|
||||||
@@ -72,9 +72,9 @@ func IsSameDayIncludingDateBoundaries(t1 time.Time, t2 time.Time) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// IsDatePartEqual returns true if a and b have the same date part (`yyyy`, `MM` and `dd`)
|
// IsDatePartEqual returns true if a and b have the same date part (`yyyy`, `MM` and `dd`)
|
||||||
func IsDatePartEqual(a time.Time, b time.Time) bool {
|
func IsDatePartEqual(a time.Time, b time.Time, tz *time.Location) bool {
|
||||||
yy1, mm1, dd1 := a.In(TimezoneBerlin).Date()
|
yy1, mm1, dd1 := a.In(tz).Date()
|
||||||
yy2, mm2, dd2 := b.In(TimezoneBerlin).Date()
|
yy2, mm2, dd2 := b.In(tz).Date()
|
||||||
|
|
||||||
return yy1 == yy2 && mm1 == mm2 && dd1 == dd2
|
return yy1 == yy2 && mm1 == mm2 && dd1 == dd2
|
||||||
}
|
}
|
||||||
@@ -82,9 +82,9 @@ func IsDatePartEqual(a time.Time, b time.Time) bool {
|
|||||||
// WithTimePart returns a timestamp with the date-part (`yyyy`, `MM`, `dd`) from base
|
// WithTimePart returns a timestamp with the date-part (`yyyy`, `MM`, `dd`) from base
|
||||||
// and the time (`HH`, `mm`, `ss`) from the parameter
|
// and the time (`HH`, `mm`, `ss`) from the parameter
|
||||||
func WithTimePart(base time.Time, hour, minute, second int) time.Time {
|
func WithTimePart(base time.Time, hour, minute, second int) time.Time {
|
||||||
datepart := TimeToDatePart(base)
|
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)
|
return datepart.Add(delta)
|
||||||
}
|
}
|
||||||
@@ -92,23 +92,23 @@ func WithTimePart(base time.Time, hour, minute, second int) time.Time {
|
|||||||
// CombineDateAndTime returns a timestamp with the date-part (`yyyy`, `MM`, `dd`) from the d parameter
|
// CombineDateAndTime returns a timestamp with the date-part (`yyyy`, `MM`, `dd`) from the d parameter
|
||||||
// and the time (`HH`, `mm`, `ss`) from the t parameter
|
// and the time (`HH`, `mm`, `ss`) from the t parameter
|
||||||
func CombineDateAndTime(d time.Time, t time.Time) time.Time {
|
func CombineDateAndTime(d time.Time, t time.Time) time.Time {
|
||||||
datepart := TimeToDatePart(d)
|
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)
|
return datepart.Add(delta)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsSunday returns true if t is a sunday (in TZ/Berlin)
|
// IsSunday returns true if t is a sunday (in TZ/Berlin)
|
||||||
func IsSunday(t time.Time) bool {
|
func IsSunday(t time.Time, tz *time.Location) bool {
|
||||||
if t.In(TimezoneBerlin).Weekday() == time.Sunday {
|
if t.In(tz).Weekday() == time.Sunday {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func DurationFromTime(hours int, minutes int, seconds int) time.Duration {
|
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 {
|
func Min(a time.Time, b time.Time) time.Time {
|
||||||
@@ -131,3 +131,7 @@ func UnixFloatSeconds(v float64) time.Time {
|
|||||||
sec, dec := math.Modf(v)
|
sec, dec := math.Modf(v)
|
||||||
return time.Unix(int64(sec), int64(dec*(1e9)))
|
return time.Unix(int64(sec), int64(dec*(1e9)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func FloorTime(t time.Time) time.Time {
|
||||||
|
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user