def pretty_time(timestamp: str): """Format timestamp for human consumption.""" try: parsed = iso_8601.parse_datetime(timestamp) except ValueError: now = datetime.utcnow().replace(tzinfo=timezone.utc) try: delta = iso_8601.parse_delta(timestamp) except ValueError: delta = human_time.parse_timedelta(timestamp) parsed = now - delta echo(human_time.human_timestamp(parsed))
def test_parse_invalid_timedelta(): with expect.raises(ValueError): parse_timedelta('1 k')
def test_parse_timedelta(string, dt): expect(parse_timedelta(string)) == dt
def test_parse_invalid_timedelta(): with raises(ValueError, match='Invalid ‘frequency’ value'): parse_timedelta('1 k')
def test_parse_timedelta(string, dt): assert parse_timedelta(string) == dt