From a59f7b55b1b9caf407bfb37870e2a71eba85a5f4 Mon Sep 17 00:00:00 2001 From: dj Date: Thu, 26 Oct 2023 12:51:50 +0300 Subject: [PATCH] Update --- tgbot/utils/const_functions.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/tgbot/utils/const_functions.py b/tgbot/utils/const_functions.py index d660c22..c7e0d4d 100644 --- a/tgbot/utils/const_functions.py +++ b/tgbot/utils/const_functions.py @@ -113,7 +113,9 @@ def get_unix(full: bool = False) -> int: # Конвертация unix в дату и даты в unix -def convert_date(from_time, full: bool = True, second: bool = True) -> Union[str, int]: +def convert_date(from_time, full=True, second=True) -> Union[str, int]: + from tgbot.data.config import BOT_TIMEZONE + if "-" in str(from_time): from_time = from_time.replace("-", ".") @@ -125,6 +127,34 @@ def convert_date(from_time, full: bool = True, second: bool = True) -> Union[str else: to_time = datetime.fromtimestamp(from_time, pytz.timezone(BOT_TIMEZONE)).strftime("%d.%m.%Y") else: + if " " in str(from_time): + cache_time = from_time.split(" ") + + if ":" in cache_time[0]: + cache_date = cache_time[1].split(".") + cache_time = cache_time[0].split(":") + else: + cache_date = cache_time[0].split(".") + cache_time = cache_time[1].split(":") + + if len(cache_date[0]) == 4: + x_year, x_month, x_day = cache_date[0], cache_date[1], cache_date[2] + else: + x_year, x_month, x_day = cache_date[2], cache_date[1], cache_date[0] + + x_hour, x_minute, x_second = cache_time[0], cache_time[2], cache_time[2] + + from_time = f"{x_day}.{x_month}.{x_year} {x_hour}:{x_minute}:{x_second}" + else: + cache_date = from_time.split(".") + + if len(cache_date[0]) == 4: + x_year, x_month, x_day = cache_date[0], cache_date[1], cache_date[2] + else: + x_year, x_month, x_day = cache_date[2], cache_date[1], cache_date[0] + + from_time = f"{x_day}.{x_month}.{x_year}" + if " " in str(from_time): to_time = int(datetime.strptime(from_time, "%d.%m.%Y %H:%M:%S").timestamp()) else: