def test_parse_timezone_mockbuilder(self):
        mockBuilder = mock.Mock()

        expectedargs = {'negative': False, 'Z': True, 'name': 'Z'}

        mockBuilder.build_timezone.return_value = expectedargs

        result = parse_timezone('Z', builder=mockBuilder)

        self.assertEqual(result, expectedargs)
        mockBuilder.build_timezone.assert_called_once_with(**expectedargs)

        mockBuilder = mock.Mock()

        expectedargs = {'negative': False, 'hh': '00', 'mm': '00',
                        'name': '+00:00'}

        mockBuilder.build_timezone.return_value = expectedargs

        result = parse_timezone('+00:00', builder=mockBuilder)

        self.assertEqual(result, expectedargs)
        mockBuilder.build_timezone.assert_called_once_with(**expectedargs)

        mockBuilder = mock.Mock()

        expectedargs = {'negative': True, 'hh': '01', 'mm': '23',
                        'name': '-01:23'}

        mockBuilder.build_timezone.return_value = expectedargs

        result = parse_timezone('-01:23', builder=mockBuilder)

        self.assertEqual(result, expectedargs)
        mockBuilder.build_timezone.assert_called_once_with(**expectedargs)
    def test_parse_timezone_mockbuilder(self):
        mockBuilder = mock.Mock()

        expectedargs = {'negative': False, 'Z': True, 'name': 'Z'}

        mockBuilder.build_timezone.return_value = expectedargs

        result = parse_timezone('Z', builder=mockBuilder)

        self.assertEqual(result, expectedargs)
        mockBuilder.build_timezone.assert_called_once_with(**expectedargs)

        mockBuilder = mock.Mock()

        expectedargs = {'negative': False, 'hh': '00', 'mm': '00',
                        'name': '+00:00'}

        mockBuilder.build_timezone.return_value = expectedargs

        result = parse_timezone('+00:00', builder=mockBuilder)

        self.assertEqual(result, expectedargs)
        mockBuilder.build_timezone.assert_called_once_with(**expectedargs)

        mockBuilder = mock.Mock()

        expectedargs = {'negative': True, 'hh': '01', 'mm': '23',
                        'name': '-01:23'}

        mockBuilder.build_timezone.return_value = expectedargs

        result = parse_timezone('-01:23', builder=mockBuilder)

        self.assertEqual(result, expectedargs)
        mockBuilder.build_timezone.assert_called_once_with(**expectedargs)
Beispiel #3
0
    def test_parse_timezone_badstr(self):
        testtuples = ('+1', '-00', '-0000', '-00:00',
                      '01', '0123', '@12:34',
                      'Y', ' Z', 'Z ', ' Z ',
                      'bad', '')

        for testtuple in testtuples:
            with self.assertRaises(ISOFormatError):
                parse_timezone(testtuple, builder=None)
Beispiel #4
0
 def test_datetime_tzinfo_dst(self):
     tzinfoobject = parse_timezone('+04:00')
     #This would raise ValueError or a TypeError if dst info is invalid
     result = datetime.datetime.now(tzinfoobject)
     #Hacky way to make sure the tzinfo is what we'd expect
     self.assertEqual(result.tzinfo.utcoffset(None),
                      datetime.timedelta(hours=4))
    def test_parse_timezone_badstr(self):
        with self.assertRaises(ISOFormatError):
            parse_timezone('Y', builder=None)

        with self.assertRaises(ISOFormatError):
            parse_timezone(' Z', builder=None)

        with self.assertRaises(ISOFormatError):
            parse_timezone('Z ', builder=None)

        with self.assertRaises(ISOFormatError):
            parse_timezone(' Z ', builder=None)
Beispiel #6
0
    def test_parse_timezone_tzstr(self):
        with self.assertRaises(ISOFormatError):
            parse_timezone('Y')

        with self.assertRaises(ISOFormatError):
            parse_timezone(' Z')

        with self.assertRaises(ISOFormatError):
            parse_timezone('Z ')

        with self.assertRaises(ISOFormatError):
            parse_timezone(' Z ')
Beispiel #7
0
    def test_parse_timezone_badstr(self):
        with self.assertRaises(ISOFormatError):
            parse_timezone('Y', builder=None)

        with self.assertRaises(ISOFormatError):
            parse_timezone(' Z', builder=None)

        with self.assertRaises(ISOFormatError):
            parse_timezone('Z ', builder=None)

        with self.assertRaises(ISOFormatError):
            parse_timezone(' Z ', builder=None)
