0

Hello I'm trying to make a countdown timer with exactly milliseconds and print it to terminal.

input : a float value like (9.200)
output : countdown time for 9 seconds 200 milliseconds

like below site tool https://www.online-stopwatch.com/countdown-timer/

even i could try to print time in milliseconds format but the time is not exactly. Maybe it's a not good question but Is there any way to do it?. Thank you guys so much.

I tried to search about this case but can't match any Below code is what i got from internet but time is not correct and i need a countdown timer a above site had. Thanks

from __future__ import print_function, division
import time , datetime
counter, timestr = 0, ''

print(time.asctime())
t0 = time.time()
try:
    while True:
        sectime, ff = divmod(counter,1000)
        mintime, ss = divmod(sectime,60)
        hh, mm = divmod(mintime, 60)
        print(''.join('\b' for c in timestr), end='')
        timestr='%02i:%02i:%02i.%2s' % (hh, mm, ss, ff)
        print(timestr, end='')
        time.sleep(.1)
        counter += 1
except KeyboardInterrupt:
    t0 = time.time() - t0
    print('\nCounter seconds: %.1f, time.time seconds: %.1f'
          % (counter/10.0, t0 ))
Neosteam
  • 51
  • 8
  • 1
    Please provide more detail. What have you tried so far? Where did you get stuck? Do you have any output or errors? – Jan Wilamowski May 18 '21 at 10:15
  • @JanWilamowski i tried to search some code via internet as below but it's not working. Btw do you have any way to make simple countdown timer as above site? sectime, ff = divmod(counter,1000) mintime, ss = divmod(sectime,60) hh, mm = divmod(mintime, 60) print(''.join('\b' for c in timestr), end='') timestr='%02i:%02i:%02i.%2s' % (hh, mm, ss, ff) – Neosteam May 18 '21 at 10:31
  • Please edit your answer to show your code and any errors you have gotten. You're more likely to get answers by showing your effort instead of asking for a complete solution. – Jan Wilamowski May 18 '21 at 10:38
  • @JanWilamowski i'm asking for a complete solution :D – Neosteam May 18 '21 at 10:48

1 Answers1

4

You haven't mentioned what exactly didn't work for you, but I'll attempt to give a general answer anyway. You could try something like this -

# Saved as timer.py
import sys, time

## Create the variable _TIME from sys.argv or wherever, make sure it is a valid float, > 0, etc etc
## Depends on how you expect input

_TIME = 5  # Just for example, remove this

while _TIME > 0:
    m, s = divmod(_TIME, 60)
    h, m = divmod(m, 60)
    print(f"\r{int(h)}".rjust(3,'0'), f"{int(m)}".rjust(2,'0'), 
          f"{s:.3f}".rjust(5,'0'), sep=':', end='')
    _TIME -= 0.001
    time.sleep(0.001)
else:
    print("\r  Completed !  ")

Then use $ python3 timer.py 5 or something like that in your terminal.

Notes :

  • This may not be accurate, especially if your print statement is slow in the the terminal. See What is the best way to repeatedly execute a function every x seconds? for several better alternatives since accuracy may be important here.
  • In some terminals/GUIs, \r may not actually move the cursor back to the start of the line as expected. You might not be able to really do anything about this, that's a problem with the terminal app.

Edit:

Right after I posted this, I saw that you'd updated your question. The code vou've shared definitely has problems - its a lot for me to completely explain here. The most obvious is time.sleep(0.1) waits 100ms, not 1, so your timer won't ever update that often. Try reading other sources on how to accomplish what you want.

gdcodes
  • 266
  • 2
  • 10
  • Thank you code is awe some, it's almost what i need. btw do we have any way to correct this timer? due to as you said if we print this timer to terminal to display then it will slow down the code? and this may not be accurate? and is there any convenience to convert a float value to a timer? and show it? as i wrote in my input and output? Thank you so much – Neosteam May 18 '21 at 11:20