示例#1
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))
示例#2
0
    def in_microseconds(self):
        """
        The number of microseconds elapsed since the midnight.

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

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

        >>> TimeEx(3, 14, 15, 92).in_microseconds
        11655000092

        >>> # Test µs_to_td() in Python 3.x only
        >>> not _PY3K or eval("TimeEx(3, 14, 15, 92).in_µs == 11655000092")
        True

        @rtype: numbers.Number
        """
        return t_to_mus(self)
示例#3
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))