Beispiel #8
0
    def test_parse_timezone_mockbuilder(self):
        mockBuilder = mock.Mock()

        expectedargs = {"negative": False, "Z": True, "name": "Z"}

        mockBuilder.build_timezone.return_value = expectedargs

        result = parse_timezone("Z", builder=mockBuilder)

        self.assertEqual(result, expectedargs)
        mockBuilder.build_timezone.assert_called_once_with(**expectedargs)

        mockBuilder = mock.Mock()

        expectedargs = {
            "negative": False,
            "hh": "00",
            "mm": "00",
            "name": "+00:00"
        }

        mockBuilder.build_timezone.return_value = expectedargs

        result = parse_timezone("+00:00", builder=mockBuilder)

        self.assertEqual(result, expectedargs)
        mockBuilder.build_timezone.assert_called_once_with(**expectedargs)

        mockBuilder = mock.Mock()

        expectedargs = {
            "negative": True,
            "hh": "01",
            "mm": "23",
            "name": "-01:23"
        }

        mockBuilder.build_timezone.return_value = expectedargs

        result = parse_timezone("-01:23", builder=mockBuilder)

        self.assertEqual(result, expectedargs)
        mockBuilder.build_timezone.assert_called_once_with(**expectedargs)
    def test_parse_timezone_badstr(self):
        testtuples = (
            "+1",
            "-00",
            "-0000",
            "-00:00",
            "01",
            "0123",
            "@12:34",
            "Y",
            " Z",
            "Z ",
            " Z ",
            "bad",
            "",
        )

        for testtuple in testtuples:
            with self.assertRaises(ISOFormatError):
                parse_timezone(testtuple, builder=None)
Beispiel #10
0
    def test_iso8601_conversion(self):
        field = micromodels.DateTimeField()
        field.populate("2010-07-13T14:01:00Z")
        result = field.to_python()
        expected = datetime.datetime(2010, 7, 13, 14, 1, 0,
                                     tzinfo=parse_timezone('+00:00'))
        self.assertEqual(expected, result)

        field = micromodels.DateTimeField()
        field.populate("2010-07-13T14:02:00-05:00")
        result = field.to_python()
        expected = datetime.datetime(2010, 7, 13, 14, 2, 0,
                                     tzinfo=parse_timezone('-05:00'))

        self.assertEqual(expected, result)

        field = micromodels.DateTimeField()
        field.populate("20100713T140200-05:00")
        result = field.to_python()
        expected = datetime.datetime(2010, 7, 13, 14, 2, 0,
                                     tzinfo=parse_timezone('-05:00'))

        self.assertEqual(expected, result)
