def test_timespec_auto(dt, sep): if dt.tzinfo is not None: # Assume offset has no sub-second components assume(dt.utcoffset().total_seconds() % 60 == 0) sep = str(sep) # Python 2.7 requires bytes dtstr = dt.isoformat(sep=sep) dt_rt = isoparse(dtstr) assert dt_rt == dt
def _isoparse_date_and_time(dt, date_fmt, time_fmt, tzoffset, microsecond_precision=None): tzi, offset_str = tzoffset fmt = date_fmt + 'T' + time_fmt dt = dt.replace(tzinfo=tzi) dtstr = dt.strftime(fmt) if microsecond_precision is not None: if not fmt.endswith('%f'): # pragma: nocover raise ValueError('Time format has no microseconds!') if microsecond_precision != 6: dtstr = dtstr[:-(6 - microsecond_precision)] elif microsecond_precision > 6: # pragma: nocover raise ValueError('Precision must be 1-6') dtstr += offset_str assert isoparse(dtstr) == dt
def test_year_month(dt): fmt = '%Y-%m' dtstr = dt.strftime(fmt) assert isoparse(dtstr) == dt
def test_year_month_day(dt, fmt): dtstr = dt.strftime(fmt) assert isoparse(dtstr) == dt
def test_iso_raises_failing(isostr, exception): # These are test cases where the current implementation is too lenient # and need to be fixed with pytest.raises(exception): isoparse(isostr)
def test_year_only(dt): dtstr = dt.strftime('%Y') assert isoparse(dtstr) == dt
def test_bytes(isostr, dt): assert isoparse(isostr) == dt
def test_iso_raises(isostr, exception): with pytest.raises(exception): isoparse(isostr)
def test_iso_ordinal(isoord, dt_expected): for fmt in ('{:04d}-{:03d}', '{:04d}{:03d}'): dtstr = fmt.format(*isoord) assert isoparse(dtstr) == dt_expected
def test_isoweek_day(isocal, dt_expected): # TODO: Figure out how to parametrize this on formats, too for fmt in ('{:04d}-W{:02d}-{:d}', '{:04d}W{:02d}{:d}'): dtstr = fmt.format(*isocal) assert isoparse(dtstr) == dt_expected
def test_isoparse_sep_none(datestr, sep): isostr = datestr + sep + '14:33:09' assert isoparse(isostr) == datetime(2014, 1, 1, 14, 33, 9)
def test_datetime_midnight(dt_str): assert isoparse(dt_str) == datetime(2014, 4, 11, 0, 0, 0, 0)
def test_extra_subsecond_digits(dt_str): assert isoparse(dt_str) == datetime(2018, 7, 3, 14, 7, 0, 123456)