简体   繁体   中英

Python Unable to convert Unix time into ISO 8601 zulu time

I have the following code, which takes the Unix time and converts it into ISO 8601 zulu time ie 2022-09-16T20:25:47.649Z

    if "reported_at" in event:
        reported_at = datetime.utcfromtimestamp(int(event.get("reported_at"))).isoformat()+"Z"

Where event is

{
  "reported_at": 1663359947649,
}

I am getting below error

{
  "errorMessage": "year 54679 is out of range",
  "errorType": "ValueError",
  "stackTrace": [
    "  File \"/var/task/app.py\", line 37, in lambda_handler\n    reported_at = datetime.utcfromtimestamp(int(event.get(\"reported_at\"))).isoformat()+\"Z\"\n"
  ]
}

Not sure what is missing I tried couple of different alterations but same error, can some please guide.

PS: I am using python 3.8

1663359947649 is the number of milliseconds, not seconds since the unix epoch: Solution. Divide by a thousand.

>>> datetime.utcfromtimestamp(1663359947649/1000)
datetime.datetime(2022, 9, 16, 20, 25, 47, 649000)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM