示例#1
0
    def __sub__(self, subtrahend):
        """
        Subtract a datetime.time or datetime.timedelta
        (with possible wrapping at the midnight)
        from the TimeEx.

        Whenever the subtrahend is the datetime.time,
        the result is a TimeDeltaEx.

        Whenever the subtrahend is the datetime.timedelta,
        the result is a TimeEx.

        >>> TimeEx(3, 4, 15, 92) - timedelta(0, 2, 71, 82)
        TimeEx(3, 4, 12, 918021)
        >>> TimeEx(3, 4, 15, 92) - timedelta(2, 71, 82, 81)
        TimeEx(3, 3, 3, 919010)
        >>> TimeEx(3, 4, 15, 92, tzinfo=DummyTZInfo()) - timedelta(2, 71, 82, 81)
        TimeEx(3, 3, 3, 919010, tzinfo=<DummyTZInfo>)

        @type subtrahend: time, timedelta
        @rtype: TimeEx, TimeDeltaEx
        """
        # TODO: HOW TO SUBTRACT DATETIME.TIME, ESPECIALLY TZ-AWARE?
        if isinstance(subtrahend, timedelta):
            return TimeEx.from_microseconds(self.in_microseconds - td_to_mus(subtrahend),
                                            tzinfo=self.tzinfo)
        else:
            raise NotImplementedError("{0!r} - {1!r}".format(self, subtrahend))
示例#2
0
    def __floordiv__(self, divisor):
        """
        Divide TimeDeltaEx by some datetime.timedelta or a number,
        with subsequent flooring to the integer value.

        For dividing TimeDeltaEx by datetime.timedelta,
        the result will be an integer number.
        For dividing TimeDeltaEx by a number, the result will be a TimeDeltaEx
        (the precision may be lost though).

        >>> TimeDeltaEx(seconds=5) // timedelta(seconds=2)
        2
        >>> TimeDeltaEx(seconds=5) // 4
        TimeDeltaEx(0, 1, 250000)
        >>> TimeDeltaEx(microseconds=75) // 2.6
        TimeDeltaEx(0, 0, 28)

        @type divisor: timedelta, numbers.Number
        @rtype: TimeDeltaEx, numbers.Number
        """
        if isinstance(divisor, timedelta):
            return self.in_microseconds // td_to_mus(divisor)
        elif isinstance(divisor, numbers.Number):
            return TimeDeltaEx.from_microseconds(self.in_microseconds //
                                                 divisor)
        else:
            raise NotImplementedError("{0!r} // {1!r}!".format(self, divisor))
示例#3
0
    def __div__(self, divisor):
        """
        Divide TimeDeltaEx by some datetime.timedelta or a number.

        For dividing TimeDeltaEx by datetime.timedelta,
        the result will be a ratio.
        For dividing TimeDeltaEx by a number, the result will be a TimeDeltaEx
        (the precision may be lost though).

        >>> TimeDeltaEx(seconds=5) / timedelta(seconds=2)
        Fraction(5, 2)

        >>> TimeDeltaEx(seconds=5) / 4
        TimeDeltaEx(0, 1, 250000)

        >>> TimeDeltaEx(microseconds=75) / 2.5
        TimeDeltaEx(0, 0, 30)

        @type divisor: timedelta, numbers.Number
        @rtype: TimeDeltaEx, numbers.Rational
        """
        if isinstance(divisor, timedelta):
            return Fraction(self.in_microseconds, td_to_mus(divisor))
        elif isinstance(divisor, numbers.Number):
            return TimeDeltaEx.from_microseconds(self.in_microseconds / divisor)
        else:
            raise NotImplementedError("{0!r} / {1!r}".format(self, divisor))
示例#4
0
    def __sub__(self, subtrahend):
        """
        Subtract a datetime.time or datetime.timedelta
        (with possible wrapping at the midnight)
        from the TimeEx.

        Whenever the subtrahend is the datetime.time,
        the result is a TimeDeltaEx.

        Whenever the subtrahend is the datetime.timedelta,
        the result is a TimeEx.

        >>> TimeEx(3, 4, 15, 92) - timedelta(0, 2, 71, 82)
        TimeEx(3, 4, 12, 918021)
        >>> TimeEx(3, 4, 15, 92) - timedelta(2, 71, 82, 81)
        TimeEx(3, 3, 3, 919010)
        >>> TimeEx(3, 4, 15, 92, tzinfo=DummyTZInfo()) - timedelta(2, 71, 82, 81)
        TimeEx(3, 3, 3, 919010, tzinfo=<DummyTZInfo>)

        @type subtrahend: time, timedelta
        @rtype: TimeEx, TimeDeltaEx
        """
        # TODO: HOW TO SUBTRACT DATETIME.TIME, ESPECIALLY TZ-AWARE?
        if isinstance(subtrahend, timedelta):
            return TimeEx.from_microseconds(self.in_microseconds -
                                            td_to_mus(subtrahend),
                                            tzinfo=self.tzinfo)
        else:
            raise NotImplementedError("{0!r} - {1!r}".format(self, subtrahend))
示例#5
0
    def __rsub__(self, minuend):
        """
        This TimeDeltaEx is subtracted from the datetime.date, datetime.datetime,
        datetime.time (with possible wrapping at the midnight),
        or datetime.timedelta.

        Whenever the minuend is the datetime.date,
        the result is automatically enhanced from datetime.date to DateEx.

        Whenever the minuend is the datetime.datetime,
        the result is automatically enhanced from datetime.datetime to DateTimeEx.

        Whenever the minuend is the datetime.time,
        the result is automatically enhanced from datetime.time to TimeEx.

        Whenever the minuend is the datetime.timedelta,
        the result is automatically enhanced from datetime.timedelta
        to TimeDeltaEx.

        >>> TimeEx(3, 4, 15, 92) - TimeDeltaEx(0, 2, 71, 82)
        TimeEx(3, 4, 12, 918021)
        >>> TimeEx(3, 4, 15, 92) - TimeDeltaEx(2, 71, 82, 81)
        TimeEx(3, 3, 3, 919010)
        >>> TimeEx(3, 4, 15, 92, tzinfo=DummyTZInfo()) - TimeDeltaEx(2, 71, 82, 81)
        TimeEx(3, 3, 3, 919010, tzinfo=<DummyTZInfo>)

        >>> time(3, 4, 15, 92) - TimeDeltaEx(0, 2, 71, 82)
        TimeEx(3, 4, 12, 918021)
        >>> time(3, 4, 15, 92) - TimeDeltaEx(2, 71, 82, 81)
        TimeEx(3, 3, 3, 919010)
        >>> time(3, 4, 15, 92, tzinfo=DummyTZInfo()) - TimeDeltaEx(2, 71, 82, 81)
        TimeEx(3, 3, 3, 919010, tzinfo=<DummyTZInfo>)

        >>> timedelta(3, 4, 15, 92) - TimeDeltaEx(2, 71, 82, 81)
        TimeDeltaEx(0, 86333, 10933)
        >>> timedelta(2, 71, 82, 81) - TimeDeltaEx(3, 4, 15, 92)
        TimeDeltaEx(-1, 66, 989067)

        @type minuend: date, datetime, time, timedelta
        @rtype: date, datetime, time, timedelta
        """
        if isinstance(minuend, date):
            # TODO
            raise NotImplementedError("Not yet implemented!")
        elif isinstance(minuend, datetime):
            # TODO
            raise NotImplementedError("Not yet implemented!")
        elif isinstance(minuend, time):
            return TimeEx.from_microseconds(t_to_mus(minuend) - self.in_microseconds,
                                            tzinfo=minuend.tzinfo)
        elif isinstance(minuend, timedelta):
            return TimeDeltaEx.from_microseconds(td_to_mus(minuend) -
                                                 self.in_microseconds)
        else:
            raise NotImplementedError("{0!r} - {1!r}".format(minuend, self))
示例#6
0
    def __rdivmod__(self, dividend):
        """
        Calculate both the result of division and the modulo
        for division of some datetime.timedelta by this TimeDeltaEx.

        >>> divmod(timedelta(seconds=42), TimeDeltaEx(seconds=11))
        (3, TimeDeltaEx(0, 9))

        @type dividend: timedelta
        @rtype: TimeDeltaEx
        """
        assert isinstance(dividend, timedelta), repr(dividend)

        _d, _m = divmod(td_to_mus(dividend), self.in_microseconds)
        return (_d, TimeDeltaEx.from_microseconds(_m))
示例#7
0
    def __sub__(self, subtrahend):
        """
        Subtract a datetime.timedelta from this TimeDeltaEx.

        >>> TimeDeltaEx(3, 4, 15, 92) - timedelta(2, 71, 82, 81)
        TimeDeltaEx(0, 86333, 10933)
        >>> TimeDeltaEx(2, 71, 82, 81) - timedelta(3, 4, 15, 92)
        TimeDeltaEx(-1, 66, 989067)

        @type subtrahend: timedelta
        @rtype: TimeDeltaEx
        """
        if isinstance(subtrahend, timedelta):
            return TimeDeltaEx.from_microseconds(self.in_microseconds -
                                                 td_to_mus(subtrahend))
        else:
            raise NotImplementedError("{0!r} - {1!r}".format(self, subtrahend))