Beispiel #11
0
    def test_parse_timezone_negativezero(self):
        #A 0 offset cannot be negative
        with self.assertRaises(ISOFormatError):
            parse_timezone('-00:00')

        with self.assertRaises(ISOFormatError):
            parse_timezone('-0000')

        with self.assertRaises(ISOFormatError):
            parse_timezone('-00')
    def test_parse_timezone_negativezero(self):
        #A 0 offset cannot be negative
        with self.assertRaises(ISOFormatError):
            parse_timezone('-00:00', builder=None)

        with self.assertRaises(ISOFormatError):
            parse_timezone('-0000', builder=None)

        with self.assertRaises(ISOFormatError):
            parse_timezone('-00', builder=None)
    def test_parse_timezone(self):
        testtuples = (('Z', {'negative': False, 'Z': True, 'name': 'Z'}),
                      ('+00:00', {'negative': False, 'hh': '00', 'mm': '00',
                                  'name': '+00:00'}),
                      ('+01:00', {'negative': False, 'hh': '01', 'mm': '00',
                                  'name': '+01:00'}),
                      ('-01:00', {'negative': True, 'hh': '01', 'mm': '00',
                                  'name': '-01:00'}),
                      ('+00:12', {'negative': False, 'hh': '00', 'mm': '12',
                                  'name': '+00:12'}),
                      ('+01:23', {'negative': False, 'hh': '01', 'mm': '23',
                                  'name': '+01:23'}),
                      ('-01:23', {'negative': True, 'hh': '01', 'mm': '23',
                                  'name': '-01:23'}),
                      ('+0000', {'negative': False, 'hh': '00', 'mm': '00',
                                 'name': '+0000'}),
                      ('+0100', {'negative': False, 'hh': '01', 'mm': '00',
                                 'name': '+0100'}),
                      ('-0100', {'negative': True, 'hh': '01', 'mm': '00',
                                 'name': '-0100'}),
                      ('+0012', {'negative': False, 'hh': '00', 'mm': '12',
                                 'name': '+0012'}),
                      ('+0123', {'negative': False, 'hh': '01', 'mm': '23',
                                 'name': '+0123'}),
                      ('-0123', {'negative': True, 'hh': '01', 'mm': '23',
                                 'name': '-0123'}),
                      ('+00', {'negative': False, 'hh': '00', 'mm': None,
                               'name': '+00'}),
                      ('+01', {'negative': False, 'hh': '01', 'mm': None,
                               'name': '+01'}),
                      ('-01', {'negative': True, 'hh': '01', 'mm': None,
                               'name': '-01'}),
                      ('+12', {'negative': False, 'hh': '12', 'mm': None,
                               'name': '+12'}),
                      ('-12', {'negative': True, 'hh': '12', 'mm': None,
                               'name': '-12'}))

        for testtuple in testtuples:
            with mock.patch.object(aniso8601.builder.PythonTimeBuilder,
                                   'build_timezone') as mockBuildTimezone:

                mockBuildTimezone.return_value = testtuple[1]

                result = parse_timezone(testtuple[0])

                self.assertEqual(result, testtuple[1])
                mockBuildTimezone.assert_called_once_with(**testtuple[1])
    def test_parse_timezone(self):
        testtuples = (('Z', {'negative': False, 'Z': True, 'name': 'Z'}),
                      ('+00:00', {'negative': False, 'hh': '00', 'mm': '00',
                                  'name': '+00:00'}),
                      ('+01:00', {'negative': False, 'hh': '01', 'mm': '00',
                                  'name': '+01:00'}),
                      ('-01:00', {'negative': True, 'hh': '01', 'mm': '00',
                                  'name': '-01:00'}),
                      ('+00:12', {'negative': False, 'hh': '00', 'mm': '12',
                                  'name': '+00:12'}),
                      ('+01:23', {'negative': False, 'hh': '01', 'mm': '23',
                                  'name': '+01:23'}),
                      ('-01:23', {'negative': True, 'hh': '01', 'mm': '23',
                                  'name': '-01:23'}),
                      ('+0000', {'negative': False, 'hh': '00', 'mm': '00',
                                 'name': '+0000'}),
                      ('+0100', {'negative': False, 'hh': '01', 'mm': '00',
                                 'name': '+0100'}),
                      ('-0100', {'negative': True, 'hh': '01', 'mm': '00',
                                 'name': '-0100'}),
                      ('+0012', {'negative': False, 'hh': '00', 'mm': '12',
                                 'name': '+0012'}),
                      ('+0123', {'negative': False, 'hh': '01', 'mm': '23',
                                 'name': '+0123'}),
                      ('-0123', {'negative': True, 'hh': '01', 'mm': '23',
                                 'name': '-0123'}),
                      ('+00', {'negative': False, 'hh': '00', 'mm': None,
                               'name': '+00'}),
                      ('+01', {'negative': False, 'hh': '01', 'mm': None,
                               'name': '+01'}),
                      ('-01', {'negative': True, 'hh': '01', 'mm': None,
                               'name': '-01'}),
                      ('+12', {'negative': False, 'hh': '12', 'mm': None,
                               'name': '+12'}),
                      ('-12', {'negative': True, 'hh': '12', 'mm': None,
                               'name': '-12'}))

        for testtuple in testtuples:
            with mock.patch.object(aniso8601.timezone.PythonTimeBuilder,
                                   'build_timezone') as mockBuildTimezone:

                mockBuildTimezone.return_value = testtuple[1]

                result = parse_timezone(testtuple[0])

                self.assertEqual(result, testtuple[1])
                mockBuildTimezone.assert_called_once_with(**testtuple[1])
Beispiel #15
0
def parse_time(isotimestr, builder=PythonTimeBuilder):
    #Given a string in any ISO 8601 time format, return a datetime.time object
    #that corresponds to the given time. Fixed offset tzdata will be included
    #if UTC offset is given in the input string. Valid time formats are:
    #
    #hh:mm:ss
    #hhmmss
    #hh:mm
    #hhmm
    #hh
    #hh:mm:ssZ
    #hhmmssZ
    #hh:mmZ
    #hhmmZ
    #hhZ
    #hh:mm:ss±hh:mm
    #hhmmss±hh:mm
    #hh:mm±hh:mm
    #hhmm±hh:mm
    #hh±hh:mm
    #hh:mm:ss±hhmm
    #hhmmss±hhmm
    #hh:mm±hhmm
    #hhmm±hhmm
    #hh±hhmm
    #hh:mm:ss±hh
    #hhmmss±hh
    #hh:mm±hh
    #hhmm±hh
    #hh±hh

    (timestr, tzstr) = _split_tz(isotimestr)

    if timestr[0].isdigit() is False or timestr[-1].isdigit() is False:
        raise ISOFormatError('"{0}" is not a valid ISO 8601 time.'
                             .format(timestr))

    if tzstr is None:
        tz = None
    else:
        tz = parse_timezone(tzstr, builder=TupleBuilder)

    return _RESOLUTION_MAP[get_time_resolution(timestr)](timestr, tz, builder)
