Q:

My uptime function isn't able to go beyond 24 hours on Heroku

I have the following uptime function for a Python Discord bot:
import datetime

start_time = datetime.datetime.utcnow() # Timestamp of when it came online

@client.command(pass_context=True)
async def uptime(ctx: commands.Context):
    now = datetime.datetime.utcnow() # Timestamp of when uptime function is run
    delta = now - start_time
    hours, remainder = divmod(int(delta.total_seconds()), 3600)
    minutes, seconds = divmod(remainder, 60)
    days, hours = divmod(hours, 24)
    if days:
        time_format = "**{d}** days, **{h}** hours, **{m}** minutes, and **{s}** seconds."
    else:
        time_format = "**{h}** hours, **{m}** minutes, and **{s}** seconds."
    uptime_stamp = time_format.format(d=days, h=hours, m=minutes, s=seconds)
    await client.say("{} has been up for {}".format(client.user.name, uptime_stamp))

I deployed this bot to Heroku (free tier) and over several days observed that I was never able to get an uptime of 24 hours or more (i.e. the uptime didn't report days, seemingly resetting midway somewhere, even though the bot was online throughout). I figured something might be wrong with my function, so I added print statements to debug, with a sample start time, like so.
My output showed that the function is capable of handling differences > 1 day:
The start time is: 2018-09-02 00:00:00
The time now is: 2018-09-03 18:58:03.458852
The delta is 1 day, 18:58:03.458852
The time difference in seconds is: 154683
Hours: 42, remainder: 3483 seconds
Minutes: 58, remainder: 3 seconds
Days: 1, remainder: 18 hours
The time difference is over one day.
**1** days, **18** hours, **58** minutes, and **3** seconds
>>> 

Two questions:
1) Is my uptime function sound? and 2) I'm reading now that Heroku may be resetting their free dynos every 24 hours - if this is the case, how can I get an uptime function that actually works over several days?
Thanks!

A:

Heroku restarts your dyno every 24 hours so you will never see more than 24 hours uptime. You would have to persist data in a store like Redis so it spans restarts.

