Time before full counter with percent python

Question:

how to calculate based on timestamp and get the progress before the max full value?

def full_energy():
     time_now = 1666650096 #changes every update
     time_end = 1666679529 
     max_energy = 50 

     diff = datetime.utcfromtimestamp(time_now) - datetime.utcfromtimestamp(time_end)
     secs = diff.total_seconds()
     ???

# expected output
# x/y (z)
# 25/50 (50%)

how to get the value of x and z based on this sample?

Asked By: TheDon

||

Answers:

Something like this will work. You need to provide the start time to compute percent completed. Not sure how you want the display to function:

from datetime import datetime, timedelta
import time

def full_energy(start, now, end):
    max_energy = 50
    percent = (now - start) / (end - start)
    current_energy = max_energy * percent
    # In a typical text console this will overwrite the same line
    print(f'r{current_energy:.0f}/{max_energy} ({percent:.0%})', end='', flush=True)

start = datetime.now()
end = start + timedelta(seconds=10)

while (now := datetime.now()) <= end:
    time.sleep(.2)
    full_energy(start, now, end)
Answered By: Mark Tolonen
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.