Beispiel #16
0
def parse_time(isotimestr, builder=PythonTimeBuilder):
    # Given a string in any ISO 8601 time format, return a datetime.time object
    # that corresponds to the given time. Fixed offset tzdata will be included
    # if UTC offset is given in the input string. Valid time formats are:
    #
    # hh:mm:ss
    # hhmmss
    # hh:mm
    # hhmm
    # hh
    # hh:mm:ssZ
    # hhmmssZ
    # hh:mmZ
    # hhmmZ
    # hhZ
    # hh:mm:ss±hh:mm
    # hhmmss±hh:mm
    # hh:mm±hh:mm
    # hhmm±hh:mm
    # hh±hh:mm
    # hh:mm:ss±hhmm
    # hhmmss±hhmm
    # hh:mm±hhmm
    # hhmm±hhmm
    # hh±hhmm
    # hh:mm:ss±hh
    # hhmmss±hh
    # hh:mm±hh
    # hhmm±hh
    # hh±hh

    (timestr, tzstr) = _split_tz(isotimestr)

    if timestr[0].isdigit() is False or timestr[-1].isdigit() is False:
        raise ISOFormatError(
            '"{0}" is not a valid ISO 8601 time.'.format(timestr))

    if tzstr is None:
        tz = None
    else:
        tz = parse_timezone(tzstr, builder=TupleBuilder)

    return _RESOLUTION_MAP[get_time_resolution(timestr)](timestr, tz, builder)
Beispiel #17
0
def parse_time(isotimestr):
    #Given a string in any ISO8601 time format, return a datetime.time object
    #that corresponds to the given time. Fixed offset tzdata will be included
    #if UTC offset is given in the input string. Valid time formats are:
    #
    #hh:mm:ss
    #hhmmss
    #hh:mm
    #hhmm
    #hh
    #hh:mm:ssZ
    #hhmmssZ
    #hh:mmZ
    #hhmmZ
    #hhZ
    #hh:mm:ss±hh:mm
    #hhmmss±hh:mm
    #hh:mm±hh:mm
    #hhmm±hh:mm
    #hh±hh:mm
    #hh:mm:ss±hhmm
    #hhmmss±hhmm
    #hh:mm±hhmm
    #hhmm±hhmm
    #hh±hhmm
    #hh:mm:ss±hh
    #hhmmss±hh
    #hh:mm±hh
    #hhmm±hh
    #hh±hh

    (timestr, tzstr) = _split_tz(isotimestr)

    if tzstr == None:
        return _parse_time_naive(timestr)
    elif tzstr == 'Z':
        return _parse_time_naive(timestr).replace(
            tzinfo=build_utcoffset('UTC', datetime.timedelta(hours=0)))
    else:
        return _parse_time_naive(timestr).replace(tzinfo=parse_timezone(tzstr))
Beispiel #18
0
def parse_time(isotimestr, builder=PythonTimeBuilder):
    #Given a string in any ISO 8601 time format, return a datetime.time object
    #that corresponds to the given time. Fixed offset tzdata will be included
    #if UTC offset is given in the input string. Valid time formats are:
    #
    #hh:mm:ss
    #hhmmss
    #hh:mm
    #hhmm
    #hh
    #hh:mm:ssZ
    #hhmmssZ
    #hh:mmZ
    #hhmmZ
    #hhZ
    #hh:mm:ss±hh:mm
    #hhmmss±hh:mm
    #hh:mm±hh:mm
    #hhmm±hh:mm
    #hh±hh:mm
    #hh:mm:ss±hhmm
    #hhmmss±hhmm
    #hh:mm±hhmm
    #hhmm±hhmm
    #hh±hhmm
    #hh:mm:ss±hh
    #hhmmss±hh
    #hh:mm±hh
    #hhmm±hh
    #hh±hh
    timeresolution = get_time_resolution(isotimestr)

    (timestr, tzstr) = _split_tz(isotimestr)

    if tzstr is None:
        tz = None
    else:
        tz = parse_timezone(tzstr, builder=TupleBuilder)

    return _RESOLUTION_MAP[timeresolution](timestr, tz, builder)
Beispiel #19
0
def parse_time(isotimestr):
    #Given a string in any ISO 8601 time format, return a datetime.time object
    #that corresponds to the given time. Fixed offset tzdata will be included
    #if UTC offset is given in the input string. Valid time formats are:
    #
    #hh:mm:ss
    #hhmmss
    #hh:mm
    #hhmm
    #hh
    #hh:mm:ssZ
    #hhmmssZ
    #hh:mmZ
    #hhmmZ
    #hhZ
    #hh:mm:ss±hh:mm
    #hhmmss±hh:mm
    #hh:mm±hh:mm
    #hhmm±hh:mm
    #hh±hh:mm
    #hh:mm:ss±hhmm
    #hhmmss±hhmm
    #hh:mm±hhmm
    #hhmm±hhmm
    #hh±hhmm
    #hh:mm:ss±hh
    #hhmmss±hh
    #hh:mm±hh
    #hhmm±hh
    #hh±hh

    (timestr, tzstr) = _split_tz(isotimestr)

    if tzstr is None:
        return _parse_time_naive(timestr)
    elif tzstr == 'Z':
        return _parse_time_naive(timestr).replace(tzinfo=build_utcoffset('UTC', datetime.timedelta(hours=0)))
    else:
        return _parse_time_naive(timestr).replace(tzinfo=parse_timezone(tzstr))
