From 77df8a495c2e5599550e0173c465ae5ef93cea3b Mon Sep 17 00:00:00 2001 From: Renato Duarte Date: Sat, 13 Feb 2021 10:37:29 +0000 Subject: [PATCH] Fix end of the week when the date provider is the end of the week --- carbon.go | 4 ++++ carbon_test.go | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/carbon.go b/carbon.go index 668de51..4d0e98a 100644 --- a/carbon.go +++ b/carbon.go @@ -1602,6 +1602,10 @@ func (c *Carbon) StartOfWeek() *Carbon { // EndOfWeek returns the date of the last day of the week at 23:59:59 func (c *Carbon) EndOfWeek() *Carbon { + if c.Weekday() == c.WeekEndsAt() { + return c.EndOfDay(); + } + return c.Next(c.WeekEndsAt()).EndOfDay() } diff --git a/carbon_test.go b/carbon_test.go index a4bf9c3..350ce5b 100644 --- a/carbon_test.go +++ b/carbon_test.go @@ -2309,6 +2309,13 @@ func TestEndOfWeek(t *testing.T) { expected, _ := Create(2016, time.August, 28, 23, 59, 59, 999999999, "UTC") assert.Equal(t, expected, t2) + + // test end of week when date is last day of weekend + t3, _ := Create(2021, time.January, 17, 13, 0, 0, 0, "UTC") + t4 := t3.EndOfWeek() + + expected2, _ := Create(2021, time.January, 17, 23, 59, 59, 999999999, "UTC") + assert.Equal(t, expected2, t4) } func TestNextWeekday(t *testing.T) {