From 093704e5aa90c37393eafed30d4a41e395b7373a Mon Sep 17 00:00:00 2001 From: Djimbo Date: Mon, 30 Jan 2023 09:13:15 +0300 Subject: [PATCH] -Update --- tgbot/routers/user/user_menu.py | 1 + tgbot/services/api_sqlite.py | 9 ++++----- tgbot/utils/const_functions.py | 26 +++++++++++++++++++++----- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/tgbot/routers/user/user_menu.py b/tgbot/routers/user/user_menu.py index f14f956..4d004c2 100644 --- a/tgbot/routers/user/user_menu.py +++ b/tgbot/routers/user/user_menu.py @@ -6,6 +6,7 @@ from aiogram.types import Message, CallbackQuery from tgbot.keyboards.inline_main import menu_finl from tgbot.keyboards.inline_misc import user_inl from tgbot.keyboards.reply_misc import user_rep +from tgbot.utils.const_functions import convert_date from tgbot.utils.misc.bot_models import FSM, RS router = Router() diff --git a/tgbot/services/api_sqlite.py b/tgbot/services/api_sqlite.py index 8eb1181..edeab13 100644 --- a/tgbot/services/api_sqlite.py +++ b/tgbot/services/api_sqlite.py @@ -45,9 +45,9 @@ def add_userx(user_id, user_login, user_name, user_surname, user_fullname): with sqlite3.connect(PATH_DATABASE) as con: con.row_factory = dict_factory con.execute("INSERT INTO storage_users " - "(user_id, user_login, user_name, user_surname, user_fullname, user_date, user_unix) " - "VALUES (?, ?, ?, ?, ?, ?, ?)", - [user_id, user_login, user_name, user_surname, user_fullname, get_date(), get_unix()]) + "(user_id, user_login, user_name, user_surname, user_fullname, user_unix) " + "VALUES (?, ?, ?, ?, ?, ?)", + [user_id, user_login, user_name, user_surname, user_fullname, get_unix()]) # Получение пользователя @@ -102,7 +102,7 @@ def create_dbx(): con.row_factory = dict_factory # Таблица с хранением пользователей - if len(con.execute("PRAGMA table_info(storage_users)").fetchall()) == 8: + if len(con.execute("PRAGMA table_info(storage_users)").fetchall()) == 7: print("DB was found(1/1)") else: con.execute("CREATE TABLE storage_users(" @@ -112,7 +112,6 @@ def create_dbx(): "user_name TEXT," "user_surname TEXT," "user_fullname TEXT," - "user_date TIMESTAMP," "user_unix INTEGER" ")") print("DB was not found(1/1) | Creating...") diff --git a/tgbot/utils/const_functions.py b/tgbot/utils/const_functions.py index fcdff8e..ff89e47 100644 --- a/tgbot/utils/const_functions.py +++ b/tgbot/utils/const_functions.py @@ -120,18 +120,34 @@ def split_messages(get_list: list, count: int) -> list[list]: # Получение даты -def get_date() -> str: - return datetime.now(pytz.timezone(BOT_TIMEZONE)).strftime("%d.%m.%Y %H:%M:%S") +def get_date(full: bool = True) -> str: + if full: # Полная дата с временем + return datetime.now(pytz.timezone(BOT_TIMEZONE)).strftime("%d.%m.%Y %H:%M:%S") + else: # Только дата без времени + return datetime.now(pytz.timezone(BOT_TIMEZONE)).strftime("%d.%m.%Y") -# Получение юникс даты +# Получение unix времени def get_unix(full: bool = False) -> int: - if full: + if full: # Время в наносекундах return time.time_ns() - else: + else: # Время в секундах return int(time.time()) +# Конвертация unix в дату и наоборот, дату в unix +def convert_date(from_time): + if str(from_time).isdigit(): + to_time = datetime.fromtimestamp(from_time, pytz.timezone(BOT_TIMEZONE)).strftime("%d.%m.%Y %H:%M:%S") + else: + if " " in str(from_time): + to_time = int(datetime.strptime(from_time, "%d.%m.%Y %H:%M:%S").timestamp()) + else: + to_time = int(datetime.strptime(from_time, "%d.%m.%Y").timestamp()) + + return to_time + + # Генерация пароля def gen_password(len_password: int = 16, type_password: str = "default") -> str: # default, number, letter, onechar if type_password == "default":