Skip to content

Commit d829230

Browse files
committed
fix: Fix lose of values of layout, weekStartsAt, weekendDays and lang when create new carbon instance #303
1 parent 3342de5 commit d829230

File tree

5 files changed

+51
-27
lines changed

5 files changed

+51
-27
lines changed

boundary.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ func (c *Carbon) StartOfCentury() *Carbon {
66
if c.IsInvalid() {
77
return c
88
}
9-
return create(c.Year()/YearsPerCentury*YearsPerCentury, 1, 1, 0, 0, 0, 0).SetLocation(c.loc)
9+
return c.create(c.Year()/YearsPerCentury*YearsPerCentury, 1, 1, 0, 0, 0, 0)
1010
}
1111

1212
// EndOfCentury returns a Carbon instance for end of the century.
@@ -15,7 +15,7 @@ func (c *Carbon) EndOfCentury() *Carbon {
1515
if c.IsInvalid() {
1616
return c
1717
}
18-
return create(c.Year()/YearsPerCentury*YearsPerCentury+99, 12, 31, 23, 59, 59, 999999999).SetLocation(c.loc)
18+
return c.create(c.Year()/YearsPerCentury*YearsPerCentury+99, 12, 31, 23, 59, 59, 999999999)
1919
}
2020

2121
// StartOfDecade returns a Carbon instance for start of the decade.
@@ -24,7 +24,7 @@ func (c *Carbon) StartOfDecade() *Carbon {
2424
if c.IsInvalid() {
2525
return c
2626
}
27-
return create(c.Year()/YearsPerDecade*YearsPerDecade, 1, 1, 0, 0, 0, 0).SetLocation(c.loc)
27+
return c.create(c.Year()/YearsPerDecade*YearsPerDecade, 1, 1, 0, 0, 0, 0)
2828
}
2929

3030
// EndOfDecade returns a Carbon instance for end of the decade.
@@ -33,7 +33,7 @@ func (c *Carbon) EndOfDecade() *Carbon {
3333
if c.IsInvalid() {
3434
return c
3535
}
36-
return create(c.Year()/YearsPerDecade*YearsPerDecade+9, 12, 31, 23, 59, 59, 999999999).SetLocation(c.loc)
36+
return c.create(c.Year()/YearsPerDecade*YearsPerDecade+9, 12, 31, 23, 59, 59, 999999999)
3737
}
3838

3939
// StartOfYear returns a Carbon instance for start of the year.
@@ -42,7 +42,7 @@ func (c *Carbon) StartOfYear() *Carbon {
4242
if c.IsInvalid() {
4343
return c
4444
}
45-
return create(c.Year(), 1, 1, 0, 0, 0, 0).SetLocation(c.loc)
45+
return c.create(c.Year(), 1, 1, 0, 0, 0, 0)
4646
}
4747

4848
// EndOfYear returns a Carbon instance for end of the year.
@@ -51,7 +51,7 @@ func (c *Carbon) EndOfYear() *Carbon {
5151
if c.IsInvalid() {
5252
return c
5353
}
54-
return create(c.Year(), 12, 31, 23, 59, 59, 999999999).SetLocation(c.loc)
54+
return c.create(c.Year(), 12, 31, 23, 59, 59, 999999999)
5555
}
5656

5757
// StartOfQuarter returns a Carbon instance for start of the quarter.
@@ -61,7 +61,7 @@ func (c *Carbon) StartOfQuarter() *Carbon {
6161
return c
6262
}
6363
year, quarter, day := c.Year(), c.Quarter(), 1
64-
return create(year, 3*quarter-2, day, 0, 0, 0, 0).SetLocation(c.loc)
64+
return c.create(year, 3*quarter-2, day, 0, 0, 0, 0)
6565
}
6666

6767
// EndOfQuarter returns a Carbon instance for end of the quarter.
@@ -77,7 +77,7 @@ func (c *Carbon) EndOfQuarter() *Carbon {
7777
case 2, 3:
7878
day = 30
7979
}
80-
return create(year, 3*quarter, day, 23, 59, 59, 999999999).SetLocation(c.loc)
80+
return c.create(year, 3*quarter, day, 23, 59, 59, 999999999)
8181
}
8282

8383
// StartOfMonth returns a Carbon instance for start of the month.
@@ -87,7 +87,7 @@ func (c *Carbon) StartOfMonth() *Carbon {
8787
return c
8888
}
8989
year, month, _ := c.Date()
90-
return create(year, month, 1, 0, 0, 0, 0).SetLocation(c.loc)
90+
return c.create(year, month, 1, 0, 0, 0, 0)
9191
}
9292

