|
1 | 1 | """Define Django models for the taskmanager app.""" |
| 2 | +import calendar |
2 | 3 | import datetime |
3 | 4 | import os |
4 | 5 | import re |
@@ -244,7 +245,7 @@ def get_next_ride(self) -> datetime.datetime: |
244 | 245 | """Get the next ride.""" |
245 | 246 | utc_tz = pytz.timezone('UTC') |
246 | 247 | if self.repetition_period and self.status in [self.STATUS_SPOOLED, self.STATUS_STARTED]: |
247 | | - now = self.last_invocation_datetime |
| 248 | + now = self.last_invocation_datetime or datetime.datetime.now() |
248 | 249 |
|
249 | 250 | if self.repetition_rate in (None, 0): |
250 | 251 | # consider 1 as default repetition_rate |
@@ -278,19 +279,19 @@ def get_next_ride(self) -> datetime.datetime: |
278 | 279 | ) |
279 | 280 | ) |
280 | 281 | else: |
281 | | - offset = datetime.timedelta(months=self.repetition_rate) |
| 282 | + # monthly scheduling requires a different computation |
| 283 | + def add_months(sourcedate, months): |
| 284 | + month = sourcedate.month - 1 + months |
| 285 | + year = sourcedate.year + month // 12 |
| 286 | + month = month % 12 + 1 |
| 287 | + day = min(sourcedate.day, calendar.monthrange(year,month)[1]) |
| 288 | + return datetime.datetime(year, month, day) |
| 289 | + |
282 | 290 | _next = ( |
283 | | - datetime.datetime( |
284 | | - now.year, |
285 | | - now.month, |
286 | | - 0, 0, 0, 0, |
287 | | - tzinfo=utc_tz |
288 | | - ) + offset |
| 291 | + add_months(now, self.repetition_rate) |
289 | 292 | + datetime.timedelta( |
290 | | - days=self.scheduling.day, |
291 | 293 | hours=self.scheduling.hour, |
292 | 294 | minutes=self.scheduling.minute, |
293 | | - seconds=self.scheduling.second |
294 | 295 | ) |
295 | 296 | ) |
296 | 297 |
|
|
0 commit comments