Skip to content

Commit 524072b

Browse files
committed
Fix weeks property for negative Period instances
1 parent 807f0e1 commit 524072b

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Change Log
22

3+
## [Unreleased]
4+
5+
### Fixed
6+
7+
- Fixed the `weeks` property for negative `Period` instances.
8+
9+
310
## [2.0.1] - 2018-05-10
411

512
### Fixed

pendulum/period.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def months(self):
144144

145145
@property
146146
def weeks(self):
147-
return self._delta.days // 7
147+
return abs(self._delta.days) // 7 * self._sign(self._delta.days)
148148

149149
@property
150150
def days(self):

tests/period/test_add_subtract.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,25 @@ def test_dst_subtract():
2828
new_start = end - period
2929

3030
assert new_start == start
31+
32+
33+
def test_naive_subtract():
34+
start = pendulum.naive(2013, 3, 31, 1, 30)
35+
end = start.add(hours=1)
36+
period = end - start
37+
new_end = start + period
38+
39+
assert new_end == end
40+
41+
42+
def test_negative_difference_subtract():
43+
start = pendulum.datetime(2018, 5, 28, 12, 34, 56, 123456)
44+
end = pendulum.datetime(2018, 1, 1)
45+
46+
print((start - end).in_words())
47+
48+
period = end - start
49+
print(period.in_words())
50+
new_end = start + period
51+
52+
assert new_end == end

0 commit comments

Comments
 (0)