【问题标题】:Need Help Decoding Unix Time Stamp Into a Date(Discord)需要帮助将 Unix 时间戳解码为日期(不和谐)
【发布时间】:2021-06-26 09:05:35
【问题描述】:

基本上我需要帮助来解码 Unix 代码。 存放Unix代码的变量是playerlog1

我已经尝试过这段代码和其他代码,但我对此缺乏了解,因此很难在我自己的代码中实现

import datetime
timestamp = datetime.datetime.fromtimestamp(1500000000)
print(timestamp.strftime('%Y-%m-%d %H:%M:%S'))

基本上我有一个打印数字的 Unix 代码,而不是我想要修复的实际日期

import discord
import os
import requests
from discord.ext import commands
bot = commands.Bot(command_prefix='1')
num = 1
Error = 'Error Please Try Again'

def getinfo(call):
    req = requests.get(call)
    return req.json()

我的问题出在哪里⬇

@bot.command()
async def displayembed(ctx):
    uuid = "uuid"
    apikey = 'key'
    url = f'https://api.hypixel.net/player?key={apikey}&uuid={uuid}'
    urldata = getinfo(url)
    playername1 = urldata["player"]["playername"]
    playerlog1 = urldata["player"]["lastLogout"]
    squishemb = discord.Embed(title='Info', discription='this is info about redacted', colour=discord.Colour.purple())
    squishemb.add_field(name='Player Name', value=playername1, inline=True)
    squishemb.add_field(name='Logout', value=playerlog1, inline=True)

    await ctx.send(embed=squishemb)

TOKEN = "redacted"
bot.run(TOKEN)

编辑: 好的,我想通了,我需要按照 abosr 告诉我的操作,然后将变量除以 1000

playerlog1 = datetime.datetime.fromtimestamp(urldata["player"]["lastLogout"]/1000)

【问题讨论】:

  • 新代码:urldata = getinfo(url) playername1 = urldata["player"]["playername"] playerlog1 = datetime.datetime.fromtimestamp(urldata["player"]["lastLogout"]) squishemb = discord.Embed(title='Info', discription='this is info about redacted', colour=discord.Colour.purple()) squishemb.add_field(name='Player Name', value=playername1, inline=True ) squishemb.add_field(name='Logout', value=playerlog1.strftime('%Y-%m-%d %H:%M:%S'), inline=True)

标签: python-3.x datetime discord.py


【解决方案1】:

如果我理解正确,您首先想将 unix 时间戳更改为 python 日期时间(使用您自己的示例:

playerlog1 = datetime.datetime.fromtimestamp(urldata["player"]["lastLogout"])

然后使用格式化为您需要的任何格式:

squishemb.add_field(name='Logout', value=playerlog1.strftime('%Y-%m-%d %H:%M:%S'), inline=True)

'%Y-%m-%d %H:%M:%S' 字符串可以更改为日期和时间的不同显示,其格式类似于2021-06-26 12:01:02

【讨论】:

  • 感谢 Absor 的快速响应。我按照您告诉我的做了,但出现错误 AttributeError: 'int' object has no attribute 'strftime'
  • 我稍微修改了一下,我有点到了某个地方,但现在它显示了:'line 22, in displayembed playerlog1 = datetime.datetime.fromtimestamp(urldata["player"]["lastLogout"]) OSError: [Errno 22] 无效参数'
猜你喜欢
  • 2012-06-13
  • 1970-01-01
  • 1970-01-01
  • 2020-10-19
  • 2016-06-07
  • 2015-04-04
  • 2011-08-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多