@@ -1093,10 +1093,7 @@ def _apply(self, n, other):
1093
1093
n -= 1
1094
1094
elif other .day > self .day_of_month :
1095
1095
other = other .replace (day = self .day_of_month )
1096
- if n == 0 :
1097
- n = 1
1098
- else :
1099
- n += 1
1096
+ n += 1
1100
1097
1101
1098
months = n // 2
1102
1099
day = 31 if n % 2 else self .day_of_month
@@ -1146,15 +1143,10 @@ def _apply(self, n, other):
1146
1143
# if other.day is not day_of_month move to day_of_month and update n
1147
1144
if other .day < self .day_of_month :
1148
1145
other = other .replace (day = self .day_of_month )
1149
- if n == 0 :
1150
- n = - 1
1151
- else :
1152
- n -= 1
1146
+ n -= 1
1153
1147
elif other .day > self .day_of_month :
1154
1148
other = other .replace (day = self .day_of_month )
1155
- if n == 0 :
1156
- n = 1
1157
- elif n < 0 :
1149
+ if n <= 0 :
1158
1150
n += 1
1159
1151
1160
1152
months = n // 2 + n % 2
@@ -1414,29 +1406,17 @@ def isAnchored(self):
1414
1406
1415
1407
@apply_wraps
1416
1408
def apply (self , other ):
1417
- base = other
1418
1409
if self .weekday is None :
1419
1410
return other + self .n * self ._inc
1420
1411
1421
- if self .n > 0 :
1422
- k = self .n
1423
- otherDay = other .weekday ()
1424
- if otherDay != self .weekday :
1425
- other = other + timedelta ((self .weekday - otherDay ) % 7 )
1426
- k = k - 1
1427
- for i in range (k ):
1428
- other = other + self ._inc
1429
- else :
1430
- k = self .n
1431
- otherDay = other .weekday ()
1432
- if otherDay != self .weekday :
1433
- other = other + timedelta ((self .weekday - otherDay ) % 7 )
1434
- for i in range (- k ):
1435
- other = other - self ._inc
1412
+ k = self .n
1413
+ otherDay = other .weekday ()
1414
+ if otherDay != self .weekday :
1415
+ other = other + timedelta ((self .weekday - otherDay ) % 7 )
1416
+ if k > 0 :
1417
+ k -= 1
1436
1418
1437
- other = datetime (other .year , other .month , other .day ,
1438
- base .hour , base .minute , base .second , base .microsecond )
1439
- return other
1419
+ return other + timedelta (weeks = k )
1440
1420
1441
1421
@apply_index_wraps
1442
1422
def apply_index (self , i ):
@@ -1511,18 +1491,11 @@ def apply(self, other):
1511
1491
base = other
1512
1492
offsetOfMonth = self .getOffsetOfMonth (other )
1513
1493
1514
- if offsetOfMonth > other :
1515
- if self .n > 0 :
1516
- months = self .n - 1
1517
- else :
1518
- months = self .n
1519
- elif offsetOfMonth == other :
1520
- months = self .n
1521
- else :
1522
- if self .n > 0 :
1523
- months = self .n
1524
- else :
1525
- months = self .n + 1
1494
+ months = self .n
1495
+ if months > 0 and offsetOfMonth > other :
1496
+ months -= 1
1497
+ elif months <= 0 and offsetOfMonth < other :
1498
+ months += 1
1526
1499
1527
1500
other = self .getOffsetOfMonth (shift_month (other , months , 'start' ))
1528
1501
other = datetime (other .year , other .month , other .day , base .hour ,
@@ -1533,11 +1506,7 @@ def getOffsetOfMonth(self, dt):
1533
1506
w = Week (weekday = self .weekday )
1534
1507
d = datetime (dt .year , dt .month , 1 , tzinfo = dt .tzinfo )
1535
1508
d = w .rollforward (d )
1536
-
1537
- for i in range (self .week ):
1538
- d = w .apply (d )
1539
-
1540
- return d
1509
+ return d + timedelta (weeks = self .week )
1541
1510
1542
1511
def onOffset (self , dt ):
1543
1512
if self .normalize and not _is_normalized (dt ):
@@ -1602,18 +1571,11 @@ def __init__(self, n=1, normalize=False, weekday=None):
1602
1571
def apply (self , other ):
1603
1572
offsetOfMonth = self .getOffsetOfMonth (other )
1604
1573
1605
- if offsetOfMonth > other :
1606
- if self .n > 0 :
1607
- months = self .n - 1
1608
- else :
1609
- months = self .n
1610
- elif offsetOfMonth == other :
1611
- months = self .n
1612
- else :
1613
- if self .n > 0 :
1614
- months = self .n
1615
- else :
1616
- months = self .n + 1
1574
+ months = self .n
1575
+ if months > 0 and offsetOfMonth > other :
1576
+ months -= 1
1577
+ elif months <= 0 and offsetOfMonth < other :
1578
+ months += 1
1617
1579
1618
1580
return self .getOffsetOfMonth (shift_month (other , months , 'start' ))
1619
1581
0 commit comments