9393
// EndOfMonth returns a Carbon instance for end of the month.
@@ -97,7 +97,7 @@ func (c *Carbon) EndOfMonth() *Carbon {
9797
return c
9898
}
9999
year, month, _ := c.Date()
100-
return create(year, month+1, 0, 23, 59, 59, 999999999).SetLocation(c.loc)
100+
return c.create(year, month+1, 0, 23, 59, 59, 999999999)
101101
}
102102

103103
// StartOfWeek returns a Carbon instance for start of the week.
@@ -133,7 +133,7 @@ func (c *Carbon) StartOfDay() *Carbon {
133133
return c
134134
}
135135
year, month, day := c.Date()
136-
return create(year, month, day, 0, 0, 0, 0).SetLocation(c.loc)
136+
return c.create(year, month, day, 0, 0, 0, 0)
137137
}
138138

139139
// EndOfDay returns a Carbon instance for end of the day.
@@ -143,7 +143,7 @@ func (c *Carbon) EndOfDay() *Carbon {
143143
return c
144144
}
145145
year, month, day := c.Date()
146-
return create(year, month, day, 23, 59, 59, 999999999).SetLocation(c.loc)
146+
return c.create(year, month, day, 23, 59, 59, 999999999)
147147
}
148148

149149
// StartOfHour returns a Carbon instance for start of the hour.
@@ -153,7 +153,7 @@ func (c *Carbon) StartOfHour() *Carbon {
153153
return c
154154
}
155155
year, month, day := c.Date()
156-
return create(year, month, day, c.Hour(), 0, 0, 0).SetLocation(c.loc)
156+
return c.create(year, month, day, c.Hour(), 0, 0, 0)
157157
}
158158

159159
// EndOfHour returns a Carbon instance for end of the hour.
@@ -163,7 +163,7 @@ func (c *Carbon) EndOfHour() *Carbon {
163163
return c
164164
}
165165
year, month, day := c.Date()
166-
return create(year, month, day, c.Hour(), 59, 59, 999999999).SetLocation(c.loc)
166+
return c.create(year, month, day, c.Hour(), 59, 59, 999999999)
167167
}
168168

169169
// StartOfMinute returns a Carbon instance for start of the minute.
@@ -173,7 +173,7 @@ func (c *Carbon) StartOfMinute() *Carbon {
173173
return c
174174
}
175175
year, month, day, hour, minute, _ := c.DateTime()
176-
return create(year, month, day, hour, minute, 0, 0).SetLocation(c.loc)
176+
return c.create(year, month, day, hour, minute, 0, 0)
177177
}
178178

179179
// EndOfMinute returns a Carbon instance for end of the minute.
@@ -183,7 +183,7 @@ func (c *Carbon) EndOfMinute() *Carbon {
183183
return c
184184
}
185185
year, month, day, hour, minute, _ := c.DateTime()
186-
return create(year, month, day, hour, minute, 59, 999999999).SetLocation(c.loc)
186+
return c.create(year, month, day, hour, minute, 59, 999999999)
187187
}
188188

189189
// StartOfSecond returns a Carbon instance for start of the second.
@@ -193,7 +193,7 @@ func (c *Carbon) StartOfSecond() *Carbon {
193193
return c
194194
}
195195
year, month, day, hour, minute, second := c.DateTime()
196-
return create(year, month, day, hour, minute, second, 0).SetLocation(c.loc)
196+
return c.create(year, month, day, hour, minute, second, 0)
197197
}
198198

199199
// EndOfSecond returns a Carbon instance for end of the second.
@@ -203,5 +203,5 @@ func (c *Carbon) EndOfSecond() *Carbon {
203203
return c
204204
}
205205
year, month, day, hour, minute, second := c.DateTime()
206-
return create(year, month, day, hour, minute, second, 999999999).SetLocation(c.loc)
206+
return c.create(year, month, day, hour, minute, second, 999999999)
207207
}

creator.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,18 @@ func create(year, month, day, hour, minute, second, nanosecond int, timezone ...
190190
}
191191
return NewCarbon(time.Date(year, time.Month(month), day, hour, minute, second, nanosecond, loc))
192192
}
193+
194+
// creates a Carbon instance from a given date, time and nanosecond.
195+
// 从给定的年、月、日、时、分、秒、纳秒创建 Carbon 实例
196+
func (c *Carbon) create(year, month, day, hour, minute, second, nanosecond int) *Carbon {
197+
return &Carbon{
198+
time: time.Date(year, time.Month(month), day, hour, minute, second, nanosecond, c.loc),
199+
weekStartsAt: c.weekStartsAt,
200+
weekendDays: c.weekendDays,
201+
loc: c.loc,
202+
lang: c.lang.Copy(),
203+
currentLayout: c.currentLayout,
204+
isEmpty: c.isEmpty,
205+
Error: c.Error,
206+
}
207+
}

