Python Time Handling

Time and logic is needed to do anything fancy in Python.

#!/usr/bin/env python

import datetime
import time

# global time of last frame
last_time = datetime.datetime.now()

# global time of current frame
current_time = datetime.datetime.now()

# a running some of the delta time of each frame
sumTime = datetime.timedelta(0, 0)

# define an update function
def update():
 # make global accessibles from function
 global deltaTime;
 global sumTime;

 #prints the current time hours, minutes, seconds, and milliseconds
 #print (datetime.datetime.now().strftime("%H:%M:%S.%f"))

 # if condition checks for 1 second to pass
 if (sumTime.total_seconds() > 1.0):
  # print the elapsed time over the last second
  print (sumTime)
  # reset the elapsed time
  sumTime -= datetime.timedelta(0, 1)
 return

try:
 while True:

  # record the time in the last frame
  last_time = current_time

  # get the current time hours, minutes, seconds, milliseconds
  current_time = datetime.datetime.now()

  # calculate the time difference between frames
  deltaTime = current_time - last_time;

  # keep track of the elapsed time
  sumTime += deltaTime;

  # invoke the update function
  update();

  # yield for the next frame
  time.sleep(0);

# wait for a key to exit
except KeyboardInterrupt:

 print '\r\nProgam complete.'