def _parse_tzstr(self, tzstr, zero_as_utc=True): if tzstr == b'Z' or tzstr == b'z': return tz.UTC if len(tzstr) not in {3, 5, 6}: raise ValueError('Time zone offset must be 1, 3, 5 or 6 characters') if tzstr[0:1] == b'-': mult = -1 elif tzstr[0:1] == b'+': mult = 1 else: raise ValueError('Time zone offset requires sign') hours = int(tzstr[1:3]) if len(tzstr) == 3: minutes = 0 else: minutes = int(tzstr[(4 if tzstr[3:4] == self._TIME_SEP else 3):]) if zero_as_utc and hours == 0 and minutes == 0: return tz.UTC else: if minutes > 59: raise ValueError('Invalid minutes in time zone offset') if hours > 23: raise ValueError('Invalid hours in time zone offset') return tz.tzoffset(None, mult * (hours * 60 + minutes) * 60)
def setup_class(cls): cls.tzinfos = {"BRST": -10800} cls.brsttz = tzoffset("BRST", -10800) cls.default = datetime(2003, 9, 25) # Parser should be able to handle bytestring and unicode cls.uni_str = '2014-05-01 08:00:00' cls.str_str = cls.uni_str.encode()
def testISOStrippedFormatStrip2(self): self.assertEqual(parse("20030925T104941+0300"), datetime(2003, 9, 25, 10, 49, 41, tzinfo=tzoffset(None, 10800)))
def testISOFormatStrip2(self): self.assertEqual(parse("2003-09-25T10:49:41+03:00"), datetime(2003, 9, 25, 10, 49, 41, tzinfo=tzoffset(None, 10800)))
def test_valid_tzinfo_int_input(self): dstr = "2014 January 19 09:00 UTC" tzinfos = {u"UTC": -28800} expected = datetime(2014, 1, 19, 9, tzinfo=tz.tzoffset(u"UTC", -28800)) res = parse(dstr, tzinfos=tzinfos) self.assert_equal_same_tz(res, expected)
("1996.07.10 AD at 15:08:56 PDT", datetime(1996, 7, 10, 15, 8, 56)), ("Tuesday, April 12, 1952 AD 3:30:42pm PST", datetime(1952, 4, 12, 15, 30, 42)), ("November 5, 1994, 8:15:30 am EST", datetime(1994, 11, 5, 8, 15, 30)), ("1994-11-05T08:15:30-05:00", datetime(1994, 11, 5, 8, 15, 30)), ("1994-11-05T08:15:30Z", datetime(1994, 11, 5, 8, 15, 30)), ("1976-07-04T00:01:02Z", datetime(1976, 7, 4, 0, 1, 2)), ("1986-07-05T08:15:30z", datetime(1986, 7, 5, 8, 15, 30)), ("Tue Apr 4 00:22:12 PDT 1995", datetime(1995, 4, 4, 0, 22, 12)), ]) def test_parse_ignoretz(dstr, expected): result = parse(dstr, ignoretz=True) assert result == expected _brsttz = tzoffset("BRST", -10800) @pytest.mark.parametrize('dstr,expected', [ ("20030925T104941-0300", datetime(2003, 9, 25, 10, 49, 41, tzinfo=_brsttz)), ("Thu, 25 Sep 2003 10:49:41 -0300", datetime(2003, 9, 25, 10, 49, 41, tzinfo=_brsttz)), ("2003-09-25T10:49:41.5-03:00", datetime(2003, 9, 25, 10, 49, 41, 500000, tzinfo=_brsttz)), ("2003-09-25T10:49:41-03:00", datetime(2003, 9, 25, 10, 49, 41, tzinfo=_brsttz)), ("20030925T104941.5-0300", datetime(2003, 9, 25, 10, 49, 41, 500000, tzinfo=_brsttz)), ]) def test_parse_with_tzoffset(dstr, expected):
def _mkoffset(hmtuple, fmt): h, m = hmtuple m_td = (-1 if h < 0 else 1) * m tzo = tz.tzoffset(None, timedelta(hours=h, minutes=m_td)) return tzo, fmt.format(h, m)
(b'2014-02-04T12:30:15', datetime(2014, 2, 4, 12, 30, 15)), (b'2014-02-04T12:30:15.224', datetime(2014, 2, 4, 12, 30, 15, 224000)), (b'20140204T123015.224', datetime(2014, 2, 4, 12, 30, 15, 224000)), (b'2014-02-04T12:30:15.224Z', datetime(2014, 2, 4, 12, 30, 15, 224000, UTC)), (b'2014-02-04T12:30:15.224z', datetime(2014, 2, 4, 12, 30, 15, 224000, UTC)), (b'2014-02-04T12:30:15.224+05:00', datetime(2014, 2, 4, 12, 30, 15, 224000, tzinfo=tz.tzoffset(None, timedelta(hours=5))))]) def test_bytes(isostr, dt): assert isoparse(isostr) == dt ### # Invalid ISO strings @pytest.mark.parametrize( 'isostr,exception', [ ('201', ValueError), # ISO string too short ('2012-0425', ValueError), # Inconsistent date separators ('201204-25', ValueError), # Inconsistent date separators ('20120425T0120:00', ValueError), # Inconsistent time separators ('20120425T012500-334', ValueError), # Wrong microsecond separator ('2001-1', ValueError), # YYYY-M not valid