mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2026-09-04 16:10:39 +00:00
Added native support for Python's datetime object
Now you can make up your Telegram Requests by using the built-in `datetime` instead of manually parsing it
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
from datetime import datetime
|
||||
from io import BytesIO, BufferedReader
|
||||
from tl.all_tlobjects import tlobjects
|
||||
from struct import unpack
|
||||
@@ -95,6 +96,11 @@ class BinaryReader:
|
||||
else:
|
||||
raise ValueError('Invalid boolean code {}'.format(hex(value)))
|
||||
|
||||
def tgread_date(self):
|
||||
"""Reads and converts Unix time (used by Telegram) into a Python datetime object"""
|
||||
value = self.read_int()
|
||||
return None if value == 0 else datetime.fromtimestamp(value)
|
||||
|
||||
def tgread_object(self):
|
||||
"""Reads a Telegram object"""
|
||||
constructor_id = self.read_int(signed=False)
|
||||
|
||||
@@ -83,12 +83,17 @@ class BinaryWriter:
|
||||
|
||||
def tgwrite_string(self, string):
|
||||
"""Write a string by using Telegram guidelines"""
|
||||
return self.tgwrite_bytes(string.encode('utf-8'))
|
||||
self.tgwrite_bytes(string.encode('utf-8'))
|
||||
|
||||
def tgwrite_bool(self, boolean):
|
||||
"""Write a boolean value by using Telegram guidelines"""
|
||||
# boolTrue boolFalse
|
||||
return self.write_int(0x997275b5 if boolean else 0xbc799737, signed=False)
|
||||
self.write_int(0x997275b5 if boolean else 0xbc799737, signed=False)
|
||||
|
||||
def tgwrite_date(self, datetime):
|
||||
"""Converts a Python datetime object into Unix time (used by Telegram) and writes it"""
|
||||
value = 0 if datetime is None else int(datetime.timestamp())
|
||||
self.write_int(value)
|
||||
|
||||
# endregion
|
||||
|
||||
|
||||
Reference in New Issue
Block a user