Skip to content

Instantly share code, notes, and snippets.

@0x2a94b5
Created June 1, 2022 06:46
Show Gist options
  • Save 0x2a94b5/4aee4050a16cc4b4b0a870fad7267ac6 to your computer and use it in GitHub Desktop.
Save 0x2a94b5/4aee4050a16cc4b4b0a870fad7267ac6 to your computer and use it in GitHub Desktop.
converting 13-digit unixtime in ms to timestamp in python.
"""
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