Lesson 23: Playing with timers

posted Apr 19, 2018, 8:02 AM by Attila Lengyel
from datetime import datetime
import time, math

for x in range(0,3):
  print(datetime.now())
  time.sleep(1)

def sectimer(seconds):
  secperday = 60 * 60 * 24
  days = math.floor(seconds / secperday)
  seconds = seconds - days * secperday
  secperhour = 60 * 60
  hours = math.floor(seconds / secperhour)
  seconds = seconds - hours * secperhour
  secpermin = 60
  minutes = math.floor(seconds / secpermin)
  seconds = seconds - minutes * secpermin
  
  strtime = ""
  if days < 10: strtime += "0"
  strtime += str(days) + ":"
  if hours < 10: strtime += "0"
  strtime += str(hours) + ":"
  if minutes < 10: strtime += "0"
  strtime += str(minutes) + ":"
  if seconds < 10: strtime += "0"
  strtime += str(seconds)
  
  return strtime
  
start = datetime.now()
counter = 0
for x in range(0, 30):
  print(sectimer(counter))
  time.sleep(1)
  counter += 1

end = datetime.now()

print (end - start)

Comments