1

there was a lot of models available here of a countdown timer but almost all of them does not have a millisecond value

the model im using :

import time

def countdown(t):

    while t:
        mins, secs = divmod(t, 60)
        timer = '{:02d}:{:02d}'.format(mins, secs)
        print(timer, end="\r")
        time.sleep(1)
        t -= 1
  
    print('Fire in the hole!!')


# input time in seconds
t = input("Enter the time in seconds: ")

# function call
countdown(int(t))

i understand the divmod() method but i still find it difficult to understand how to extract milliseconds from this

  • You sleep about 1 s - this may pan out to exactly 1000ms or be slightly more (never less) - why do you need ms displaY? – Patrick Artner Apr 19 '22 at 12:09
  • 2
    https://stackoverflow.com/questions/5998245/get-current-time-in-milliseconds-in-python – JadenJin Apr 19 '22 at 12:10
  • @PatrickArtner , i need it for an animation to perform text reveal at specific times in my video , and seconds is not showing precisely at time the (t = input("Enter the time in seconds: ")) is not necessary in my model because i'll use it in the background just to show up text at certain times in milliseconds and seconds – kakashi senpai Apr 19 '22 at 12:17
  • Indentation _is part of the control flow in python, so part of the code_ - it will change how code is executed. Fix it and the "negativity" vanishes. – Patrick Artner Apr 19 '22 at 12:26
  • 2
    the Indentation in correct in my code editor , just in the website here it lost the spaces that's it , im not used to stackoverflow editor @PatrickArtner – kakashi senpai Apr 19 '22 at 12:28
  • Thats why I told you about it and suggested you [edit] it. **I have no clue how _your_ code on your side looks** - and if the problem you have is related to that or something completely different. – Patrick Artner Apr 19 '22 at 12:33
  • @PatrickArtner , i get what you mean , because the problem sometimes maybe the Indentation itself if there was an error , but since i didn't mention any error in my situation and i only mention that i need a milliseconds in my model , this alone could tell you a hint that the code is working correctly from my end , but still you are right about fixing the Indentation , because some new students who have no enough knowledge in python format may want to copy that model as it is , and if it has a writing problem it may confuse them :) – kakashi senpai Apr 19 '22 at 12:38

2 Answers2

2

Your code does not keep track of any milliseconds. You sleep(1) - which should sleep at least 1000ms (maybe more - see here - it depends on what is going on on your PC elsewise).

To display any ms you need to capture the current time somehow:

from datetime import datetime, timedelta
import time

def countdown(seconds):
    started = datetime.now()
    ended = started + timedelta(seconds=seconds)
    while datetime.now() < ended:
        print(f"Waiting for {ended-datetime.now()}", flush=True)
        time.sleep(1)

    now = datetime.now()
    if now > ended:
        print(f'Sorry, I overslept: {now-ended}')

    print('Fire in the hole!!')


# input time in seconds
t ="4"

# function call
countdown(int(t))

To get:

Waiting for 0:00:04
Waiting for 0:00:02.995686
Waiting for 0:00:01.995361
Waiting for 0:00:00.980077
Sorry, I overslept: 0:00:00.020248
Fire in the hole!!

You can format the timedelta to your conveniece - more solutions to that f.e. here: Formatting timedelta objects.

Sleeping for a calculated time like

while datetime.now() < ended:
    remainder = ended-datetime.now()
    print(f"Waiting for {remainder}", flush=True)
    time.sleep(min(1, ( ended-datetime.now()).total_seconds()))

could try to minimize your over-sleep time on the last loop. You could also try to do this for every loop by calculating what you need to sleep if need better 1s precision.

But in the end your loops may still be off due to factors you can not influence.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
-2
fulldate = datetime.datetime.strptime(date + ' ' + time, "%Y-%m-%d %H:%M:%S.%f")
fulldate = fulldate + datetime.timedelta(milliseconds=500)

This should solve your problem, https://docs.python.org/2/library/datetime.html#timedelta-objects

Emi OB
  • 2,814
  • 3
  • 13
  • 29
sodium
  • 9
  • 2
  • 3
    Code only answers are bad - explain _why_ this solves it. Ill formatted code only answers are even worse. – Patrick Artner Apr 19 '22 at 12:11
  • 2
    Link only answers are also unhelpful as sometimes links break. You should include some explanation in your answer to explain how/why the code/link solves the question :) – Emi OB Apr 19 '22 at 12:44