I wanted to format this string but I don't know how I can do the formatting part at all.

class MyTime:

    def __init__(self, hrs=0, mins=0, secs=0):
        self.hours = hrs
        self.minutes = mins
        self.seconds = secs

        if self.seconds >= 60:
            self.minutes += self.seconds // 60
            self.seconds = self.seconds % 60

        if self.minutes >= 60:
            self.hours += self.minutes // 60
            self.minutes = self.minutes % 60

        if self.hours >= 24:
            self.hours = self.hours % 24

    def get_sec(self):
        return (self.hours * 60 + self.minutes) * 60 + self.seconds

    def __str__(self):
        return "{:02d}:{:02d}:{:02d}".\
               format(self.hours, self.minutes, self.seconds)

    def increment(self, t):
        totalsec = self.get_sec() + t
        return totalsec


def between(t1, t3, t2):
    return t1.get_sec() <= t3.get_sec() <= t2.get_sec()


t1 = MyTime(9, 59, 59)

t2 = MyTime(10, 0, 1)

t3 = MyTime(10, 0, 0)

print (t1.increment(300))
print (t1.increment(-300))

I want to format it with something like this:

"{:02d}:{:02d}:{:02d}".\
format(self.hours, self.minutes, self.seconds

Any help would be great.

If you do print(t1), you'll use the __str__ method that you defined. The reason that it's not being called when you do print(t1.increment(300)) is that increment returns an integer, not a MyTime, so print prints it using the normal rules for printing integers. To fix this you should probably make increment return a MyTime rather than an integer.

So I've changed increment as per your suggestion:

def increment(self, t):
        totalsec = self.get_sec() + t
        return MyTime(totalsec)

But that adds t as hours, not as seconds.

Yes, because secs is the third argument and hrs is the first. You can use either MyTime(0, 0, totalsec) or MyTime(secs = totalsec).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.