1

I need to get negative number after division by % operator. There is a new class that I created. It gets duration of process in milliseconds and represents it in seconds, minutes, hours and days. In case of negative input, it should represent negative value of duration. I made method for representing seconds.

    class Duration:
        def __init__(self, milliseconds: int):
            self.__milliseconds = int(milliseconds)

        def seconds(self):
            return abs(self.__milliseconds) // 1000 % 60 * self.is_negative()

        def is_negative(self):
            return -1 if self.__milliseconds < 0 else 1

The function is_negative() helps return negative value after % division. Otherwise I get 1 after % divison, and it disrupts my add() calculation. Like Duration(1000)-Duration(3000) or etc.

Is there more convinient way for getting seconds value in case of negative milliseconds value?

Itzhak Galitch
  • 366
  • 4
  • 19

1 Answers1

0

What don't you like about your code ? I would have done it like this, but yours looks ok as well...

class Duration:
    def __init__(self, milliseconds: int):
        self.__milliseconds = int(milliseconds)

    def seconds(self):
        div, mod = divmod(self.__milliseconds // 1000, 60)
        return mod if div >= 0 else mod-60
Guillaume
  • 5,497
  • 3
  • 24
  • 42