Discord 机器人在 strftime 语句中将小时数显示为 00

问题描述 投票:0回答:1

我正在创建一个 Discord 机器人。当您输入命令时,它应该给出当前时间的时间戳。我用

strftime
做到了这一点。但每当输入命令时,小时都会显示为 00。分和秒显示正常,只是小时始终为 00。

@bot.command()
async def start(ctx):
    if session.is_active:
        await ctx.send("Always look to the future!")
        return
    
    session.is_active = True
    session.start_time = ctx.message.created_at.timestamp()
    human_readable_time = ctx.message.created_at.strftime("%H:%M:%S")
    positive_reminder.start()
    await ctx.send("Positive thoughts fuel a positive mind. Started at {}".format(human_readable_time))

消息应准确说出上面列出的内容:

Positive thoughts fuel a positive mind. Started at 07.08.32
(时间为示例)

输出为:

Positive thoughts fuel a positive mind. Started at 00.08.32

python discord strftime
1个回答
0
投票

您需要将时区信息添加到时间戳中:

import pytz

session.start_time = ctx.message.created_at.timestamp()
# Add timezone info to the timestamp
tz = pytz.timezone('UTC')
timestamp_with_tz = datetime.utcfromtimestamp(session.start_time).replace(tzinfo=tz)
human_readable_time = timestamp_with_tz.strftime("%H:%M:%S")
© www.soinside.com 2019 - 2024. All rights reserved.