Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix calendar before 1970 on windows #401

Merged
merged 2 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading