Skip to content

Commit b697826

Browse files
committed
Replace integer timestamps with datetime objects
1 parent bbad58a commit b697826

48 files changed

Lines changed: 359 additions & 333 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/source/conf.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import os
2020
import sys
21-
from datetime import datetime
2221

2322
sys.path.insert(0, os.path.abspath("../.."))
2423

@@ -38,9 +37,14 @@
3837
"sphinx.ext.autodoc",
3938
"sphinx.ext.napoleon",
4039
"sphinx.ext.autosummary",
40+
"sphinx.ext.intersphinx",
4141
"sphinx_copybutton"
4242
]
4343

44+
intersphinx_mapping = {
45+
"python": ("https://docs.python.org/3", None)
46+
}
47+
4448
master_doc = "index"
4549
source_suffix = ".rst"
4650
autodoc_member_order = "bysource"

pyrogram/methods/chats/ban_chat_member.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19+
from datetime import datetime
1920
from typing import Union
2021

21-
from pyrogram import raw
22+
from pyrogram import raw, utils
2223
from pyrogram import types
2324
from pyrogram.scaffold import Scaffold
2425

@@ -28,7 +29,7 @@ async def ban_chat_member(
2829
self,
2930
chat_id: Union[int, str],
3031
user_id: Union[int, str],
31-
until_date: int = 0
32+
until_date: datetime = datetime.fromtimestamp(0)
3233
) -> Union["types.Message", bool]:
3334
"""Ban a user from a group, a supergroup or a channel.
3435
In the case of supergroups and channels, the user will not be able to return to the group on their own using
@@ -48,10 +49,10 @@ async def ban_chat_member(
4849
Unique identifier (int) or username (str) of the target user.
4950
For a contact that exists in your Telegram address book you can use his phone number (str).
5051
51-
until_date (``int``, *optional*):
52-
Date when the user will be unbanned, unix time.
52+
until_date (:py:obj:`~datetime.datetime`, *optional*):
53+
Date when the user will be unbanned.
5354
If user is banned for more than 366 days or less than 30 seconds from the current time they are
54-
considered to be banned forever. Defaults to 0 (ban forever).
55+
considered to be banned forever. Defaults to epoch (ban forever).
5556
5657
Returns:
5758
:obj:`~pyrogram.types.Message` | ``bool``: On success, a service message will be returned (when applicable),
@@ -60,13 +61,13 @@ async def ban_chat_member(
6061
Example:
6162
.. code-block:: python
6263
63-
from time import time
64+
from datetime import datetime, timedelta
6465
6566
# Ban chat member forever
6667
app.ban_chat_member(chat_id, user_id)
6768
6869
# Ban chat member and automatically unban after 24h
69-
app.ban_chat_member(chat_id, user_id, int(time.time() + 86400))
70+
app.ban_chat_member(chat_id, user_id, datetime.now() + timedelta(days=1))
7071
"""
7172
chat_peer = await self.resolve_peer(chat_id)
7273
user_peer = await self.resolve_peer(user_id)
@@ -77,7 +78,7 @@ async def ban_chat_member(
7778
channel=chat_peer,
7879
participant=user_peer,
7980
banned_rights=raw.types.ChatBannedRights(
80-
until_date=until_date,
81+
until_date=utils.datetime_to_timestamp(until_date),
8182
view_messages=True,
8283
send_messages=True,
8384
send_media=True,

pyrogram/methods/chats/get_dialogs.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

1919
import logging
20+
from datetime import datetime
2021
from typing import List
2122

2223
from pyrogram import raw
@@ -30,7 +31,7 @@
3031
class GetDialogs(Scaffold):
3132
async def get_dialogs(
3233
self,
33-
offset_date: int = 0,
34+
offset_date: datetime = datetime.fromtimestamp(0),
3435
limit: int = 100,
3536
pinned_only: bool = False
3637
) -> List["types.Dialog"]:
@@ -40,9 +41,9 @@ async def get_dialogs(
4041
For a more convenient way of getting a user's dialogs see :meth:`~pyrogram.Client.iter_dialogs`.
4142
4243
Parameters:
43-
offset_date (``int``):
44-
The offset date in Unix time taken from the top message of a :obj:`~pyrogram.types.Dialog`.
45-
Defaults to 0. Valid for non-pinned dialogs only.
44+
offset_date (:py:obj:`~datetime.datetime`):
45+
The offset date taken from the top message of a :obj:`~pyrogram.types.Dialog`.
46+
Defaults to epoch. Valid for non-pinned dialogs only.
4647
4748
limit (``str``, *optional*):
4849
Limits the number of dialogs to be retrieved.
@@ -73,7 +74,7 @@ async def get_dialogs(
7374
else:
7475
r = await self.send(
7576
raw.functions.messages.GetDialogs(
76-
offset_date=offset_date,
77+
offset_date=utils.datetime_to_timestamp(offset_date),
7778
offset_id=0,
7879
offset_peer=raw.types.InputPeerEmpty(),
7980
limit=limit,

pyrogram/methods/chats/restrict_chat_member.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19+
from datetime import datetime
1920
from typing import Union
2021

21-
from pyrogram import raw
22+
from pyrogram import raw, utils
2223
from pyrogram import types
2324
from pyrogram.scaffold import Scaffold
2425

@@ -29,7 +30,7 @@ async def restrict_chat_member(
2930
chat_id: Union[int, str],
3031
user_id: Union[int, str],
3132
permissions: "types.ChatPermissions",
32-
until_date: int = 0
33+
until_date: datetime = datetime.fromtimestamp(0)
3334
) -> "types.Chat":
3435
"""Restrict a user in a supergroup.
3536
@@ -47,26 +48,26 @@ async def restrict_chat_member(
4748
permissions (:obj:`~pyrogram.types.ChatPermissions`):
4849
New user permissions.
4950
50-
until_date (``int``, *optional*):
51-
Date when the user will be unbanned, unix time.
51+
until_date (:py:obj:`~datetime.datetime`, *optional*):
52+
Date when the user will be unbanned.
5253
If user is banned for more than 366 days or less than 30 seconds from the current time they are
53-
considered to be banned forever. Defaults to 0 (ban forever).
54+
considered to be banned forever. Defaults to epoch (ban forever).
5455
5556
Returns:
5657
:obj:`~pyrogram.types.Chat`: On success, a chat object is returned.
5758
5859
Example:
5960
.. code-block:: python
6061
61-
from time import time
62+
from datetime import datetime, timedelta
6263
6364
from pyrogram.types import ChatPermissions
6465
6566
# Completely restrict chat member (mute) forever
6667
app.restrict_chat_member(chat_id, user_id, ChatPermissions())
6768
6869
# Chat member muted for 24h
69-
app.restrict_chat_member(chat_id, user_id, ChatPermissions(), int(time() + 86400))
70+
app.restrict_chat_member(chat_id, user_id, ChatPermissions(), datetime.now() + timedelta(days=1))
7071
7172
# Chat member can only send text messages
7273
app.restrict_chat_member(chat_id, user_id, ChatPermissions(can_send_messages=True))
@@ -76,7 +77,7 @@ async def restrict_chat_member(
7677
channel=await self.resolve_peer(chat_id),
7778
participant=await self.resolve_peer(user_id),
7879
banned_rights=raw.types.ChatBannedRights(
79-
until_date=until_date,
80+
until_date=utils.datetime_to_timestamp(until_date),
8081
send_messages=not permissions.can_send_messages,
8182
send_media=not permissions.can_send_media_messages,
8283
send_stickers=not permissions.can_send_other_messages,

pyrogram/methods/invite_links/create_chat_invite_link.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19+
from datetime import datetime
1920
from typing import Union
2021

21-
from pyrogram import raw
22+
from pyrogram import raw, utils
2223
from pyrogram import types
2324
from pyrogram.scaffold import Scaffold
2425

@@ -28,7 +29,7 @@ async def create_chat_invite_link(
2829
self,
2930
chat_id: Union[int, str],
3031
name: str = None,
31-
expire_date: int = None,
32+
expire_date: datetime = None,
3233
member_limit: int = None,
3334
creates_join_request: bool = None
3435
) -> "types.ChatInviteLink":
@@ -46,8 +47,8 @@ async def create_chat_invite_link(
4647
name (``str``, *optional*):
4748
Invite link name.
4849
49-
expire_date (``int``, *optional*):
50-
Point in time (Unix timestamp) when the link will expire.
50+
expire_date (:py:obj:`~datetime.datetime`, *optional*):
51+
Point in time when the link will expire.
5152
Defaults to None (no expiration date).
5253
5354
member_limit (``int``, *optional*):
@@ -74,7 +75,7 @@ async def create_chat_invite_link(
7475
r = await self.send(
7576
raw.functions.messages.ExportChatInvite(
7677
peer=await self.resolve_peer(chat_id),
77-
expire_date=expire_date,
78+
expire_date=utils.datetime_to_timestamp(expire_date),
7879
usage_limit=member_limit,
7980
title=name,
8081
request_needed=creates_join_request

pyrogram/methods/invite_links/edit_chat_invite_link.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19+
from datetime import datetime
1920
from typing import Union
2021

21-
from pyrogram import raw
22+
from pyrogram import raw, utils
2223
from pyrogram import types
2324
from pyrogram.scaffold import Scaffold
2425

@@ -29,7 +30,7 @@ async def edit_chat_invite_link(
2930
chat_id: Union[int, str],
3031
invite_link: str,
3132
name: str = None,
32-
expire_date: int = None,
33+
expire_date: datetime = None,
3334
member_limit: int = None,
3435
creates_join_request: bool = None
3536
) -> "types.ChatInviteLink":
@@ -48,9 +49,9 @@ async def edit_chat_invite_link(
4849
name (``str``, *optional*):
4950
Invite link name.
5051
51-
expire_date (``int``, *optional*):
52-
Point in time (Unix timestamp) when the link will expire.
53-
Defaults to None (no change), pass 0 to set no expiration date.
52+
expire_date (:py:obj:`~datetime.datetime`, *optional*):
53+
Point in time when the link will expire.
54+
Defaults to None (no change), pass ``datetime.fromtimestamp(0)`` to set no expiration date.
5455
5556
member_limit (``int``, *optional*):
5657
Maximum number of users that can be members of the chat simultaneously after joining the chat via this
@@ -77,7 +78,7 @@ async def edit_chat_invite_link(
7778
raw.functions.messages.EditExportedChatInvite(
7879
peer=await self.resolve_peer(chat_id),
7980
link=invite_link,
80-
expire_date=expire_date,
81+
expire_date=utils.datetime_to_timestamp(expire_date),
8182
usage_limit=member_limit,
8283
title=name,
8384
request_needed=creates_join_request

pyrogram/methods/messages/copy_media_group.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19+
from datetime import datetime
1920
from typing import Union, List
2021

2122
from pyrogram import types, utils, raw
@@ -31,7 +32,7 @@ async def copy_media_group(
3132
captions: Union[List[str], str] = None,
3233
disable_notification: bool = None,
3334
reply_to_message_id: int = None,
34-
schedule_date: int = None,
35+
schedule_date: datetime = None,
3536
) -> List["types.Message"]:
3637
"""Copy a media group by providing one of the message ids.
3738
@@ -65,8 +66,8 @@ async def copy_media_group(
6566
reply_to_message_id (``int``, *optional*):
6667
If the message is a reply, ID of the original message.
6768
68-
schedule_date (``int``, *optional*):
69-
Date when the message will be automatically sent. Unix time.
69+
schedule_date (:py:obj:`~datetime.datetime`, *optional*):
70+
Date when the message will be automatically sent.
7071
7172
Returns:
7273
List of :obj:`~pyrogram.types.Message`: On success, a list of copied messages is returned.
@@ -114,7 +115,7 @@ async def copy_media_group(
114115
multi_media=multi_media,
115116
silent=disable_notification or None,
116117
reply_to_msg_id=reply_to_message_id,
117-
schedule_date=schedule_date
118+
schedule_date=utils.datetime_to_timestamp(schedule_date)
118119
),
119120
sleep_threshold=60
120121
)

pyrogram/methods/messages/copy_message.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

1919
import logging
20+
from datetime import datetime
2021
from typing import Union, List, Optional
2122

2223
from pyrogram import types, enums
@@ -36,7 +37,7 @@ async def copy_message(
3637
caption_entities: List["types.MessageEntity"] = None,
3738
disable_notification: bool = None,
3839
reply_to_message_id: int = None,
39-
schedule_date: int = None,
40+
schedule_date: datetime = None,
4041
protect_content: bool = None,
4142
reply_markup: Union[
4243
"types.InlineKeyboardMarkup",
@@ -83,8 +84,8 @@ async def copy_message(
8384
reply_to_message_id (``int``, *optional*):
8485
If the message is a reply, ID of the original message.
8586
86-
schedule_date (``int``, *optional*):
87-
Date when the message will be automatically sent. Unix time.
87+
schedule_date (:py:obj:`~datetime.datetime`, *optional*):
88+
Date when the message will be automatically sent.
8889
8990
protect_content (``bool``, *optional*):
9091
Protects the contents of the sent message from forwarding and saving.

pyrogram/methods/messages/forward_messages.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19+
from datetime import datetime
1920
from typing import Union, Iterable, List
2021

21-
from pyrogram import raw
22+
from pyrogram import raw, utils
2223
from pyrogram import types
2324
from pyrogram.scaffold import Scaffold
2425

@@ -30,7 +31,7 @@ async def forward_messages(
3031
from_chat_id: Union[int, str],
3132
message_ids: Union[int, Iterable[int]],
3233
disable_notification: bool = None,
33-
schedule_date: int = None,
34+
schedule_date: datetime = None,
3435
protect_content: bool = None
3536
) -> Union["types.Message", List["types.Message"]]:
3637
"""Forward messages of any kind.
@@ -54,8 +55,8 @@ async def forward_messages(
5455
Sends the message silently.
5556
Users will receive a notification with no sound.
5657
57-
schedule_date (``int``, *optional*):
58-
Date when the message will be automatically sent. Unix time.
58+
schedule_date (:py:obj:`~datetime.datetime`, *optional*):
59+
Date when the message will be automatically sent.
5960
6061
protect_content (``bool``, *optional*):
6162
Protects the contents of the sent message from forwarding and saving.
@@ -85,7 +86,7 @@ async def forward_messages(
8586
id=message_ids,
8687
silent=disable_notification or None,
8788
random_id=[self.rnd_id() for _ in message_ids],
88-
schedule_date=schedule_date,
89+
schedule_date=utils.datetime_to_timestamp(schedule_date),
8990
noforwards=protect_content
9091
)
9192
)

0 commit comments

Comments
 (0)