Beispiel #20
0
    def test_parse_timezone(self):
        tzinfoobject = parse_timezone('Z')
        self.assertEqual(tzinfoobject.utcoffset(None), datetime.timedelta(hours=0))
        self.assertEqual(tzinfoobject.tzname(None), 'UTC')

        tzinfoobject = parse_timezone('+00:00')
        self.assertEqual(tzinfoobject.utcoffset(None), datetime.timedelta(hours=0))
        self.assertEqual(tzinfoobject.tzname(None), '+00:00')

        tzinfoobject = parse_timezone('+01:00')
        self.assertEqual(tzinfoobject.utcoffset(None), datetime.timedelta(hours=1))
        self.assertEqual(tzinfoobject.tzname(None), '+01:00')

        tzinfoobject = parse_timezone('-01:00')
        self.assertEqual(tzinfoobject.utcoffset(None), -datetime.timedelta(hours=1))
        self.assertEqual(tzinfoobject.tzname(None), '-01:00')

        tzinfoobject = parse_timezone('+00:12')
        self.assertEqual(tzinfoobject.utcoffset(None), datetime.timedelta(minutes=12))
        self.assertEqual(tzinfoobject.tzname(None), '+00:12')

        tzinfoobject = parse_timezone('+01:23')
        self.assertEqual(tzinfoobject.utcoffset(None), datetime.timedelta(hours=1, minutes=23))
        self.assertEqual(tzinfoobject.tzname(None), '+01:23')

        tzinfoobject = parse_timezone('-01:23')
        self.assertEqual(tzinfoobject.utcoffset(None), -datetime.timedelta(hours=1, minutes=23))
        self.assertEqual(tzinfoobject.tzname(None), '-01:23')

        tzinfoobject = parse_timezone('+0000')
        self.assertEqual(tzinfoobject.utcoffset(None), datetime.timedelta(hours=0))
        self.assertEqual(tzinfoobject.tzname(None), '+0000')

        tzinfoobject = parse_timezone('+0100')
        self.assertEqual(tzinfoobject.utcoffset(None), datetime.timedelta(hours=1))
        self.assertEqual(tzinfoobject.tzname(None), '+0100')

        tzinfoobject = parse_timezone('-0100')
        self.assertEqual(tzinfoobject.utcoffset(None), -datetime.timedelta(hours=1))
        self.assertEqual(tzinfoobject.tzname(None), '-0100')

        tzinfoobject = parse_timezone('+0012')
        self.assertEqual(tzinfoobject.utcoffset(None), datetime.timedelta(minutes=12))
        self.assertEqual(tzinfoobject.tzname(None), '+0012')

        tzinfoobject = parse_timezone('+0123')
        self.assertEqual(tzinfoobject.utcoffset(None), datetime.timedelta(hours=1, minutes=23))
        self.assertEqual(tzinfoobject.tzname(None), '+0123')

        tzinfoobject = parse_timezone('-0123')
        self.assertEqual(tzinfoobject.utcoffset(None), -datetime.timedelta(hours=1, minutes=23))
        self.assertEqual(tzinfoobject.tzname(None), '-0123')

        tzinfoobject = parse_timezone('+00')
        self.assertEqual(tzinfoobject.utcoffset(None), datetime.timedelta(hours=0))
        self.assertEqual(tzinfoobject.tzname(None), '+00')

        tzinfoobject = parse_timezone('+01')
        self.assertEqual(tzinfoobject.utcoffset(None), datetime.timedelta(hours=1))
        self.assertEqual(tzinfoobject.tzname(None), '+01')

        tzinfoobject = parse_timezone('-01')
        self.assertEqual(tzinfoobject.utcoffset(None), -datetime.timedelta(hours=1))
        self.assertEqual(tzinfoobject.tzname(None), '-01')

        tzinfoobject = parse_timezone('+12')
        self.assertEqual(tzinfoobject.utcoffset(None), datetime.timedelta(hours=12))
        self.assertEqual(tzinfoobject.tzname(None), '+12')

        tzinfoobject = parse_timezone('-12')
        self.assertEqual(tzinfoobject.utcoffset(None), -datetime.timedelta(hours=12))
        self.assertEqual(tzinfoobject.tzname(None), '-12')
