|
| 1 | +from datetime import datetime, timedelta |
| 2 | + |
| 3 | + |
| 4 | +def delivery_date(start, description): |
| 5 | + start_date = datetime.fromisoformat(start) |
| 6 | + |
| 7 | + match description: |
| 8 | + case 'NOW': |
| 9 | + due_date = start_date + timedelta(hours=2) |
| 10 | + case 'ASAP': |
| 11 | + if str(start_date.time()) < "13:00:00": |
| 12 | + due_date = start_date.replace(hour=17, minute=0) |
| 13 | + else: |
| 14 | + due_date = ( |
| 15 | + start_date.replace(hour=12, minute=0) + |
| 16 | + timedelta(days=1) |
| 17 | + ) |
| 18 | + case 'EOW': |
| 19 | + if start_date.isoweekday() < 4: |
| 20 | + due_date = ( |
| 21 | + start_date.replace(hour=17, minute=0) + |
| 22 | + timedelta(days=5 - start_date.isoweekday()) |
| 23 | + ) |
| 24 | + else: |
| 25 | + due_date = ( |
| 26 | + start_date.replace(hour=20, minute=0) + |
| 27 | + timedelta(days=7 - start_date.isoweekday()) |
| 28 | + ) |
| 29 | + case description if description.endswith('M'): |
| 30 | + month = int(description[:-1]) |
| 31 | + target = datetime(start_date.year, month, 1, 8, 0, 0) |
| 32 | + |
| 33 | + if start_date.month >= target.month: |
| 34 | + target = target.replace(year=target.year + 1) |
| 35 | + if target.isoweekday() not in (6,7) and target.day in range(1, 8): |
| 36 | + due_date = target |
| 37 | + else: |
| 38 | + if target.isoweekday() == 6: |
| 39 | + due_date = target + timedelta(days = 2) |
| 40 | + if target.isoweekday() == 7: |
| 41 | + due_date = target + timedelta(days = 1) |
| 42 | + case description if description.startswith('Q'): |
| 43 | + target = int(description[1:]) |
| 44 | + current = ((start_date.month + 2) // 3) |
| 45 | + month = {"Q1":4,"Q2": 7,"Q3": 10,"Q4": 1}[description] |
| 46 | + rollover = 1 if (current > target or target == 4) else 0 |
| 47 | + |
| 48 | + due_date = start_date.replace( |
| 49 | + start_date.year + rollover, month, 1, 8, 0, 0 |
| 50 | + ) - timedelta(days=1) |
| 51 | + |
| 52 | + if due_date.isoweekday() == 6: |
| 53 | + due_date -= timedelta(days=1) |
| 54 | + if due_date.isoweekday() == 7: |
| 55 | + due_date -= timedelta(days=2) |
| 56 | + |
| 57 | + return due_date.isoformat() |
0 commit comments