season.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ func (c *Carbon) StartOfSeason() *Carbon {
5656
}
5757
year, month, _ := c.Date()
5858
if month == 1 || month == 2 {
59-
return create(year-1, 12, 1, 0, 0, 0, 0).SetLocation(c.loc)
59+
return c.create(year-1, 12, 1, 0, 0, 0, 0)
6060
}
61-
return create(year, month/3*3, 1, 0, 0, 0, 0).SetLocation(c.loc)
61+
return c.create(year, month/3*3, 1, 0, 0, 0, 0)
6262
}
6363

6464
// EndOfSeason returns a Carbon instance for end of the season.
@@ -69,12 +69,12 @@ func (c *Carbon) EndOfSeason() *Carbon {
6969
}
7070
year, month, _ := c.Date()
7171
if month == 1 || month == 2 {
72-
return create(year, 3, 0, 23, 59, 59, 999999999).SetLocation(c.loc)
72+
return c.create(year, 3, 0, 23, 59, 59, 999999999)
7373
}
7474
if month == 12 {
75-
return create(year+1, 3, 0, 23, 59, 59, 999999999).SetLocation(c.loc)
75+
return c.create(year+1, 3, 0, 23, 59, 59, 999999999)
7676
}
77-
return create(year, month/3*3+3, 0, 23, 59, 59, 999999999).SetLocation(c.loc)
77+
return c.create(year, month/3*3+3, 0, 23, 59, 59, 999999999)
7878
}
7979

8080
// IsSpring reports whether is spring.

traveler.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,11 @@ func (c *Carbon) AddYearsNoOverflow(years int) *Carbon {
185185
nanosecond := c.Nanosecond()
186186
year, month, day, hour, minute, second := c.DateTime()
187187
// 获取N年后本月的最后一天
188-
lastYear, lastMonth, lastDay := create(year+years, month+1, 0, hour, minute, second, nanosecond).SetLocation(c.loc).Date()
188+
lastYear, lastMonth, lastDay := c.create(year+years, month+1, 0, hour, minute, second, nanosecond).Date()
189189
if day > lastDay {
190190
day = lastDay
191191
}
192-
return create(lastYear, lastMonth, day, hour, minute, second, nanosecond).SetLocation(c.loc)
192+
return c.create(lastYear, lastMonth, day, hour, minute, second, nanosecond).SetLocation(c.loc)
193193
}
194194

195195
// AddYear adds one year.
@@ -298,11 +298,11 @@ func (c *Carbon) AddMonthsNoOverflow(months int) *Carbon {
298298
nanosecond := c.Nanosecond()
299299
year, month, day, hour, minute, second := c.DateTime()
300300
// 获取N月后的最后一天
301-
lastYear, lastMonth, lastDay := create(year, month+months+1, 0, hour, minute, second, nanosecond).SetLocation(c.loc).Date()
301+
lastYear, lastMonth, lastDay := c.create(year, month+months+1, 0, hour, minute, second, nanosecond).Date()
302302
if day > lastDay {
303303
day = lastDay
304304
}
305-
return create(lastYear, lastMonth, day, hour, minute, second, nanosecond).SetLocation(c.loc)
305+
return c.create(lastYear, lastMonth, day, hour, minute, second, nanosecond).SetLocation(c.loc)
306306
}
307307

308308
// AddMonth adds one month.

traveler_unit_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,6 +1378,15 @@ func (s *TravelerSuite) TestCarbon_AddMonthsNoOverflow() {
13781378
s.Equal("2020-05-29", Parse("2020-02-29").AddMonthsNoOverflow(3).ToDateString())
13791379
s.Equal("2020-10-31", Parse("2020-08-31").AddMonthsNoOverflow(2).ToDateString())
13801380
})
1381+
1382+
// https://github.com/dromara/carbon/issues/303
1383+
s.Run("issue303", func() {
1384+
c := CreateFromDate(2025, 6, 11, "Asia/Shanghai")
1385+
c = c.SetWeekStartsAt(time.Sunday)
1386+
c = c.SubMonthNoOverflow()
1387+
c = c.StartOfWeek()
1388+
s.Equal("2025-05-11", c.ToDateString())
1389+
})
13811390
}
13821391

13831392
func (s *TravelerSuite) TestCarbon_AddMonth() {

0 commit comments

Comments
 (0)