示例#8
0
    def __rfloordiv__(self, dividend):
        """
        The dividend is divided by this TimeDeltaEx,
        with subsequent flooring to the integer value.

        The dividend must be a datetime.timedelta (or any compatible subclass).
        The result is a number.

        >>> timedelta(seconds=5) // TimeDeltaEx(seconds=2)
        2

        @type dividend: timedelta
        @rtype: number.Number
        """
        assert isinstance(dividend, timedelta), repr(dividend)

        return td_to_mus(dividend) // self.in_microseconds
示例#9
0
    def __rdiv__(self, dividend):
        """
        The dividend is divided by this TimeDeltaEx.

        The dividend must be a datetime.timedelta.
        The result is a ratio.

        >>> timedelta(seconds=5) / TimeDeltaEx(seconds=2)
        Fraction(5, 2)

        @type dividend: timedelta
        @rtype: numbers.Rational
        """
        assert isinstance(dividend, (timedelta, numbers.Number)), \
               repr(dividend)

        return Fraction(td_to_mus(dividend), self.in_microseconds)
示例#10
0
    def __add__(self, summand):
        """
        Add this TimeEx to a datetime.timedelta
        (with possible wrapping at the midnight).

        >>> TimeEx(23, 44, 55) + timedelta(hours=3, minutes=20)
        TimeEx(3, 4, 55)
        >>> TimeEx(23, 44, 55, tzinfo=DummyTZInfo()) + \
            timedelta(hours=3, minutes=20)
        TimeEx(3, 4, 55, tzinfo=<DummyTZInfo>)

        @type summand: timedelta
        @rtype: TimeEx
        """
        if isinstance(summand, timedelta):
            return TimeEx.from_microseconds(self.in_microseconds + td_to_mus(summand),
                                            tzinfo=self.tzinfo)
        else:
            raise NotImplementedError("{0!r} + {1!r}".format(self, summand))
示例#11
0
    def in_microseconds(self):
        """
        The number of microseconds in the time interval.

        The number is always integer, due to inherent storage limitation.

        Under Python 3.x, this property has two synonims:
        in_microseconds and in_µs.

        >>> TimeDeltaEx(3, 14, 15).in_microseconds
        259214000015

        >>> # Test µs_to_td() in Python 3.x only
        >>> not _PY3K or eval("TimeDeltaEx(3, 14, 15).in_microseconds == 259214000015")
        True

        @rtype: numbers.Number
        """
        return td_to_mus(self)
示例#12
0
    def __divmod__(self, divisor):
        """
        Calculate both the result of division and the modulo
        for division of this TimeDeltaEx by some datetime.timedelta.

        Even though the TimeDeltaEx may be divided to the regular number,
        the modulo for such operation cannot be calculated.

        >>> divmod(TimeDeltaEx(seconds=42), timedelta(seconds=11))
        (3, TimeDeltaEx(0, 9))

        @type divisor: timedelta
        @rtype: tuple
        """
        if isinstance(divisor, timedelta):
            _d, _m = divmod(self.in_microseconds, td_to_mus(divisor))
            return (_d, TimeDeltaEx.from_microseconds(_m))
        else:
            raise NotImplementedError("divmod({0!r}, {1!r})"
                                          .format(self, divisor))
示例#13
0
    def __rmod__(self, dividend):
        """
        Find modulo for division of some datetime.timedelta
        by the TimeDeltaEx.

        For dividing some datetime.timedelta by this TimeDeltaEx,
        the modulo will be a TimeDeltaEx.
        The modulo for dividing any other type by this TimeDeltaEx
        is not defined.

        >>> timedelta(seconds=42) % TimeDeltaEx(seconds=11)
        TimeDeltaEx(0, 9)

        @type dividend: timedelta
        @rtype: TimeDeltaEx
        """
        assert isinstance(dividend, timedelta), repr(dividend)

        return TimeDeltaEx.from_microseconds(td_to_mus(dividend) %
                                             self.in_microseconds)
