diff --git a/go.mod b/go.mod index cbfb8a2..db9f011 100644 --- a/go.mod +++ b/go.mod @@ -21,14 +21,14 @@ require ( ) require ( - github.com/bytedance/sonic v1.11.7 // indirect + github.com/bytedance/sonic v1.11.8 // indirect github.com/bytedance/sonic/loader v0.1.1 // indirect github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect github.com/chenzhuoyu/iasm v0.9.1 // indirect github.com/cloudwego/base64x v0.1.4 // indirect github.com/cloudwego/iasm v0.2.0 // indirect github.com/dustin/go-humanize v1.0.1 // indirect - github.com/gabriel-vasile/mimetype v1.4.3 // indirect + github.com/gabriel-vasile/mimetype v1.4.4 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect diff --git a/go.sum b/go.sum index d60fd17..67ef16a 100644 --- a/go.sum +++ b/go.sum @@ -20,6 +20,8 @@ github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= github.com/bytedance/sonic v1.11.7 h1:k/l9p1hZpNIMJSk37wL9ltkcpqLfIho1vYthi4xT2t4= github.com/bytedance/sonic v1.11.7/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= +github.com/bytedance/sonic v1.11.8 h1:Zw/j1KfiS+OYTi9lyB3bb0CFxPJVkM17k1wyDG32LRA= +github.com/bytedance/sonic v1.11.8/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= github.com/bytedance/sonic/loader v0.1.0/go.mod h1:UmRT+IRTGKz/DAkzcEGzyVqQFJ7H9BqwBO3pm9H/+HY= github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM= github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= @@ -51,6 +53,8 @@ github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkp github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= +github.com/gabriel-vasile/mimetype v1.4.4 h1:QjV6pZ7/XZ7ryI2KuyeEDE8wnh7fHP9YnQy+R0LnH8I= +github.com/gabriel-vasile/mimetype v1.4.4/go.mod h1:JwLei5XPtWdGiMFB5Pjle1oEeoSeEuJfJE+TtfvdB/s= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= diff --git a/goextVersion.go b/goextVersion.go index cb5dae9..415aab0 100644 --- a/goextVersion.go +++ b/goextVersion.go @@ -1,5 +1,5 @@ package goext -const GoextVersion = "0.0.462" +const GoextVersion = "0.0.463" -const GoextVersionTimestamp = "2024-05-23T14:37:05+0200" +const GoextVersionTimestamp = "2024-05-29T20:20:01+0200" diff --git a/timeext/time.go b/timeext/time.go index 4af8ac1..517f02a 100644 --- a/timeext/time.go +++ b/timeext/time.go @@ -156,7 +156,7 @@ func SubtractYears(t time.Time, yearCount float64, tz *time.Location) time.Time intCount, floatCount := math.Modf(yearCount) - t.AddDate(-int(intCount), 0, 0) + t = t.AddDate(-int(intCount), 0, 0) t0 := TimeToYearStart(t, tz) t1 := TimeToYearEnd(t, tz) @@ -173,7 +173,7 @@ func AddYears(t time.Time, yearCount float64, tz *time.Location) time.Time { intCount, floatCount := math.Modf(yearCount) - t.AddDate(int(intCount), 0, 0) + t = t.AddDate(int(intCount), 0, 0) t0 := TimeToYearStart(t, tz) t1 := TimeToYearEnd(t, tz) diff --git a/timeext/time_test.go b/timeext/time_test.go new file mode 100644 index 0000000..df45868 --- /dev/null +++ b/timeext/time_test.go @@ -0,0 +1,158 @@ +package timeext + +import ( + "testing" + "time" +) + +func TestTimeToDayStart(t *testing.T) { + tz := TimezoneBerlin + tm := time.Date(2022, 1, 1, 13, 14, 15, 0, tz) + expected := time.Date(2022, 1, 1, 0, 0, 0, 0, tz) + result := TimeToDayStart(tm, tz) + if !result.Equal(expected) { + t.Errorf("Expected %v but got %v", expected, result) + } +} + +func TestTimeToDayEnd(t *testing.T) { + tz := TimezoneBerlin + tm := time.Date(2022, 1, 1, 13, 14, 15, 0, tz) + expected := time.Date(2022, 1, 2, 0, 0, 0, 0, tz).Add(-1) + result := TimeToDayEnd(tm, tz) + if !result.Equal(expected) { + t.Errorf("Expected %v but got %v", expected, result) + } +} + +func TestIsSameDayIncludingDateBoundaries(t *testing.T) { + tz := TimezoneBerlin + t1 := time.Date(2022, 1, 1, 23, 59, 59, 0, tz) + t2 := time.Date(2022, 1, 2, 0, 0, 0, 0, tz) + if !IsSameDayIncludingDateBoundaries(t1, t2, tz) { + t.Errorf("Expected %v and %v to be the same day", t1, t2) + } +} + +func TestIsDatePartEqual(t *testing.T) { + tz := TimezoneBerlin + t1 := time.Date(2022, 1, 1, 23, 59, 59, 0, tz) + t2 := time.Date(2022, 1, 1, 0, 0, 0, 0, tz) + if !IsDatePartEqual(t1, t2, tz) { + t.Errorf("Expected %v and %v to have the same date part", t1, t2) + } +} + +func TestWithTimePart(t *testing.T) { + tz := TimezoneBerlin + base := time.Date(2022, 1, 1, 0, 0, 0, 0, tz) + expected := time.Date(2022, 1, 1, 13, 14, 15, 0, tz) + result := WithTimePart(base, 13, 14, 15) + if !result.Equal(expected) { + t.Errorf("Expected %v but got %v", expected, result) + } +} + +func TestCombineDateAndTime(t *testing.T) { + tz := TimezoneBerlin + d := time.Date(2022, 1, 1, 0, 0, 0, 0, tz) + tm := time.Date(0, 0, 0, 13, 14, 15, 0, tz) + expected := time.Date(2022, 1, 1, 13, 14, 15, 0, tz) + result := CombineDateAndTime(d, tm) + if !result.Equal(expected) { + t.Errorf("Expected %v but got %v", expected, result) + } +} + +func TestIsSunday(t *testing.T) { + tz := TimezoneBerlin + tm := time.Date(2022, 1, 2, 0, 0, 0, 0, tz) // 2nd January 2022 is a Sunday + if !IsSunday(tm, tz) { + t.Errorf("Expected %v to be a Sunday", tm) + } +} + +func TestDurationFromTime(t *testing.T) { + expected := time.Duration(13*time.Hour + 14*time.Minute + 15*time.Second) + result := DurationFromTime(13, 14, 15) + if result != expected { + t.Errorf("Expected %v but got %v", expected, result) + } +} + +func TestMin(t *testing.T) { + t1 := time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC) + t2 := time.Date(2022, 1, 2, 0, 0, 0, 0, time.UTC) + expected := t1 + result := Min(t1, t2) + if !result.Equal(expected) { + t.Errorf("Expected %v but got %v", expected, result) + } +} + +func TestMax(t *testing.T) { + t1 := time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC) + t2 := time.Date(2022, 1, 2, 0, 0, 0, 0, time.UTC) + expected := t2 + result := Max(t1, t2) + if !result.Equal(expected) { + t.Errorf("Expected %v but got %v", expected, result) + } +} + +func TestUnixFloatSeconds(t *testing.T) { + v := 1640995200.0 // 1st January 2022 00:00:00 UTC in Unix timestamp + expected := time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC) + result := UnixFloatSeconds(v) + if !result.Equal(expected) { + t.Errorf("Expected %v but got %v", expected, result) + } +} + +func TestFloorTime(t *testing.T) { + tm := time.Date(2022, 1, 1, 13, 14, 15, 0, time.UTC) + expected := time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC) + result := FloorTime(tm) + if !result.Equal(expected) { + t.Errorf("Expected %v but got %v", expected, result) + } +} + +func TestSubtractYears(t *testing.T) { + tz := TimezoneBerlin + tm := time.Date(2022, 1, 1, 0, 0, 0, 0, tz) + + expected := time.Date(2021, 1, 1, 0, 0, 0, 0, tz) + result := SubtractYears(tm, 1, tz) + if !result.Equal(expected) { + t.Errorf("Expected %v but got %v", expected, result) + } + + expected = time.Date(2020, 1, 1, 0, 0, 0, 0, tz) + result = SubtractYears(tm, 2, tz) + if !result.Equal(expected) { + t.Errorf("Expected %v but got %v", expected, result) + } + + expected = time.Date(2019, 1, 1, 0, 0, 0, 0, tz) + result = SubtractYears(tm, 3, tz) + if !result.Equal(expected) { + t.Errorf("Expected %v but got %v", expected, result) + } + + expected = time.Date(2025, 1, 1, 0, 0, 0, 0, tz) + result = SubtractYears(tm, -3, tz) + if !result.Equal(expected) { + t.Errorf("Expected %v but got %v", expected, result) + } +} + +func TestAddYears(t *testing.T) { + tz := TimezoneBerlin + tm := time.Date(2022, 1, 1, 0, 0, 0, 0, tz) + expected := time.Date(2023, 1, 1, 0, 0, 0, 0, tz) + result := AddYears(tm, 1, tz) + if !result.Equal(expected) { + t.Errorf("Expected %v but got %v", expected, result) + } +}