Skip to content

Commit 5df0eac

Browse files
committed
Improves day_of_week and day_of_year performance.
1 parent 1393d06 commit 5df0eac

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

pendulum/date.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,27 @@ def _setter(self, **kwargs):
123123

124124
@property
125125
def day_of_week(self):
126-
return int(self.format('%w', formatter='classic'))
126+
"""
127+
Returns the day of the week (0-6).
128+
129+
:rtype: int
130+
"""
131+
return self.isoweekday() % 7
127132

128133
@property
129134
def day_of_year(self):
130-
return int(self.format('%j', formatter='classic'))
135+
"""
136+
Returns the day of the year (1-366).
137+
138+
:rtype: int
139+
"""
140+
k = 1 if self.is_leap_year else 2
141+
142+
return (
143+
(275 * self.month) // 9
144+
- k * (self.month + 9) // 12
145+
+ self.day - 30
146+
)
131147

132148
@property
133149
def week_of_year(self):

0 commit comments

Comments
 (0)