示例#14
0
    def __add__(self, summand):
        """
        Add this TimeEx to a datetime.timedelta
        (with possible wrapping at the midnight).

        >>> TimeEx(23, 44, 55) + timedelta(hours=3, minutes=20)
        TimeEx(3, 4, 55)
        >>> TimeEx(23, 44, 55, tzinfo=DummyTZInfo()) + \
            timedelta(hours=3, minutes=20)
        TimeEx(3, 4, 55, tzinfo=<DummyTZInfo>)

        @type summand: timedelta
        @rtype: TimeEx
        """
        if isinstance(summand, timedelta):
            return TimeEx.from_microseconds(self.in_microseconds +
                                            td_to_mus(summand),
                                            tzinfo=self.tzinfo)
        else:
            raise NotImplementedError("{0!r} + {1!r}".format(self, summand))
示例#15
0
    def __mod__(self, divisor):
        """
        Find modulo for division of TimeDeltaEx by some
        datetime.timedelta.

        For dividing TimeDeltaEx by a datetime.timedelta,
        the modulo will be a TimeDeltaEx.
        The modulo for dividing TimeDeltaEx by a regular number
        is not defined.

        >>> TimeDeltaEx(seconds=42) % timedelta(seconds=11)
        TimeDeltaEx(0, 9)

        @type divisor: timedelta
        @rtype: TimeDeltaEx
        """
        if isinstance(divisor, timedelta):
            return TimeDeltaEx.from_microseconds(self.in_microseconds %
                                                 td_to_mus(divisor))
        else:
            raise NotImplementedError("{0!r} % {1!r}".format(self, divisor))
示例#16
0
    def __add__(self, summand):
        """
        Add this TimeDeltaEx to the datetime.date, datetime.datetime,
        datetime.time (with possible wrapping at the midnight),
        or to the datetime.timedelta.

        Whenever another summand is the datetime.date,
        the result is automatically enhanced from datetime.date to DateEx.

        Whenever another summand is the datetime.datetime,
        the result is automatically enhanced from datetime.datetime to DateTimeEx.

        Whenever another summand is the datetime.time,
        the result is automatically enhanced from datetime.time to TimeEx.

        Whenever another summand is the datetime.timedelta,
        the result is automatically enhanced from datetime.timedelta
        to TimeDeltaEx.

        >>> time(23, 44, 55) + TimeDeltaEx(hours = 3, minutes = 20)
        TimeEx(3, 4, 55)
        >>> TimeEx(23, 44, 55) + TimeDeltaEx(hours = 3, minutes = 20)
        TimeEx(3, 4, 55)
        >>> time(23, 44, 55, tzinfo=DummyTZInfo()) + TimeDeltaEx(hours=3, minutes=20)
        TimeEx(3, 4, 55, tzinfo=<DummyTZInfo>)
        >>> TimeEx(23, 44, 55, tzinfo=DummyTZInfo()) + TimeDeltaEx(hours=3, minutes =20)
        TimeEx(3, 4, 55, tzinfo=<DummyTZInfo>)

        >>> TimeDeltaEx(hours=3, minutes=20) + time(23, 44, 55)
        TimeEx(3, 4, 55)
        >>> TimeDeltaEx(hours=3, minutes=20) + TimeEx(23, 44, 55)
        TimeEx(3, 4, 55)
        >>> TimeDeltaEx(hours=3, minutes=20) + time(23, 44, 55, tzinfo=DummyTZInfo())
        TimeEx(3, 4, 55, tzinfo=<DummyTZInfo>)
        >>> TimeDeltaEx(hours=3, minutes=20) + TimeEx(23, 44, 55, tzinfo=DummyTZInfo())
        TimeEx(3, 4, 55, tzinfo=<DummyTZInfo>)

        >>> TimeDeltaEx(3, 14, 15, 92) + timedelta(2, 71, 82, 81)
        TimeDeltaEx(5, 85, 173097)
        >>> timedelta(2, 71, 82, 81) + TimeDeltaEx(3, 14, 15, 92)
        TimeDeltaEx(5, 85, 173097)

        @type summand: date, datetime, time, timedelta
        @rtype: TimeDeltaEx
        """
        assert isinstance(summand, (date, datetime, time, timedelta)), \
               repr(summand)

        if isinstance(summand, date):
            # TODO
            raise NotImplementedError("Not yet implemented!")
        elif isinstance(summand, datetime):
            # TODO
            raise NotImplementedError("Not yet implemented!")
        elif isinstance(summand, time):
            return TimeEx.from_microseconds(self.in_microseconds + t_to_mus(summand),
                                            tzinfo=summand.tzinfo)
        elif isinstance(summand, timedelta):
            return TimeDeltaEx.from_microseconds(td_to_mus(self) + td_to_mus(summand))
        else:
            raise NotImplementedError("{0!r} + {1!r}".format(self, summand))