def test_partial_intervals(): assert parsers.interval('2005-03-20T05:15+03:00/23T17:00') == interval(repeats=0, start=datetime_(2005, 3, 20, 5, 15, tzinfo=timezone(hours=3, minutes=0)), end=datetime_(2005, 3, 23, 17, tzinfo=timezone(hours=3, minutes=0))) assert parsers.interval('2007-11-13/15') == interval(repeats=0, start=datetime_(2007, 11, 13, tzinfo=utc), end=datetime_(2007, 11, 16, tzinfo=utc)) assert parsers.interval('2007-11-13T09:00/15T17:00') == interval(repeats=0, start=datetime_(2007, 11, 13, 9, tzinfo=utc), end=datetime_(2007, 11, 15, 17, tzinfo=utc))
def test_tz(): tz = timezone() assert tz.offset == timedelta(hours=0, minutes=0) assert tz.hours == 0 assert tz.minutes == 0 assert tuple(tz) == (0, 0) assert tz.name == tz.tzname(None) assert tz.dst(None) == timedelta(0) assert tz.utcoffset(None) == timedelta(0) assert tz.__repr__() == '+00:00' assert str(tz) == '+00:00' tz = -timezone(hours=5, minutes=20) assert tz.hours == -5 assert tz.minutes == -20 assert tuple(tz) == (-5, -20) assert tz.name == tz.tzname(None) assert tz.dst(None) == timedelta(0) assert tz.utcoffset(None) == -timedelta(hours=5, minutes=20) assert tz.__repr__() == '-05:20' assert str(tz) == '-05:20' tz = -timezone(hours=4, minutes=30, name='test') assert tz.hours == -4 assert tz.minutes == -30 assert tuple(tz) == (-4, -30) assert tz.name == 'test' assert tz.name == tz.tzname(None) assert tz.dst(None) == timedelta(0) assert tz.utcoffset(None) == -timedelta(hours=4, minutes=30) assert tz.__repr__() == 'test' assert str(tz) == 'test'
def time(match): group = match.groupdict() data = {k: int(v or 0) for k, v in group.items() if k != 'sign'} sign = group.get('sign') if sign: hours = data['offset_hour'] minutes = data['offset_minute'] if hours == minutes == 0: raise ValueError('Invalid timezone offset {0}00:00.'.format(sign)) if sign == '+': tz = timezone(hours=hours, minutes=minutes) else: tz = -timezone(hours=hours, minutes=minutes) explicit_tz = True else: explicit_tz = False tz = utc hour, minute = data['hour'], data['minute'] second, millisecond = data['second'], data['millisecond'] day = 0 if hour == 24 and minute == second == millisecond == 0: hour = 0 day = 1 return (time_(hour, minute, second, 1000 * millisecond, tz), day, explicit_tz)
def test_datetime(): assert parsers.datetime('2007-04-05T14:30') == datetime_(2007, 4, 5, 14, 30, tzinfo=utc) assert parsers.datetime('2007-08-09T12:30Z') == datetime_(2007, 8, 9, 12, 30, tzinfo=utc) assert parsers.datetime('2007-01-01T24:00:00') == datetime_(2007, 1, 2, 0, 0, 0, tzinfo=utc) assert parsers.datetime('2007-01-01T24:00:00') == parsers.datetime('2007-01-02T00:00:00') assert parsers.datetime('2007-08-09T12:30-02:00') == datetime_(2007, 8, 9, 12, 30, tzinfo=-timezone(hours=2, minutes=0)) with pytest.raises(ValueError): parsers.datetime('invalid')
def test_time(): with pytest.raises(ValueError): parsers.time('1234a') with pytest.raises(ValueError): parsers.time('1234a') with pytest.raises(ValueError): parsers.time('12:30:40.05+0:15') with pytest.raises(ValueError): parsers.time('1230401.05+10:15') with pytest.raises(ValueError): parsers.time('24:00:00.0001') assert parsers.time('24:00:00') == parsers.time('00:00:00') assert parsers.time('12') == time_(hour=12, tzinfo=utc) assert parsers.time('12+05:10') == time_(hour=12, tzinfo=timezone(hours=5, minutes=10)) assert parsers.time('12-05:10') == time_(hour=12, tzinfo=-timezone(hours=5, minutes=10)) assert parsers.time('13:15') == time_(hour=13, minute=15, tzinfo=utc) assert parsers.time('13:15+05:10') == time_(hour=13, minute=15, tzinfo=timezone(hours=5, minutes=10)) assert parsers.time('13:15-05:10') == time_(hour=13, minute=15, tzinfo=-timezone(hours=5, minutes=10)) assert parsers.time('14:20:50') == time_(hour=14, minute=20, second=50, tzinfo=utc) assert parsers.time('14:20:50+05:10') == time_(hour=14, minute=20, second=50, tzinfo=timezone(hours=5, minutes=10)) assert parsers.time('14:20:50-05:10') == time_(hour=14, minute=20, second=50, tzinfo=-timezone(hours=5, minutes=10)) assert parsers.time('14:20:50+05') == time_(hour=14, minute=20, second=50, tzinfo=timezone(hours=5)) assert parsers.time('14:20:50-05') == time_(hour=14, minute=20, second=50, tzinfo=-timezone(hours=5)) assert parsers.time('14:20:50+0510') == time_(hour=14, minute=20, second=50, tzinfo=timezone(hours=5, minutes=10)) assert parsers.time('14:20:50-0510') == time_(hour=14, minute=20, second=50, tzinfo=-timezone(hours=5, minutes=10)) assert parsers.time('12:30:40.05') == time_(hour=12, minute=30, second=40, microsecond=5000, tzinfo=utc) assert parsers.time('12:30:40.05Z') == time_(hour=12, minute=30, second=40, microsecond=5000, tzinfo=utc) assert parsers.time('12:30:40.05+10:15') == time_(hour=12, minute=30, second=40, microsecond=5000, tzinfo=timezone(hours=10, minutes=15)) assert parsers.time('12:30:40.05-08:45') == time_(hour=12, minute=30, second=40, microsecond=5000, tzinfo=-timezone(hours=8, minutes=45)) assert parsers.time('1315') == time_(hour=13, minute=15, tzinfo=utc) assert parsers.time('1315+05:10') == time_(hour=13, minute=15, tzinfo=timezone(hours=5, minutes=10)) assert parsers.time('1315-05:10') == time_(hour=13, minute=15, tzinfo=-timezone(hours=5, minutes=10)) assert parsers.time('142050'), time_(hour=14, minute=20, second=50, tzinfo=utc) assert parsers.time('142050+05:10') == time_(hour=14, minute=20, second=50, tzinfo=timezone(hours=5, minutes=10)) assert parsers.time('142050-05:10') == time_(hour=14, minute=20, second=50, tzinfo=-timezone(hours=5, minutes=10)) assert parsers.time('142050+05') == time_(hour=14, minute=20, second=50, tzinfo=timezone(hours=5)) assert parsers.time('142050-05') == time_(hour=14, minute=20, second=50, tzinfo=-timezone(hours=5)) assert parsers.time('142050+0510') == time_(hour=14, minute=20, second=50, tzinfo=timezone(hours=5, minutes=10)) assert parsers.time('142050-0510') == time_(hour=14, minute=20, second=50, tzinfo=-timezone(hours=5, minutes=10)) assert parsers.time('123040.05') == time_(hour=12, minute=30, second=40, microsecond=5000, tzinfo=utc) assert parsers.time('123040.05Z') == time_(hour=12, minute=30, second=40, microsecond=5000, tzinfo=utc) assert parsers.time('123040.05+10:15') == time_(hour=12, minute=30, second=40, microsecond=5000, tzinfo=timezone(hours=10, minutes=15)) assert parsers.time('123040.05-08:45') == time_(hour=12, minute=30, second=40, microsecond=5000, tzinfo=-timezone(hours=8, minutes=45))
def test_interval(): now = datetime_(2016, 1, 1) with pytest.raises(ValueError): parsers.interval('P6Yasdf') with pytest.raises(ValueError): parsers.interval('7432891') with pytest.raises(ValueError): parsers.interval('asdf') with pytest.raises(ValueError): parsers.interval('23P7DT5H') with pytest.raises(ValueError): parsers.interval('1999-12-31T16:00:00.000Z') with pytest.raises(ValueError): parsers.interval('1999-12-31T16:00:00.000+08:30') with pytest.raises(ValueError): parsers.interval('P6Yasdf/P8Y') with pytest.raises(ValueError): parsers.interval('P7Y/asdf') with pytest.raises(ValueError): parsers.interval('1999-12-01T00:00:0a/1999-12-31T16:00:00.000Z') with pytest.raises(ValueError): parsers.interval('A4/1999-12-01T00:00:00/1999-12-31T16:00:00.000Z') with pytest.raises(ValueError): parsers.interval('P6Y5M/P9D') with pytest.raises(ValueError): parsers.interval('7432891/1234') with pytest.raises(ValueError): parsers.interval('asdf/87rf') with pytest.raises(ValueError): parsers.interval('23P7DT5H/89R3') assert parsers.interval('P7Y', now=now) == interval(end=now, duration=duration(years=7)) assert parsers.interval('P6W', now=now) == interval(end=now, duration=duration(weeks=6)) assert parsers.interval('R1/P6Y5M', now=now) == interval(end=now, repeats=1, duration=duration(years=6, months=5)) assert parsers.interval('R5/1999-12-31T16:00:00.000Z/P5DT7H') == interval(start=datetime_(year=1999, month=12, day=31, hour=16, tzinfo=utc), duration=duration(days=5, hours=7), repeats=5) assert parsers.interval('R7/2016-08-01T23:10:59.111Z/2016-08-08T00:13:23.001Z') == interval(start=datetime_(year=2016, month=8, day=1, hour=23, minute=10, second=59, microsecond=111000, tzinfo=utc), end=datetime_(year=2016, month=8, day=8, hour=0, minute=13, second=23, microsecond=1000, tzinfo=utc), repeats=7) assert parsers.interval('R2/P5DT7H/1999-12-31T16:00:00.000Z') == interval(end=datetime_(year=1999, month=12, day=31, hour=16, tzinfo=utc), duration=duration(days=5, hours=7), repeats=2) assert parsers.interval('R5/2002-08-15T16:20:05.100+08:10/P5DT7H') == interval(start=datetime_(year=2002, month=8, day=15, hour=16, minute=20, second=5, microsecond=100000, tzinfo=timezone(hours=8, minutes=10)), duration=duration(days=5, hours=7), repeats=5) assert parsers.interval('2002-08-15T16:20:05.100+08:10/2002-10-12T17:05:25.020-01:40') == interval(start=datetime_(year=2002, month=8, day=15, hour=16, minute=20, second=5, microsecond=100000, tzinfo=timezone(hours=8, minutes=10)), end=datetime_(year=2002, month=10, day=12, hour=17, minute=5, second=25, microsecond=20000, tzinfo=-timezone(hours=1, minutes=40))) assert parsers.interval('R/P5DT7H/2002-08-15T16:20:05.100+08:10') == interval(end=datetime_(year=2002, month=8, day=15, hour=16, minute=20, second=5, microsecond=100000, tzinfo=timezone(hours=8, minutes=10)), duration=duration(days=5, hours=7), repeats=float('inf'))