首页 > 解决方案 > OSError 22 无效参数 [将纪元时间戳转换为人类时间]

问题描述

我正在尝试将纪元时间戳转换为日期时间。

我试过使用 datetime.fromtimestamp 但它给了我一个无效的参数错误。

这是我的代码:

def get_last_login(name):
    url = f"https://api.hypixel.net/player?key={API_KEY}&name={name}"
    res = requests.get(url)
    data = res.json()
    if data["player"] is None:
        return None

    lastlogouttime = (data["player"]["lastLogout"])
    timedate = datetime.fromtimestamp(lastlogouttime)
    return format(timedate)

lastlogouttime 从 hypixel api 返回一个纪元时间戳。

编辑:完整追溯:

Ignoring exception in command lastlogin:
Traceback (most recent call last):
  File "C:\Users\Deagan\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 83, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users\Deagan\Desktop\hybot\bot.py", line 46, in lastlogin
    lastlogin = hypixelcustomwrapper.get_last_login(name)
  File "C:\Users\Deagan\Desktop\hybot\hypixelcustomwrapper.py", line 74, in get_last_login
    timedate = datetime.fromtimestamp(lastlogouttime)
OSError: [Errno 22] Invalid argument

进口声明:

from datetime import datetime, timezone

它从 api 得到什么:

"lastLogout":1589588947970"

它应该返回什么:

1589588947970

标签: pythonepoch

解决方案


推荐阅读