Skip to content

Instantly share code, notes, and snippets.

@akirap3
Created January 8, 2021 11:45
Show Gist options
  • Save akirap3/7953b6788ffb3678c28be553df14f970 to your computer and use it in GitHub Desktop.
Save akirap3/7953b6788ffb3678c28be553df14f970 to your computer and use it in GitHub Desktop.
class Interval:
       
    def __init__(self, hours=0, minutes=0, seconds=0):
        self.hours = hours
        self.minutes = minutes
        self.seconds = seconds
        self.total_seconds = 60*60*self.hours + 60*self.minutes + self.seconds
        
    def __str__(self):
        self.init_str = "{}:{}:{}".format(self.hours, self.minutes, self.seconds)
        return self.init_str
        
    def output(self, result):
        self.result_hours = self.result // 3600
        self.result_minutes = (self.result - self.result_hours*3600) // 60
        self.result_seconds = self.result - self.result_hours*3600 - self.result_minutes*60
        self.message = "{}:{}:{}".format(self.result_hours, self.result_minutes, self.result_seconds)
        return self.message
                
    def __add__(self, other):
        if isinstance(other, Interval):
            self.result = self.total_seconds + other.total_seconds
        elif isinstance(other, int):
            self.result = self.total_seconds + other
        else:
            raise TypeError()
        return self.output(self.result)
                
    def __sub__(self, other):
        if isinstance(other, Interval):
            self.result = self.total_seconds - other.total_seconds
        elif isinstance(other, int):
            self.result = self.total_seconds - other
        else:
            raise TypeError()
        return self.output(self.result)
    
    def __mul__(self, other):
        self.result = self.total_seconds * other
        return self.output(self.result)

ttl = Interval(21,58,50)
print("The Time Interval:\t\t", ttl)
print("The result of (ttl + 62):\t", ttl + 62)
print("The result of (ttl - 62):\t", ttl - 62)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment