-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon_utils.py
41 lines (32 loc) · 1.25 KB
/
common_utils.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
import pytz
import argparse
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta # To handle months
def calculate_expiry(expires_after: int, unit: str) -> str:
now = datetime.now(pytz.utc) # Get the current time in UTC
if unit == 'day':
expiry_date = now + timedelta(days=float(expires_after))
elif unit == 'month':
expiry_date = now + relativedelta(months=int(expires_after))
elif unit == 'hour':
expiry_date = now + timedelta(hours=float(expires_after))
else:
raise ValueError("Invalid unit. Use 'hour', 'day', or 'month'.")
return expiry_date.strftime('%Y-%m-%dT%H:%M:%SZ')
def main():
"""
:return:
"""
# return_date_time()
parser = argparse.ArgumentParser(description="Calculate expiry date based on input duration.")
parser.add_argument("--expires_after", type=int, required=True, help="Number of units for expiry.")
parser.add_argument("--unit", type=str, choices=['hour', 'day', 'month'], default='day',
help="Unit of time for expiry.")
args = parser.parse_args()
try:
expiry_date = calculate_expiry(args.expires_after, args.unit)
print(f"Expiry date: {expiry_date}")
except ValueError as e:
print(f"Error: {e}")
if __name__ == "__main__":
main()