mirror of
				https://github.com/LonamiWebs/Telethon.git
				synced 2025-10-30 23:47:33 +03:00 
			
		
		
		
	Support more types to represent a date
This commit is contained in:
		
							parent
							
								
									75a342e24b
								
							
						
					
					
						commit
						3537e9bcc9
					
				|  | @ -1,4 +1,5 @@ | ||||||
| from datetime import datetime | import struct | ||||||
|  | from datetime import datetime, date | ||||||
| from threading import Event | from threading import Event | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | @ -125,6 +126,23 @@ class TLObject: | ||||||
|         r.append(bytes(padding)) |         r.append(bytes(padding)) | ||||||
|         return b''.join(r) |         return b''.join(r) | ||||||
| 
 | 
 | ||||||
|  |     @staticmethod | ||||||
|  |     def serialize_datetime(dt): | ||||||
|  |         if not dt: | ||||||
|  |             return b'\0\0\0\0' | ||||||
|  | 
 | ||||||
|  |         if isinstance(dt, datetime): | ||||||
|  |             dt = int(dt.timestamp()) | ||||||
|  |         elif isinstance(dt, date): | ||||||
|  |             dt = int(datetime(dt.year, dt.month, dt.day, dt).timestamp()) | ||||||
|  |         elif isinstance(dt, float): | ||||||
|  |             dt = int(dt) | ||||||
|  | 
 | ||||||
|  |         if isinstance(dt, int): | ||||||
|  |             return struct.pack('<I', dt) | ||||||
|  | 
 | ||||||
|  |         raise TypeError('Cannot interpret "{}" as a date.'.format(dt)) | ||||||
|  | 
 | ||||||
|     # These should be overrode |     # These should be overrode | ||||||
|     def to_dict(self, recursive=True): |     def to_dict(self, recursive=True): | ||||||
|         return {} |         return {} | ||||||
|  |  | ||||||
|  | @ -540,11 +540,7 @@ class TLGenerator: | ||||||
|             builder.write('TLObject.serialize_bytes({})'.format(name)) |             builder.write('TLObject.serialize_bytes({})'.format(name)) | ||||||
| 
 | 
 | ||||||
|         elif 'date' == arg.type:  # Custom format |         elif 'date' == arg.type:  # Custom format | ||||||
|             # 0 if datetime is None else int(datetime.timestamp()) |             builder.write('TLObject.serialize_datetime({})'.format(name)) | ||||||
|             builder.write( |  | ||||||
|                 r"b'\0\0\0\0' if {0} is None else " |  | ||||||
|                 r"struct.pack('<I', int({0}.timestamp()))".format(name) |  | ||||||
|             ) |  | ||||||
| 
 | 
 | ||||||
|         else: |         else: | ||||||
|             # Else it may be a custom type |             # Else it may be a custom type | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user