需要帮助将 Unix 时间戳解码为日期(Discord)

时间:2021-06-26 09:05:35

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

基本上我需要帮助来解码 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)

1 个答案:

答案 0 :(得分:0)

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

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