Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
a127b24e62 | |||
69d6290376 | |||
c08a739158
|
@@ -1,5 +1,5 @@
|
|||||||
package goext
|
package goext
|
||||||
|
|
||||||
const GoextVersion = "0.0.267"
|
const GoextVersion = "0.0.269"
|
||||||
|
|
||||||
const GoextVersionTimestamp = "2023-09-21T14:15:02+0200"
|
const GoextVersionTimestamp = "2023-09-25T09:18:22+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")
|
||||||
|
|
||||||
|
}
|
@@ -28,6 +28,24 @@ func AssertDeepEqual[T any](t *testing.T, actual T, expected T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
func AssertNotDeepEqual[T any](t *testing.T, actual T, expected T) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
if !reflect.DeepEqual(actual, expected) {
|
if !reflect.DeepEqual(actual, expected) {
|
||||||
|
Reference in New Issue
Block a user