Skip to content

Commit

Permalink
Merge pull request #401 from Tishka17/fix/calendar_1970_win
Browse files Browse the repository at this point in the history
fix calendar before 1970 on windows
  • Loading branch information
Tishka17 authored May 12, 2024
2 parents ea98691 + 076b4bc commit e474848
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 16 additions & 4 deletions src/aiogram_dialog/widgets/kbd/calendar_kbd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -20,6 +19,8 @@
)
from .base import Keyboard

EPOCH = date(1970, 1, 1)

CALLBACK_NEXT_MONTH = "+"
CALLBACK_PREV_MONTH = "-"
CALLBACK_NEXT_YEAR = "+Y"
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit e474848

Please sign in to comment.