Beispiel #1
0
    def range_check_interval(cls, start=None, end=None, duration=None):
        #Handles concise format, range checks any potential durations
        if start is not None and end is not None:
            #<start>/<end>
            #Handle concise format
            if cls._is_interval_end_concise(end) is True:
                end = cls._combine_concise_interval_tuples(start, end)

            return (start, end, duration)

        durationobject = cls._build_object(duration)

        if end is not None:
            #<duration>/<end>
            endobject = cls._build_object(end)

            #Range check
            if type(end) is DateTuple:
                enddatetime = cls.build_datetime(end,
                                                 TupleBuilder.build_time())

                if enddatetime - datetime.datetime.min < durationobject:
                    raise YearOutOfBoundsError(
                        'Interval end less than minimium date.')
            else:
                mindatetime = datetime.datetime.min

                if end.time.tz is not None:
                    mindatetime = mindatetime.replace(tzinfo=endobject.tzinfo)

                if endobject - mindatetime < durationobject:
                    raise YearOutOfBoundsError(
                        'Interval end less than minimium date.')
        else:
            #<start>/<duration>
            startobject = cls._build_object(start)

            #Range check
            if type(start) is DateTuple:
                startdatetime = cls.build_datetime(start,
                                                   TupleBuilder.build_time())

                if datetime.datetime.max - startdatetime < durationobject:
                    raise YearOutOfBoundsError(
                        'Interval end greater than maximum date.')
            else:
                maxdatetime = datetime.datetime.max

                if start.time.tz is not None:
                    maxdatetime = maxdatetime.replace(
                        tzinfo=startobject.tzinfo)

                if maxdatetime - startobject < durationobject:
                    raise YearOutOfBoundsError(
                        'Interval end greater than maximum date.')

        return (start, end, duration)
Beispiel #2
0
    def test_build_time(self):
        testtuples = (
            ({}, (None, None, None, None, "time")),
            (
                {
                    "hh": "1",
                    "mm": "2",
                    "ss": "3",
                    "tz": None
                },
                ("1", "2", "3", None, "time"),
            ),
            (
                {
                    "hh": "1",
                    "mm": "2",
                    "ss": "3",
                    "tz": (False, False, "4", "5", "tz name", "timezone"),
                },
                (
                    "1",
                    "2",
                    "3",
                    (False, False, "4", "5", "tz name", "timezone"),
                    "time",
                ),
            ),
        )

        for testtuple in testtuples:
            self.assertEqual(TupleBuilder.build_time(**testtuple[0]),
                             testtuple[1])
Beispiel #3
0
    def test_build_time(self):
        testtuples = (({}, TimeTuple(None, None, None, None)), ({
            'hh': '1',
            'mm': '2',
            'ss': '3',
            'tz': None
        }, TimeTuple('1', '2', '3',
                     None)), ({
                         'hh':
                         '1',
                         'mm':
                         '2',
                         'ss':
                         '3',
                         'tz':
                         TimezoneTuple(False, False, '4', '5', 'tz name')
                     },
                              TimeTuple(
                                  '1', '2', '3',
                                  TimezoneTuple(False, False, '4', '5',
                                                'tz name'))))

        for testtuple in testtuples:
            self.assertEqual(TupleBuilder.build_time(**testtuple[0]),
                             testtuple[1])
Beispiel #4
0
    def build_interval(cls, start=None, end=None, duration=None):
        start, end, duration = cls.range_check_interval(start, end, duration)

        if start is not None and end is not None:
            # <start>/<end>
            startobject = cls._build_object(start)
            endobject = cls._build_object(end)

            return (startobject, endobject)

        durationobject = cls._build_object(duration)

        # Determine if datetime promotion is required
        datetimerequired = (duration.TnH is not None
                            or duration.TnM is not None
                            or duration.TnS is not None
                            or durationobject.seconds != 0
                            or durationobject.microseconds != 0)

        if end is not None:
            # <duration>/<end>
            endobject = cls._build_object(end)

            # Range check
            if type(end) is DateTuple and datetimerequired is True:
                # <end> is a date, and <duration> requires datetime resolution
                return (
                    endobject,
                    cls.build_datetime(end, TupleBuilder.build_time()) -
                    durationobject,
                )

            return (endobject, endobject - durationobject)

        # <start>/<duration>
        startobject = cls._build_object(start)

        # Range check
        if type(start) is DateTuple and datetimerequired is True:
            # <start> is a date, and <duration> requires datetime resolution
            return (
                startobject,
                cls.build_datetime(start, TupleBuilder.build_time()) +
                durationobject,
            )

        return (startobject, startobject + durationobject)
Beispiel #5
0
    def build_interval(cls, start=None, end=None, duration=None):
        if start is not None and end is not None:
            # <start>/<end>
            startobject = cls._build_object(start)
            endobject = cls._build_object(end)

            return (startobject, endobject)

        durationobject = cls._build_object(duration)

        # Determine if datetime promotion is required
        datetimerequired = (
            duration[4] is not None
            or duration[5] is not None
            or duration[6] is not None
            or durationobject.seconds != 0
            or durationobject.microseconds != 0
        )

        if end is not None:
            # <duration>/<end>
            endobject = cls._build_object(end)
            if end[-1] == "date" and datetimerequired is True:
                # <end> is a date, and <duration> requires datetime resolution
                return (
                    endobject,
                    cls.build_datetime(end, TupleBuilder.build_time()) - durationobject,
                )

            return (endobject, endobject - durationobject)

        # <start>/<duration>
        startobject = cls._build_object(start)

        if start[-1] == "date" and datetimerequired is True:
            # <start> is a date, and <duration> requires datetime resolution
            return (
                startobject,
                cls.build_datetime(start, TupleBuilder.build_time()) + durationobject,
            )

        return (startobject, startobject + durationobject)