sbt-idp/cope2n-api/fwd_api/utils/date.py
2023-12-15 12:43:19 +07:00

33 lines
896 B
Python

from datetime import datetime
from enum import Enum
from zoneinfo import ZoneInfo
from fwd import settings
from fwd_api.exception.exceptions import InvalidException
default_zone = settings.TIME_ZONE
class FORMAT(Enum):
DD_MM_YYYY_HHMMSS = "%d/%m/%Y %H:%M:%S"
def to_date(date_str: str, format_date: str) -> datetime:
try:
from_date: datetime = datetime.strptime(date_str, format_date)
from_date = from_date.replace(tzinfo=ZoneInfo(default_zone))
return from_date
except ValueError:
raise InvalidException(excArgs='dateFormat')
def to_str(date: datetime, format: str) -> str:
try:
return date.strftime(format)
except ValueError:
raise InvalidException(excArgs='dateFormat')
def get_date_time_now():
# return datetime.utcnow().replace(tzinfo=ZoneInfo(default_zone))
return datetime.now(tz=ZoneInfo(default_zone))