data-api / services /utils.py
camphong24032002
Convert date to estimated date
e96d69f
raw
history blame
882 Bytes
from datetime import datetime, timedelta
from utils.config import DATE_FORMAT
class Utility:
def __init__(self) -> None: pass
@staticmethod
def ts_to_date(ts: int, format=DATE_FORMAT) -> str:
return datetime.fromtimestamp(ts).strftime(format)
@staticmethod
def date_to_ts(date: str, format=DATE_FORMAT) -> int:
return int(datetime.timestamp(datetime.strptime(date, format)))
@staticmethod
def generate_dates(start_date: datetime.date,
num_days: int,
format=DATE_FORMAT):
lst_date = []
current_date = start_date
for i in range(num_days):
current_date += timedelta(days=1)
while current_date.weekday() >= 5:
current_date += timedelta(days=1)
lst_date.append(current_date.strftime(format))
return lst_date