Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
a2ba283632 | |||
4a1fb1ae18 | |||
a127b24e62 | |||
69d6290376 | |||
c08a739158
|
|||
5f5f0e44f0 | |||
6e6797eac5 | |||
cd9406900a | |||
6c81f7f6bc |
2
go.mod
2
go.mod
@@ -6,7 +6,7 @@ require (
|
|||||||
github.com/gin-gonic/gin v1.9.1
|
github.com/gin-gonic/gin v1.9.1
|
||||||
github.com/jmoiron/sqlx v1.3.5
|
github.com/jmoiron/sqlx v1.3.5
|
||||||
github.com/rs/xid v1.5.0
|
github.com/rs/xid v1.5.0
|
||||||
github.com/rs/zerolog v1.30.0
|
github.com/rs/zerolog v1.31.0
|
||||||
go.mongodb.org/mongo-driver v1.12.1
|
go.mongodb.org/mongo-driver v1.12.1
|
||||||
golang.org/x/crypto v0.13.0
|
golang.org/x/crypto v0.13.0
|
||||||
golang.org/x/sys v0.12.0
|
golang.org/x/sys v0.12.0
|
||||||
|
2
go.sum
2
go.sum
@@ -105,6 +105,8 @@ github.com/rs/zerolog v1.29.1 h1:cO+d60CHkknCbvzEWxP0S9K6KqyTjrCNUy1LdQLCGPc=
|
|||||||
github.com/rs/zerolog v1.29.1/go.mod h1:Le6ESbR7hc+DP6Lt1THiV8CQSdkkNrd3R0XbEgp3ZBU=
|
github.com/rs/zerolog v1.29.1/go.mod h1:Le6ESbR7hc+DP6Lt1THiV8CQSdkkNrd3R0XbEgp3ZBU=
|
||||||
github.com/rs/zerolog v1.30.0 h1:SymVODrcRsaRaSInD9yQtKbtWqwsfoPcRff/oRXLj4c=
|
github.com/rs/zerolog v1.30.0 h1:SymVODrcRsaRaSInD9yQtKbtWqwsfoPcRff/oRXLj4c=
|
||||||
github.com/rs/zerolog v1.30.0/go.mod h1:/tk+P47gFdPXq4QYjvCmT5/Gsug2nagsFWBWhAiSi1w=
|
github.com/rs/zerolog v1.30.0/go.mod h1:/tk+P47gFdPXq4QYjvCmT5/Gsug2nagsFWBWhAiSi1w=
|
||||||
|
github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A=
|
||||||
|
github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
package goext
|
package goext
|
||||||
|
|
||||||
const GoextVersion = "0.0.265"
|
const GoextVersion = "0.0.270"
|
||||||
|
|
||||||
const GoextVersionTimestamp = "2023-09-18T12:57:27+0200"
|
const GoextVersionTimestamp = "2023-09-25T11:35:03+0200"
|
||||||
|
28
timeext/calendarweek.go
Normal file
28
timeext/calendarweek.go
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package timeext
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
func WeekStart(year, week int) time.Time {
|
||||||
|
|
||||||
|
// https://stackoverflow.com/a/52303730/1761622
|
||||||
|
|
||||||
|
// Start from the middle of the year:
|
||||||
|
t := time.Date(year, 7, 1, 0, 0, 0, 0, time.UTC)
|
||||||
|
|
||||||
|
// Roll back to Monday:
|
||||||
|
if wd := t.Weekday(); wd == time.Sunday {
|
||||||
|
t = t.AddDate(0, 0, -6)
|
||||||
|
} else {
|
||||||
|
t = t.AddDate(0, 0, -int(wd)+1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Difference in weeks:
|
||||||
|
_, w := t.ISOWeek()
|
||||||
|
t = t.AddDate(0, 0, (week-w)*7)
|
||||||
|
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
|
||||||
|
func WeekEnd(year, week int) time.Time {
|
||||||
|
return WeekStart(year, week).AddDate(0, 0, 7).Add(time.Duration(-1))
|
||||||
|
}
|
25
timeext/calendarweek_test.go
Normal file
25
timeext/calendarweek_test.go
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package timeext
|
||||||
|
|
||||||
|
import (
|
||||||
|
"gogs.mikescher.com/BlackForestBytes/goext/tst"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestWeekStart(t *testing.T) {
|
||||||
|
|
||||||
|
tst.AssertEqual(t, WeekStart(2018, 1).Format(time.RFC3339Nano), "2018-01-01T00:00:00Z")
|
||||||
|
tst.AssertEqual(t, WeekStart(2018, 2).Format(time.RFC3339Nano), "2018-01-08T00:00:00Z")
|
||||||
|
tst.AssertEqual(t, WeekStart(2019, 1).Format(time.RFC3339Nano), "2018-12-31T00:00:00Z")
|
||||||
|
tst.AssertEqual(t, WeekStart(2019, 2).Format(time.RFC3339Nano), "2019-01-07T00:00:00Z")
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWeekEnd(t *testing.T) {
|
||||||
|
|
||||||
|
tst.AssertEqual(t, WeekEnd(2018, 1).Format(time.RFC3339Nano), "2018-01-07T23:59:59.999999999Z")
|
||||||
|
tst.AssertEqual(t, WeekEnd(2018, 2).Format(time.RFC3339Nano), "2018-01-14T23:59:59.999999999Z")
|
||||||
|
tst.AssertEqual(t, WeekEnd(2019, 1).Format(time.RFC3339Nano), "2019-01-06T23:59:59.999999999Z")
|
||||||
|
tst.AssertEqual(t, WeekEnd(2019, 2).Format(time.RFC3339Nano), "2019-01-13T23:59:59.999999999Z")
|
||||||
|
|
||||||
|
}
|
@@ -2,23 +2,59 @@ package tst
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"reflect"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func AssertEqual[T comparable](t *testing.T, actual T, expected T) {
|
func AssertEqual[T comparable](t *testing.T, actual T, expected T) {
|
||||||
|
t.Helper()
|
||||||
if actual != expected {
|
if actual != expected {
|
||||||
t.Errorf("values differ: Actual: '%v', Expected: '%v'", actual, expected)
|
t.Errorf("values differ: Actual: '%v', Expected: '%v'", actual, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func AssertNotEqual[T comparable](t *testing.T, actual T, expected T) {
|
func AssertNotEqual[T comparable](t *testing.T, actual T, expected T) {
|
||||||
|
t.Helper()
|
||||||
if actual == expected {
|
if actual == expected {
|
||||||
t.Errorf("values do not differ: Actual: '%v', Expected: '%v'", actual, expected)
|
t.Errorf("values do not differ: Actual: '%v', Expected: '%v'", actual, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func AssertDeepEqual[T any](t *testing.T, actual T, expected T) {
|
||||||
|
t.Helper()
|
||||||
|
if !reflect.DeepEqual(actual, expected) {
|
||||||
|
t.Errorf("values differ: Actual: '%v', Expected: '%v'", actual, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func AssertSetDeepEqual[T any](t *testing.T, actual []T, expected []T) {
|
||||||
|
t.Helper()
|
||||||
|
if len(actual) != len(expected) {
|
||||||
|
t.Errorf("values differ in length: Actual (n=%d): '%v', Expected (n=%d): '%v'", len(actual), actual, len(expected), expected)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, a := range expected {
|
||||||
|
found := false
|
||||||
|
for _, b := range actual {
|
||||||
|
found = found || reflect.DeepEqual(a, b)
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
|
t.Errorf("values differ: Element '%v' not found. Actual: '%v', Expected: '%v'", a, actual, expected)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func AssertNotDeepEqual[T any](t *testing.T, actual T, expected T) {
|
||||||
|
t.Helper()
|
||||||
|
if reflect.DeepEqual(actual, expected) {
|
||||||
|
t.Errorf("values do not differ: Actual: '%v', Expected: '%v'", actual, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func AssertDeRefEqual[T comparable](t *testing.T, actual *T, expected T) {
|
func AssertDeRefEqual[T comparable](t *testing.T, actual *T, expected T) {
|
||||||
|
t.Helper()
|
||||||
if actual == nil {
|
if actual == nil {
|
||||||
t.Errorf("values differ: Actual: NIL, Expected: '%v'", expected)
|
t.Errorf("values differ: Actual: NIL, Expected: '%v'", expected)
|
||||||
}
|
}
|
||||||
@@ -28,6 +64,7 @@ func AssertDeRefEqual[T comparable](t *testing.T, actual *T, expected T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func AssertPtrEqual[T comparable](t *testing.T, actual *T, expected *T) {
|
func AssertPtrEqual[T comparable](t *testing.T, actual *T, expected *T) {
|
||||||
|
t.Helper()
|
||||||
if actual == nil && expected == nil {
|
if actual == nil && expected == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -47,6 +84,7 @@ func AssertPtrEqual[T comparable](t *testing.T, actual *T, expected *T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func AssertHexEqual(t *testing.T, expected string, actual []byte) {
|
func AssertHexEqual(t *testing.T, expected string, actual []byte) {
|
||||||
|
t.Helper()
|
||||||
actualStr := hex.EncodeToString(actual)
|
actualStr := hex.EncodeToString(actual)
|
||||||
if actualStr != expected {
|
if actualStr != expected {
|
||||||
t.Errorf("values differ: Actual: '%v', Expected: '%v'", actualStr, expected)
|
t.Errorf("values differ: Actual: '%v', Expected: '%v'", actualStr, expected)
|
||||||
@@ -54,18 +92,21 @@ func AssertHexEqual(t *testing.T, expected string, actual []byte) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func AssertTrue(t *testing.T, value bool) {
|
func AssertTrue(t *testing.T, value bool) {
|
||||||
|
t.Helper()
|
||||||
if !value {
|
if !value {
|
||||||
t.Error("value should be true\n" + string(debug.Stack()))
|
t.Error("value should be true\n" + string(debug.Stack()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func AssertFalse(t *testing.T, value bool) {
|
func AssertFalse(t *testing.T, value bool) {
|
||||||
|
t.Helper()
|
||||||
if value {
|
if value {
|
||||||
t.Error("value should be false\n" + string(debug.Stack()))
|
t.Error("value should be false\n" + string(debug.Stack()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func AssertNoErr(t *testing.T, anerr error) {
|
func AssertNoErr(t *testing.T, anerr error) {
|
||||||
|
t.Helper()
|
||||||
if anerr != nil {
|
if anerr != nil {
|
||||||
t.Error("Function returned an error: " + anerr.Error() + "\n" + string(debug.Stack()))
|
t.Error("Function returned an error: " + anerr.Error() + "\n" + string(debug.Stack()))
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user