greeting = '''Welcome to Timelab, another wonderful creation by Mason Simon

This is an interactive shell for doing arithmetic with time. A time is written
in the form HOURS:MINUTES:SECONDS or just MINUTES:SECONDS. For instance,
54:13.7 is a valid time, and it's equivalent to 00:54:13.7

The following examples demonstrate the supported operations.

0_o>  5:13
ans = 00:05:13
0_o>  12:00:00 + 0:54:13 - 4:13
ans = 12:50:00
0_o>  2:00 * 10 / 5
ans = 00:04:00
0_o>  2:00 * 10**2
ans = 03:20:00
0_o>  q
bye bye.



'''
print greeting



PROMPT = '0_o>  '


import re

validity_test = re.compile(r'[+-/*0-9:]')
def is_valid(s):
    return validity_test.match(s) != None


hms_re = re.compile(r'(?:(\d+):)?(\d+):(\d+)')
def hms2secs(hms):
    """hms is a string of the form hours:minutes:seconds, or minutes:seconds.
       the return value will be a string containing the number of seconds that
       hms is equivalent to.
    """
    if hms.group(1):
      hours = int( hms.group(1) )
    else:
      hours = 0
    mins = int( hms.group(2) )
    secs = float( hms.group(3) )

    return '%s' % (hours*60**2 + mins*60 + secs)

def secs2hms(seconds_str):
    """seconds_str is a number. the return value will be a string of the form
       hours:minutes:seconds
    """
    total_secs = float(seconds_str)
    (total_mins, secs) = divmod(total_secs, 60)
    (hours, mins) = divmod(total_mins, 60)

    return '%02d:%02d:%02.2g' % (hours, mins, secs)


ans = '0'
while True:
    s = raw_input(PROMPT)

    if s in ['q','Q','quit','bye','peace out','later']:
      print 'bye bye.'
      import time
      time.sleep(.5)
      break

    s = s.replace('ans', ans)
    
    if is_valid(s) == False:
        print 'invalid input: time expressions must be in the form HOURS:MINUTES:SECONDS'
        continue
    
    seconds_arithmetic = hms_re.sub(hms2secs, s)
    try:
      total_seconds = eval(seconds_arithmetic)
    except:
      print "that didn't taste good. feed me something else!"
      continue
    ans = secs2hms(total_seconds)
    print 'ans = %s' % ans
