mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Fixed Must-die implementation of the datetime serialization
This commit is contained in:
parent
a7e44eeb8e
commit
4705ce0f8e
2 changed files with 17 additions and 2 deletions
|
|
@ -23,10 +23,11 @@ from aiohttp.http import SERVER_SOFTWARE
|
|||
|
||||
from aiogram.__meta__ import __version__
|
||||
from aiogram.methods import TelegramMethod
|
||||
from .base import BaseSession
|
||||
|
||||
from ...exceptions import TelegramNetworkError
|
||||
from ...methods.base import TelegramType
|
||||
from ...types import InputFile
|
||||
from .base import BaseSession
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from ..bot import Bot
|
||||
|
|
|
|||
|
|
@ -1,13 +1,27 @@
|
|||
import sys
|
||||
from datetime import datetime
|
||||
|
||||
from pydantic import PlainSerializer
|
||||
from typing_extensions import Annotated
|
||||
|
||||
if sys.platform == "win32": # pragma: no cover
|
||||
|
||||
def _datetime_serializer(value: datetime) -> int:
|
||||
# https://github.com/aiogram/aiogram/issues/349
|
||||
# https://github.com/aiogram/aiogram/pull/880
|
||||
return round((value - datetime(1970, 1, 1)).total_seconds())
|
||||
|
||||
else: # pragma: no cover
|
||||
|
||||
def _datetime_serializer(value: datetime) -> int:
|
||||
return round(value.timestamp())
|
||||
|
||||
|
||||
# Make datetime compatible with Telegram Bot API (unixtime)
|
||||
DateTime = Annotated[
|
||||
datetime,
|
||||
PlainSerializer(
|
||||
func=lambda dt: int(dt.timestamp()),
|
||||
func=_datetime_serializer,
|
||||
return_type=int,
|
||||
when_used="unless-none",
|
||||
),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue