-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpers.py
65 lines (54 loc) · 2.26 KB
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from datetime import datetime, timedelta
import pytz
from typing import List
from MyLogger import Logger
logging = Logger().logging
def bday(from_date: datetime, n_days: int) -> datetime:
temp_date: datetime = from_date + timedelta(days=1)
comp_date: datetime = datetime(temp_date.year, temp_date.month, temp_date.day)
n_days = n_days - 1
while n_days > 0 or comp_date.weekday() == 5 or comp_date.weekday() == 6:
# print(comp_date, "weekday = " + str(comp_date.weekday()), comp_date.strftime("%A"), n_days)
if comp_date.weekday() == 5 or comp_date.weekday() == 6:
comp_date += timedelta(days=1)
continue
n_days -= 1
comp_date += timedelta(days=1)
return comp_date
def find_bigger_max_processing(receipt: dict) -> int:
listings = []
try:
listings: List[dict] = receipt["Listings"]
except KeyError:
return -1
max_pro_day: int = -1
for listing in listings:
max_pro_day = max(max_pro_day, listing["processing_max"])
return max_pro_day
def find_smaller_min_processing(receipt: dict) -> int:
listings = []
try:
listings: List[dict] = receipt["Listings"]
except KeyError:
return -1
min_pro_day: int = 999
for listing in listings:
min_pro_day = min(min_pro_day, listing["processing_min"])
return min_pro_day
def calculate_max_min_due_date(receipt: dict) -> None:
logging.debug(f"number of transactions: {len(receipt['Transactions'])}")
logging.debug(f"number of listings: {len(receipt['Listings'])}")
# pprint.pprint(receipt['Transactions'])
receipt["is_completed"] = False
paid_date = datetime.fromtimestamp(int(receipt["Transactions"][0]["paid_tsz"]), pytz.timezone('Canada/Eastern'))
max_pro_n_day: int = find_bigger_max_processing(receipt)
max_due_date = bday(paid_date, max_pro_n_day)
min_pro_n_day: int = find_smaller_min_processing(receipt)
min_due_date = bday(paid_date, min_pro_n_day)
receipt["max_due_date"] = max_due_date
receipt["min_due_date"] = min_due_date
logging.debug(f"{receipt['receipt_id']}'s due dates:")
logging.debug(f"\tmax_due_date => is {paid_date} +{max_pro_n_day} business day ({max_due_date})")
logging.debug(f"\tmin_due_date => is {paid_date} +{min_pro_n_day} business day ({min_due_date})")
if __name__ == '__main__':
print(bday(datetime(2021, 5, 7), 7))