From fee9fc104d398dd4cae65b8a3d17d7b95e364d7e Mon Sep 17 00:00:00 2001 From: Artem Ukolov Date: Fri, 1 Mar 2024 18:30:48 +0300 Subject: [PATCH 1/2] fix calendar bug https://github.com/Tishka17/aiogram_dialog/issues/376 --- .../widgets/kbd/calendar_kbd.py | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/aiogram_dialog/widgets/kbd/calendar_kbd.py b/src/aiogram_dialog/widgets/kbd/calendar_kbd.py index 01af9aaf..98984d16 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,8 @@ 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), + manager.event, self.managed(manager), manager, date_from_raw(int(data)) ) async def _process_item_callback( From 076b4bcb87102dac896a7b3d384908f2bec7a5cb Mon Sep 17 00:00:00 2001 From: Andrey Tikhonov <17@itishka.org> Date: Mon, 13 May 2024 01:20:23 +0200 Subject: [PATCH 2/2] Fix formatting --- .flake8 | 2 ++ src/aiogram_dialog/widgets/kbd/calendar_kbd.py | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) 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 98984d16..a886f347 100644 --- a/src/aiogram_dialog/widgets/kbd/calendar_kbd.py +++ b/src/aiogram_dialog/widgets/kbd/calendar_kbd.py @@ -866,7 +866,10 @@ async def _handle_click_date( self, data: str, manager: DialogManager, ) -> None: await self.on_click.process_event( - manager.event, self.managed(manager), manager, date_from_raw(int(data)) + manager.event, + self.managed(manager), + manager, + date_from_raw(int(data)), ) async def _process_item_callback(