@@ -58,13 +58,27 @@ func (c *Carbon) AddDuration(duration string) *Carbon {
5858 c .Error = err
5959 return c
6060 }
61- c .time = c .StdTime ().Add (td )
62- return c
61+ result := c .Copy ()
62+ result .time = c .StdTime ().Add (td )
63+ return result
6364}
6465
6566// SubDuration subtracts duration.
6667func (c * Carbon ) SubDuration (duration string ) * Carbon {
67- return c .AddDuration ("-" + duration )
68+ if c .IsInvalid () {
69+ return c
70+ }
71+ var (
72+ td Duration
73+ err error
74+ )
75+ if td , err = parseDuration (duration ); err != nil {
76+ c .Error = err
77+ return c
78+ }
79+ result := c .Copy ()
80+ result .time = c .StdTime ().Add (- td )
81+ return result
6882}
6983
7084// AddCenturies adds some centuries.
@@ -152,24 +166,26 @@ func (c *Carbon) AddYears(years int) *Carbon {
152166 if c .IsInvalid () {
153167 return c
154168 }
155- c .time = c .StdTime ().AddDate (years , 0 , 0 )
156- return c
169+ result := c .Copy ()
170+ result .time = c .StdTime ().AddDate (years , 0 , 0 )
171+ return result
157172}
158173
159174// AddYearsNoOverflow adds some years without overflowing month.
160175func (c * Carbon ) AddYearsNoOverflow (years int ) * Carbon {
161176 if c .IsInvalid () {
162177 return c
163178 }
164- nanosecond := c .Nanosecond ()
165- year , month , day , hour , minute , second := c .DateTime ()
179+ result := c .Copy ()
180+ nanosecond := result .Nanosecond ()
181+ year , month , day , hour , minute , second := result .DateTime ()
166182 // get the last day of this month after some years
167183 lastYear , lastMonth , lastDay := time .Date (year + years , time .Month (month + 1 ), 0 , hour , minute , second , nanosecond , c .loc ).Date ()
168184 if day > lastDay {
169185 day = lastDay
170186 }
171- c .time = time .Date (lastYear , lastMonth , day , hour , minute , second , nanosecond , c .loc )
172- return c
187+ result .time = time .Date (lastYear , lastMonth , day , hour , minute , second , nanosecond , c .loc )
188+ return result
173189}
174190
175191// AddYear adds one year.
@@ -232,7 +248,7 @@ func (c *Carbon) SubQuarters(quarters int) *Carbon {
232248
233249// SubQuartersNoOverflow subtracts some quarters without overflowing month.
234250func (c * Carbon ) SubQuartersNoOverflow (quarters int ) * Carbon {
235- return c .AddMonthsNoOverflow (- quarters * MonthsPerQuarter )
251+ return c .AddQuartersNoOverflow (- quarters )
236252}
237253
238254// SubQuarter subtracts one quarter.
@@ -250,24 +266,26 @@ func (c *Carbon) AddMonths(months int) *Carbon {
250266 if c .IsInvalid () {
251267 return c
252268 }
253- c .time = c .StdTime ().AddDate (0 , months , 0 )
254- return c
269+ result := c .Copy ()
270+ result .time = c .StdTime ().AddDate (0 , months , 0 )
271+ return result
255272}
256273
257274// AddMonthsNoOverflow adds some months without overflowing month.
258275func (c * Carbon ) AddMonthsNoOverflow (months int ) * Carbon {
259276 if c .IsInvalid () {
260277 return c
261278 }
262- nanosecond := c .Nanosecond ()
263- year , month , day , hour , minute , second := c .DateTime ()
279+ result := c .Copy ()
280+ nanosecond := result .Nanosecond ()
281+ year , month , day , hour , minute , second := result .DateTime ()
264282 // get the last day of this month after some months
265283 lastYear , lastMonth , lastDay := time .Date (year , time .Month (month + months + 1 ), 0 , hour , minute , second , nanosecond , c .loc ).Date ()
266284 if day > lastDay {
267285 day = lastDay
268286 }
269- c .time = time .Date (lastYear , lastMonth , day , hour , minute , second , nanosecond , c .loc )
270- return c
287+ result .time = time .Date (lastYear , lastMonth , day , hour , minute , second , nanosecond , c .loc )
288+ return result
271289}
272290
273291// AddMonth adds one month.
@@ -325,8 +343,9 @@ func (c *Carbon) AddDays(days int) *Carbon {
325343 if c .IsInvalid () {
326344 return c
327345 }
328- c .time = c .StdTime ().AddDate (0 , 0 , days )
329- return c
346+ result := c .Copy ()
347+ result .time = c .StdTime ().AddDate (0 , 0 , days )
348+ return result
330349}
331350
332351// AddDay adds one day.
@@ -349,8 +368,9 @@ func (c *Carbon) AddHours(hours int) *Carbon {
349368 if c .IsInvalid () {
350369 return c
351370 }
352- c .time = c .StdTime ().Add (Duration (hours ) * time .Hour )
353- return c
371+ result := c .Copy ()
372+ result .time = c .StdTime ().Add (Duration (hours ) * time .Hour )
373+ return result
354374}
355375
356376// AddHour adds one hour.
@@ -373,8 +393,9 @@ func (c *Carbon) AddMinutes(minutes int) *Carbon {
373393 if c .IsInvalid () {
374394 return c
375395 }
376- c .time = c .StdTime ().Add (Duration (minutes ) * time .Minute )
377- return c
396+ result := c .Copy ()
397+ result .time = c .StdTime ().Add (Duration (minutes ) * time .Minute )
398+ return result
378399}
379400
380401// AddMinute adds one minute.
@@ -397,8 +418,9 @@ func (c *Carbon) AddSeconds(seconds int) *Carbon {
397418 if c .IsInvalid () {
398419 return c
399420 }
400- c .time = c .StdTime ().Add (Duration (seconds ) * time .Second )
401- return c
421+ result := c .Copy ()
422+ result .time = c .StdTime ().Add (Duration (seconds ) * time .Second )
423+ return result
402424}
403425
404426// AddSecond adds one second.
@@ -421,8 +443,9 @@ func (c *Carbon) AddMilliseconds(milliseconds int) *Carbon {
421443 if c .IsInvalid () {
422444 return c
423445 }
424- c .time = c .StdTime ().Add (Duration (milliseconds ) * time .Millisecond )
425- return c
446+ result := c .Copy ()
447+ result .time = c .StdTime ().Add (Duration (milliseconds ) * time .Millisecond )
448+ return result
426449}
427450
428451// AddMillisecond adds one millisecond.
@@ -445,8 +468,9 @@ func (c *Carbon) AddMicroseconds(microseconds int) *Carbon {
445468 if c .IsInvalid () {
446469 return c
447470 }
448- c .time = c .StdTime ().Add (Duration (microseconds ) * time .Microsecond )
449- return c
471+ result := c .Copy ()
472+ result .time = c .StdTime ().Add (Duration (microseconds ) * time .Microsecond )
473+ return result
450474}
451475
452476// AddMicrosecond adds one microsecond.
@@ -469,8 +493,9 @@ func (c *Carbon) AddNanoseconds(nanoseconds int) *Carbon {
469493 if c .IsInvalid () {
470494 return c
471495 }
472- c .time = c .StdTime ().Add (Duration (nanoseconds ) * time .Nanosecond )
473- return c
496+ result := c .Copy ()
497+ result .time = c .StdTime ().Add (Duration (nanoseconds ) * time .Nanosecond )
498+ return result
474499}
475500
476501// AddNanosecond adds one nanosecond.
0 commit comments