Beispiel #21
0
    def test_string_representation(self):
        #Make sure UTC offsets can be printed out prettily
        tzinfoobject = parse_timezone('+00:00')
        self.assertEqual(str(tzinfoobject), '+0:00:00 UTC')

        tzinfoobject = parse_timezone('+01:00')
        self.assertEqual(str(tzinfoobject), '+1:00:00 UTC')

        tzinfoobject = parse_timezone('-01:00')
        self.assertEqual(str(tzinfoobject), '-1:00:00 UTC')

        tzinfoobject = parse_timezone('+00:12')
        self.assertEqual(str(tzinfoobject), '+0:12:00 UTC')

        tzinfoobject = parse_timezone('-00:12')
        self.assertEqual(str(tzinfoobject), '-0:12:00 UTC')

        tzinfoobject = parse_timezone('+01:23')
        self.assertEqual(str(tzinfoobject), '+1:23:00 UTC')

        tzinfoobject = parse_timezone('-01:23')
        self.assertEqual(str(tzinfoobject), '-1:23:00 UTC')

        tzinfoobject = parse_timezone('+24:00')
        self.assertEqual(str(tzinfoobject), '+1 day, 0:00:00 UTC')

        tzinfoobject = parse_timezone('-24:00')
        self.assertEqual(str(tzinfoobject), '-1 day, 0:00:00 UTC')

        tzinfoobject = parse_timezone('+49:27')
        self.assertEqual(str(tzinfoobject), '+2 days, 1:27:00 UTC')

        tzinfoobject = parse_timezone('-49:27')
        self.assertEqual(str(tzinfoobject), '-2 days, 1:27:00 UTC')
    def test_string_representation(self):
        #Make sure UTC offsets can be printed out prettily
        tzinfoobject = parse_timezone('+00:00')
        self.assertEqual(str(tzinfoobject), '+0:00:00 UTC')

        tzinfoobject = parse_timezone('+01:00')
        self.assertEqual(str(tzinfoobject), '+1:00:00 UTC')

        tzinfoobject = parse_timezone('-01:00')
        self.assertEqual(str(tzinfoobject), '-1:00:00 UTC')

        tzinfoobject = parse_timezone('+00:12')
        self.assertEqual(str(tzinfoobject), '+0:12:00 UTC')

        tzinfoobject = parse_timezone('-00:12')
        self.assertEqual(str(tzinfoobject), '-0:12:00 UTC')

        tzinfoobject = parse_timezone('+01:23')
        self.assertEqual(str(tzinfoobject), '+1:23:00 UTC')

        tzinfoobject = parse_timezone('-01:23')
        self.assertEqual(str(tzinfoobject), '-1:23:00 UTC')

        tzinfoobject = parse_timezone('+24:00')
        self.assertEqual(str(tzinfoobject), '+1 day, 0:00:00 UTC')

        tzinfoobject = parse_timezone('-24:00')
        self.assertEqual(str(tzinfoobject), '-1 day, 0:00:00 UTC')

        tzinfoobject = parse_timezone('+49:27')
        self.assertEqual(str(tzinfoobject), '+2 days, 1:27:00 UTC')

        tzinfoobject = parse_timezone('-49:27')
        self.assertEqual(str(tzinfoobject), '-2 days, 1:27:00 UTC')
Beispiel #23
0
    def test_parse_timezone_badtype(self):
        testtuples = (None, 1, False, 1.234)

        for testtuple in testtuples:
            with self.assertRaises(ValueError):
                parse_timezone(testtuple, builder=None)
