Created
June 1, 2022 06:46
-
-
Save 0x2a94b5/4aee4050a16cc4b4b0a870fad7267ac6 to your computer and use it in GitHub Desktop.
converting 13-digit unixtime in ms to timestamp in python.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
converting 13-digit unixtime in ms to timestamp in python. | |
""" | |
from datetime import datetime | |
from pydantic import BaseModel, validator | |
class Timestamp(BaseModel): | |
time: int | |
@validator('time') | |
def check_time(cls, value): | |
if len(str(value)) == 13: | |
return value | |
raise ValueError('timestamp is invalid') | |
def int_to_datetime(time: int): | |
try: | |
Timestamp(time=time) | |
except ValueError as e: | |
return e | |
_time = datetime.fromtimestamp(time/1000) | |
return _time.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3] | |
if __name__ == '__main__': | |
res = int_to_datetime(1654061396980) | |
print(res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment