python - 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
解决方案
推荐阅读
- php - PHPMailer 不工作:有新的方法吗?
- python - Pandas 数据框分组求和,同时忽略非数值
- tensorflow - 修复 seq2seq 模型的错误输出
- arrays - 如何使用 eloquent 在数组数组中搜索字符串
- angular - (Angular 2+)尝试设置基于 sessionStorage 的 var ngOnInit() 但在页面加载后未定义
- docker - 如何在 docker 容器中导出 DGraph 数据库(Windows)
- java - 错误:找不到属性“com.example.b3rk4.myapplication:layout_constraintTop_toTopOf”
- php - JSON API 到 HTML Koinex API
- javascript - 如何在多个鼠标悬停上更改光标
- d3.js - Music21 和 D3.js 用于音乐特征提取和可视化?