Beispiel #24
0
    def test_parse_timezone(self):
        testtuples = (
            ("Z", {
                "negative": False,
                "Z": True,
                "name": "Z"
            }),
            ("+00:00", {
                "negative": False,
                "hh": "00",
                "mm": "00",
                "name": "+00:00"
            }),
            ("+01:00", {
                "negative": False,
                "hh": "01",
                "mm": "00",
                "name": "+01:00"
            }),
            ("-01:00", {
                "negative": True,
                "hh": "01",
                "mm": "00",
                "name": "-01:00"
            }),
            ("+00:12", {
                "negative": False,
                "hh": "00",
                "mm": "12",
                "name": "+00:12"
            }),
            ("+01:23", {
                "negative": False,
                "hh": "01",
                "mm": "23",
                "name": "+01:23"
            }),
            ("-01:23", {
                "negative": True,
                "hh": "01",
                "mm": "23",
                "name": "-01:23"
            }),
            ("+0000", {
                "negative": False,
                "hh": "00",
                "mm": "00",
                "name": "+0000"
            }),
            ("+0100", {
                "negative": False,
                "hh": "01",
                "mm": "00",
                "name": "+0100"
            }),
            ("-0100", {
                "negative": True,
                "hh": "01",
                "mm": "00",
                "name": "-0100"
            }),
            ("+0012", {
                "negative": False,
                "hh": "00",
                "mm": "12",
                "name": "+0012"
            }),
            ("+0123", {
                "negative": False,
                "hh": "01",
                "mm": "23",
                "name": "+0123"
            }),
            ("-0123", {
                "negative": True,
                "hh": "01",
                "mm": "23",
                "name": "-0123"
            }),
            ("+00", {
                "negative": False,
                "hh": "00",
                "mm": None,
                "name": "+00"
            }),
            ("+01", {
                "negative": False,
                "hh": "01",
                "mm": None,
                "name": "+01"
            }),
            ("-01", {
                "negative": True,
                "hh": "01",
                "mm": None,
                "name": "-01"
            }),
            ("+12", {
                "negative": False,
                "hh": "12",
                "mm": None,
                "name": "+12"
            }),
            ("-12", {
                "negative": True,
                "hh": "12",
                "mm": None,
                "name": "-12"
            }),
        )

        for testtuple in testtuples:
            with mock.patch.object(aniso8601.timezone.PythonTimeBuilder,
                                   "build_timezone") as mockBuildTimezone:

                mockBuildTimezone.return_value = testtuple[1]

                result = parse_timezone(testtuple[0])

                self.assertEqual(result, testtuple[1])
                mockBuildTimezone.assert_called_once_with(**testtuple[1])
 def test_datetime_tzinfo_dst(self):
     tzinfoobject = parse_timezone('+04:00')
     datetime.datetime.now(tzinfoobject)
     # Will raise ValueError or a TypeError if dst info is invalid
     assert True
Beispiel #26
0
def parse_time(isotimestr, builder=PythonTimeBuilder):
    # Given a string in any ISO 8601 time format, return a datetime.time object
    # that corresponds to the given time. Fixed offset tzdata will be included
    # if UTC offset is given in the input string. Valid time formats are:
    #
    # hh:mm:ss
    # hhmmss
    # hh:mm
    # hhmm
    # hh
    # hh:mm:ssZ
    # hhmmssZ
    # hh:mmZ
    # hhmmZ
    # hhZ
    # hh:mm:ss±hh:mm
    # hhmmss±hh:mm
    # hh:mm±hh:mm
    # hhmm±hh:mm
    # hh±hh:mm
    # hh:mm:ss±hhmm
    # hhmmss±hhmm
    # hh:mm±hhmm
    # hhmm±hhmm
    # hh±hhmm
    # hh:mm:ss±hh
    # hhmmss±hh
    # hh:mm±hh
    # hhmm±hh
    # hh±hh
    if is_string(isotimestr) is False:
        raise ValueError("Time must be string.")

    if len(isotimestr) == 0:
        raise ISOFormatError(
            '"{0}" is not a valid ISO 8601 time.'.format(isotimestr))

    timestr = normalize(isotimestr)

    hourstr = None
    minutestr = None
    secondstr = None
    tzstr = None

    fractionalstr = None

    # Split out the timezone
    for delimiter in TIMEZONE_DELIMITERS:
        delimiteridx = timestr.find(delimiter)

        if delimiteridx != -1:
            tzstr = timestr[delimiteridx:]
            timestr = timestr[0:delimiteridx]

    # Split out the fractional component
    if timestr.find(".") != -1:
        timestr, fractionalstr = timestr.split(".", 1)

        if fractionalstr.isdigit() is False:
            raise ISOFormatError(
                '"{0}" is not a valid ISO 8601 time.'.format(isotimestr))

    if len(timestr) == 2:
        # hh
        hourstr = timestr
    elif len(timestr) == 4 or len(timestr) == 5:
        # hh:mm
        # hhmm
        if timestr.count(":") == 1:
            hourstr, minutestr = timestr.split(":")
        else:
            hourstr = timestr[0:2]
            minutestr = timestr[2:]
    elif len(timestr) == 6 or len(timestr) == 8:
        # hh:mm:ss
        # hhmmss
        if timestr.count(":") == 2:
            hourstr, minutestr, secondstr = timestr.split(":")
        else:
            hourstr = timestr[0:2]
            minutestr = timestr[2:4]
            secondstr = timestr[4:]
    else:
        raise ISOFormatError(
            '"{0}" is not a valid ISO 8601 time.'.format(isotimestr))

    for componentstr in [hourstr, minutestr, secondstr]:
        if componentstr is not None and componentstr.isdigit() is False:
            raise ISOFormatError(
                '"{0}" is not a valid ISO 8601 time.'.format(isotimestr))

    if fractionalstr is not None:
        if secondstr is not None:
            secondstr = secondstr + "." + fractionalstr
        elif minutestr is not None:
            minutestr = minutestr + "." + fractionalstr
        else:
            hourstr = hourstr + "." + fractionalstr

    if tzstr is None:
        tz = None
    else:
        tz = parse_timezone(tzstr, builder=TupleBuilder)

    return builder.build_time(hh=hourstr, mm=minutestr, ss=secondstr, tz=tz)
