Skip to content

Commit 213a3ab

Browse files
committed
chore: Optimize StartOfWeek and EndOfWeek methods
1 parent 8b7a59d commit 213a3ab

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

boundary.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,11 @@ func (c *Carbon) StartOfWeek() *Carbon {
106106
if c.IsInvalid() {
107107
return c
108108
}
109-
dayOfWeek, weekStartsAt := c.DayOfWeek(), int(c.weekStartsAt)
110-
return c.SubDays((DaysPerWeek + dayOfWeek - weekStartsAt) % DaysPerWeek).StartOfDay()
109+
dayOfWeek, weekStartsAt := c.StdTime().Weekday(), c.WeekStartsAt()
110+
if dayOfWeek == weekStartsAt {
111+
return c.StartOfDay()
112+
}
113+
return c.SubDays(int(DaysPerWeek+dayOfWeek-weekStartsAt) % DaysPerWeek).StartOfDay()
111114
}
112115

113116
// EndOfWeek returns a Carbon instance for end of the week.
@@ -116,8 +119,11 @@ func (c *Carbon) EndOfWeek() *Carbon {
116119
if c.IsInvalid() {
117120
return c
118121
}
119-
dayOfWeek, weekEndsAt := c.DayOfWeek(), int(c.weekStartsAt)+DaysPerWeek-1
120-
return c.AddDays((DaysPerWeek - dayOfWeek + weekEndsAt) % DaysPerWeek).EndOfDay()
122+
dayOfWeek, weekEndsAt := c.StdTime().Weekday(), c.WeekEndsAt()
123+
if dayOfWeek == weekEndsAt {
124+
return c.EndOfDay()
125+
}
126+
return c.AddDays(int(DaysPerWeek-dayOfWeek+weekEndsAt) % DaysPerWeek).EndOfDay()
121127
}
122128

123129
// StartOfDay returns a Carbon instance for start of the day.

boundary_example_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,10 @@ func ExampleCarbon_StartOfWeek() {
143143
fmt.Println(carbon.Parse("2020-12-31 23:59:59").StartOfWeek().ToString())
144144

145145
// Output:
146-
// 0000-12-31 00:00:00 +0000 UTC
147-
// 2019-12-29 00:00:00 +0000 UTC
148-
// 2020-08-09 00:00:00 +0000 UTC
149-
// 2020-12-27 00:00:00 +0000 UTC
146+
// 0001-01-01 00:00:00 +0000 UTC
147+
// 2019-12-30 00:00:00 +0000 UTC
148+
// 2020-08-10 00:00:00 +0000 UTC
149+
// 2020-12-28 00:00:00 +0000 UTC
150150
}
151151

152152
func ExampleCarbon_EndOfWeek() {
@@ -156,10 +156,10 @@ func ExampleCarbon_EndOfWeek() {
156156
fmt.Println(carbon.Parse("2020-12-31 23:59:59").EndOfWeek().ToString())
157157

158158
// Output:
159-
// 0001-01-06 23:59:59.999999999 +0000 UTC
160-
// 2020-01-04 23:59:59.999999999 +0000 UTC
161-
// 2020-08-15 23:59:59.999999999 +0000 UTC
162-
// 2021-01-02 23:59:59.999999999 +0000 UTC
159+
// 0001-01-07 23:59:59.999999999 +0000 UTC
160+
// 2020-01-05 23:59:59.999999999 +0000 UTC
161+
// 2020-08-16 23:59:59.999999999 +0000 UTC
162+
// 2021-01-03 23:59:59.999999999 +0000 UTC
163163
}
164164

165165
func ExampleCarbon_StartOfDay() {

boundary_unit_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ func TestCarbon_StartOfWeek(t *testing.T) {
201201
assert.Equal(t, "2019-12-30 00:00:00 +0000 UTC", Parse("2020-01-01 00:00:00").StartOfWeek().ToString())
202202
assert.Equal(t, "2020-08-10 00:00:00 +0000 UTC", Parse("2020-08-15 12:30:30").StartOfWeek().ToString())
203203
assert.Equal(t, "2020-12-28 00:00:00 +0000 UTC", Parse("2020-12-31 23:59:59").StartOfWeek().ToString())
204+
assert.Equal(t, "2025-04-07 00:00:00 +0000 UTC", Parse("2025-04-07 00:00:00").StartOfWeek().ToString())
204205
})
205206
}
206207

@@ -219,6 +220,7 @@ func TestCarbon_EndOfWeek(t *testing.T) {
219220
assert.Equal(t, "2020-01-05 23:59:59.999999999 +0000 UTC", Parse("2020-01-01 00:00:00").EndOfWeek().ToString())
220221
assert.Equal(t, "2020-08-16 23:59:59.999999999 +0000 UTC", Parse("2020-08-15 12:30:30").EndOfWeek().ToString())
221222
assert.Equal(t, "2021-01-03 23:59:59.999999999 +0000 UTC", Parse("2020-12-31 23:59:59").EndOfWeek().ToString())
223+
assert.Equal(t, "2025-04-13 23:59:59.999999999 +0000 UTC", Parse("2025-04-13 00:00:00").EndOfWeek().ToString())
222224
})
223225
}
224226

0 commit comments

Comments
 (0)