Rechnen mit Uhrzeiten (Overloading)


#!/usr/bin/python
import time

class Time:
def __init__(self, seconds):
self.seconds = seconds
def __repr__(self):
return time.ctime(self.seconds)
def __add__(self, x):
return Time(self.seconds + x)
__radd__ = __add__            # support for x+t
def __sub__(self, x):
if hasattr(x, 'seconds'): # test if x could be a Time
return self.seconds - x.seconds
else:
return self.seconds - x

now = Time(time.time())
tomorrow = 24*3600 + now
yesterday = now - today
print tomorrow - yesterday        # prints 172800