@@ -392,9 +392,8 @@ class MonthBegin(DateOffset, CacheableOffset):
392
392
def apply (self , other ):
393
393
n = self .n
394
394
395
- if other .day > 1 : #then roll forward if n<=0
396
- if n <= 0 :
397
- n = n + 1
395
+ if other .day > 1 and n <= 0 : #then roll forward if n<=0
396
+ n = + 1
398
397
399
398
other = other + lib .Delta (months = n , day = 1 )
400
399
return other
@@ -439,10 +438,9 @@ def apply(self, other):
439
438
wkday , _ = calendar .monthrange (other .year , other .month )
440
439
firstBDay = _get_firstbday (wkday )
441
440
442
- if other .day > firstBDay :
443
- if n <= 0 :
444
- # as if rolled forward already
445
- n = n + 1
441
+ if other .day > firstBDay and n <= 0 :
442
+ # as if rolled forward already
443
+ n += 1
446
444
447
445
other = other + lib .Delta (months = n )
448
446
wkday , _ = calendar .monthrange (other .year , other .month )
@@ -646,12 +644,15 @@ def apply(self, other):
646
644
647
645
if n <= 0 and monthsSince != 0 : # make sure to roll forward so negate
648
646
monthsSince = monthsSince - 3
647
+
648
+ # roll forward if on same month later than first bday
649
649
if n <= 0 and (monthsSince == 0 and other .day > firstBDay ):
650
650
n = n + 1
651
+ # pretend to roll back if on same month but before firstbday
651
652
elif n > 0 and (monthsSince == 0 and other .day < firstBDay ):
652
653
n = n - 1
653
654
654
-
655
+ # get the first bday for result
655
656
other = other + lib .Delta (months = 3 * n - monthsSince )
656
657
wkday , _ = calendar .monthrange (other .year , other .month )
657
658
firstBDay = _get_firstbday (wkday )
@@ -718,12 +719,14 @@ def apply(self, other):
718
719
wkday , days_in_month = calendar .monthrange (other .year , other .month )
719
720
720
721
monthsSince = (other .month - self .startingMonth ) % 3
722
+
721
723
if monthsSince == 3 : # on an offset
722
724
monthsSince = 0
723
725
724
726
if n <= 0 and monthsSince != 0 :
725
727
# make sure you roll forward, so negate
726
728
monthsSince = monthsSince - 3
729
+
727
730
if n < 0 and (monthsSince == 0 and other .day > 1 ):
728
731
# after start, so come back an extra period as if rolled forward
729
732
n = n + 1
@@ -799,21 +802,21 @@ def apply(self, other):
799
802
firstBDay = _get_firstbday (wkday )
800
803
801
804
years = n
802
- if n > 0 :
805
+
806
+
807
+ if n > 0 : # roll back first for positive n
803
808
if (other .month < self .month or
804
809
(other .month == self .month and other .day < firstBDay )):
805
810
years -= 1
806
- elif n <= 0 :
811
+ elif n <= 0 : # roll forward
807
812
if (other .month > self .month or
808
813
(other .month == self .month and other .day > firstBDay )):
809
814
years += 1
810
815
816
+ # set first bday for result
811
817
other = other + lib .Delta (years = years )
812
-
813
818
wkday , days_in_month = calendar .monthrange (other .year , self .month )
814
-
815
819
firstBDay = _get_firstbday (wkday )
816
-
817
820
result = datetime (other .year , self .month , firstBDay )
818
821
return result
819
822
0 commit comments