Beispiel #27
0
    def test_parse_timezone(self):
        tzinfoobject = parse_timezone('+00:00')
        self.assertEqual(tzinfoobject.utcoffset(None), datetime.timedelta(hours=0))
        self.assertEqual(tzinfoobject.tzname(None), '+00:00')

        tzinfoobject = parse_timezone('+01:00')
        self.assertEqual(tzinfoobject.utcoffset(None), datetime.timedelta(hours=1))
        self.assertEqual(tzinfoobject.tzname(None), '+01:00')

        tzinfoobject = parse_timezone('-01:00')
        self.assertEqual(tzinfoobject.utcoffset(None), -datetime.timedelta(hours=1))
        self.assertEqual(tzinfoobject.tzname(None), '-01:00')

        tzinfoobject = parse_timezone('+00:12')
        self.assertEqual(tzinfoobject.utcoffset(None), datetime.timedelta(minutes=12))
        self.assertEqual(tzinfoobject.tzname(None), '+00:12')

        tzinfoobject = parse_timezone('+01:23')
        self.assertEqual(tzinfoobject.utcoffset(None), datetime.timedelta(hours=1, minutes=23))
        self.assertEqual(tzinfoobject.tzname(None), '+01:23')

        tzinfoobject = parse_timezone('-01:23')
        self.assertEqual(tzinfoobject.utcoffset(None), -datetime.timedelta(hours=1, minutes=23))
        self.assertEqual(tzinfoobject.tzname(None), '-01:23')

        with self.assertRaises(ValueError):
            parse_timezone('-00:00')

        tzinfoobject = parse_timezone('+0000')
        self.assertEqual(tzinfoobject.utcoffset(None), datetime.timedelta(hours=0))
        self.assertEqual(tzinfoobject.tzname(None), '+0000')

        tzinfoobject = parse_timezone('+0100')
        self.assertEqual(tzinfoobject.utcoffset(None), datetime.timedelta(hours=1))
        self.assertEqual(tzinfoobject.tzname(None), '+0100')

        tzinfoobject = parse_timezone('-0100')
        self.assertEqual(tzinfoobject.utcoffset(None), -datetime.timedelta(hours=1))
        self.assertEqual(tzinfoobject.tzname(None), '-0100')

        tzinfoobject = parse_timezone('+0012')
        self.assertEqual(tzinfoobject.utcoffset(None), datetime.timedelta(minutes=12))
        self.assertEqual(tzinfoobject.tzname(None), '+0012')

        tzinfoobject = parse_timezone('+0123')
        self.assertEqual(tzinfoobject.utcoffset(None), datetime.timedelta(hours=1, minutes=23))
        self.assertEqual(tzinfoobject.tzname(None), '+0123')

        tzinfoobject = parse_timezone('-0123')
        self.assertEqual(tzinfoobject.utcoffset(None), -datetime.timedelta(hours=1, minutes=23))
        self.assertEqual(tzinfoobject.tzname(None), '-0123')

        with self.assertRaises(ValueError):
            parse_timezone('-0000')

        tzinfoobject = parse_timezone('+00')
        self.assertEqual(tzinfoobject.utcoffset(None), datetime.timedelta(hours=0))
        self.assertEqual(tzinfoobject.tzname(None), '+00')

        tzinfoobject = parse_timezone('+01')
        self.assertEqual(tzinfoobject.utcoffset(None), datetime.timedelta(hours=1))
        self.assertEqual(tzinfoobject.tzname(None), '+01')

        tzinfoobject = parse_timezone('-01')
        self.assertEqual(tzinfoobject.utcoffset(None), -datetime.timedelta(hours=1))
        self.assertEqual(tzinfoobject.tzname(None), '-01')

        tzinfoobject = parse_timezone('+12')
        self.assertEqual(tzinfoobject.utcoffset(None), datetime.timedelta(hours=12))
        self.assertEqual(tzinfoobject.tzname(None), '+12')

        tzinfoobject = parse_timezone('-12')
        self.assertEqual(tzinfoobject.utcoffset(None), -datetime.timedelta(hours=12))
        self.assertEqual(tzinfoobject.tzname(None), '-12')

        with self.assertRaises(ValueError):
            parse_timezone('-00')
Beispiel #28
0
 def test_datetime_tzinfo_dst(self):
     tzinfoobject = parse_timezone('+04:00')
     datetime.datetime.now(tzinfoobject)
     # Will raise ValueError or a TypeError if dst info is invalid
     assert True