-
Notifications
You must be signed in to change notification settings - Fork 4
/
support.py
83 lines (53 loc) · 2.07 KB
/
support.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import datetime
from dateutil.relativedelta import relativedelta
from tinydb import TinyDB
from configuration import debugEverythingNeedsRolling
import alert
from configuration import dbName
# how many days before expiration we close the contracts
ccExpDaysOffset = 1
defaultWaitTime = 3600
def validDateFormat(date):
try:
datetime.datetime.strptime(date, '%Y-%m-%d')
return True
except ValueError:
return False
def getNewCcExpirationDate():
now = datetime.datetime.utcnow()
now = now.replace(tzinfo=datetime.timezone.utc)
third = getThirdFridayOfMonth(now)
# if we are within 7 days or past, get the same day next month
if now.day > third.day - 7:
nextMonth = now + relativedelta(months=1)
third = getThirdFridayOfMonth(nextMonth)
return third
def getThirdFridayOfMonth(monthDate):
# the third friday will be the 15 - 21 day, check lowest
third = datetime.date(monthDate.year, monthDate.month, 15)
w = third.weekday()
if w != 4:
# replace the day
third = third.replace(day=(15 + (4 - w) % 7))
return third
def getDeltaDiffNowTomorrow1Am():
now = datetime.datetime.utcnow()
tomorrow = datetime.datetime.combine(now.date(), datetime.time(0, 0)) + datetime.timedelta(days=1, hours=1)
delta = tomorrow - now
return delta
def getDeltaDiffNowNextRollDate1Am():
db = TinyDB(dbName)
soldCalls = db.all()
db.close()
if not soldCalls:
return None
soldCalls = sorted(soldCalls, key=lambda d: d['expiration'])
now = datetime.datetime.utcnow()
if now.strftime('%Y-%m-%d') >= soldCalls[0]['expiration']:
# This call should've been rolled (this should never happen)
return alert.botFailed(None, 'Unrolled cc found in database, manual review required.')
expDate = datetime.datetime.strptime(soldCalls[0]['expiration'], '%Y-%m-%d') - datetime.timedelta(days=ccExpDaysOffset) + datetime.timedelta(hours=1)
delta = expDate - now
if debugEverythingNeedsRolling:
return datetime.timedelta(0)
return delta