from datetime import date, datetime, timedelta
import pytest
from mf import parse_dt
def test_none():
assert parse_dt(None) is None
def test_parse_dates():
assert parse_dt("2014-04-27") == date(2014, 4, 27)
assert parse_dt("2014-9-2") == date(2014, 9, 2)
assert parse_dt("1982-11-24") == date(1982, 11, 24)
with pytest.raises(ValueError):
# day/month switched
parse_dt("2014-24-11")
with pytest.raises(ValueError):
# 2-character year
parse_dt("14-09-27")
def test_parse_dts_no_tz():
# tantek.com -- no seconds, no timezone
assert parse_dt("2014-05-09T17:53") == datetime(2014, 5, 9, 17, 53)
# same as above without 'T'
assert parse_dt("2014-05-09 17:53") == datetime(2014, 5, 9, 17, 53)
# Homebrew Website Club
assert parse_dt("2014-04-23T18:30") == datetime(2014, 4, 23, 18, 30)
with pytest.raises(ValueError):
# hour only
parse_dt("2012-09-01T12")
with pytest.raises(ValueError):
# invalid hour minute
parse_dt("2014-04-23T30:90")
def test_parse_dts():
def assert_with_tz(dt, naive, offset):
"""return a tuple with naive datetime, and an timedelta tz offset"""
assert naive == dt.replace(tzinfo=None)
assert offset == dt.utcoffset()
# waterpigs.co.uk -- utc time
assert_with_tz(
parse_dt("2014-05-10T10:48:28+00:00"),
datetime(2014, 5, 10, 10, 48, 28),
timedelta(hours=0),
)
# same as above with Zulu time
assert_with_tz(
parse_dt("2014-05-10T10:48:28Z"),
datetime(2014, 5, 10, 10, 48, 28),
timedelta(hours=0),
)
# snarfed.org -- pacific time
assert_with_tz(
parse_dt("2014-05-05T09:59:08-07:00"),
datetime(2014, 5, 5, 9, 59, 8),
timedelta(hours=-7),
)
# same as above, no colon in tz
assert_with_tz(
parse_dt("2014-05-05T09:59:08-0700"),
datetime(2014, 5, 5, 9, 59, 8),
timedelta(hours=-7),
)
with pytest.raises(ValueError):
# cannot read timezones by name
parse_dt("2013-07-04T11:22 PST")