Discord bot displays hours as 00 in strftime statement

34 Views Asked by At

I am creating a Discord bot. When you enter the command, it is supposed to give a timestamp of the current time. I did this with strftime. But whenever the command is entered, the hours keep coming up as 00. The minutes and seconds display properly, just the hours is always 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))

The message should say exactly what is listed above: Positive thoughts fuel a positive mind. Started at 07.08.32 (Time is a sample)

Output is: Positive thoughts fuel a positive mind. Started at 00.08.32

1

There are 1 best solutions below

1
fannheyward On

You need to add timezone info to the timestamp:

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")