diff --git a/.flake8 b/.flake8 index ea09b9d6..8a4bef99 100644 --- a/.flake8 +++ b/.flake8 @@ -16,6 +16,8 @@ ignore= A002, # A003 class attribute "id" is shadowing a python builtin A003, + # A005 A module is shadowing a Python builtin module + A005, # D100 Missing docstring in public module D100, # D101 Missing docstring in public class diff --git a/src/aiogram_dialog/widgets/kbd/calendar_kbd.py b/src/aiogram_dialog/widgets/kbd/calendar_kbd.py index 01af9aaf..a886f347 100644 --- a/src/aiogram_dialog/widgets/kbd/calendar_kbd.py +++ b/src/aiogram_dialog/widgets/kbd/calendar_kbd.py @@ -3,7 +3,6 @@ from dataclasses import dataclass from datetime import date, datetime, timedelta, timezone from enum import Enum -from time import mktime from typing import ( Any, Callable, Dict, List, Optional, Protocol, TypedDict, TypeVar, Union, ) @@ -20,6 +19,8 @@ ) from .base import Keyboard +EPOCH = date(1970, 1, 1) + CALLBACK_NEXT_MONTH = "+" CALLBACK_PREV_MONTH = "-" CALLBACK_NEXT_YEAR = "+Y" @@ -63,6 +64,16 @@ class CalendarScope(Enum): YEARS = "YEARS" +def raw_from_date(d: date) -> int: + diff = d - EPOCH + raw_date = int(diff.total_seconds()) + return raw_date + + +def date_from_raw(raw_date: int) -> date: + return EPOCH + timedelta(seconds=raw_date) + + def month_begin(offset: date): return offset.replace(day=1) @@ -203,7 +214,9 @@ async def _render_date_button( text = self.today_text else: text = self.date_text - raw_date = int(mktime(selected_date.timetuple())) + + raw_date = raw_from_date(selected_date) + return InlineKeyboardButton( text=await text.render_text( current_data, manager, @@ -852,12 +865,11 @@ async def _handle_click_year( async def _handle_click_date( self, data: str, manager: DialogManager, ) -> None: - raw_date = int(data) await self.on_click.process_event( manager.event, self.managed(manager), manager, - date.fromtimestamp(raw_date), + date_from_raw(int(data)), ) async def _process_item_callback(