"""Test PDS times modules."""

from datetime import datetime as dt

from pyvims.pds.times import (cassini2utc, cassini_time, dt_date, dt_doy, dt_iso,
                              dyear, pds_folder, pds_time, utc2cassini)


from pytest import approx, raises


def test_dt_iso():
    """Test parsing ISO time pattern."""
    assert str(dt_iso('2005-02-14T18:02:29.123')) == '2005-02-14 18:02:29.123000+00:00'
    assert str(dt_iso('2005-02-14 18:02:29')) == '2005-02-14 18:02:29+00:00'
    assert str(dt_iso('2005-02-14:18:02')) == '2005-02-14 18:02:00+00:00'
    assert str(dt_iso('2005-02-14')) == '2005-02-14 00:00:00+00:00'

    times = dt_iso('from 2005-02-14T18:02:29 to 2005-02-14T18:03')

    assert len(times) == 2
    assert str(times[0]) == '2005-02-14 18:02:29+00:00'
    assert str(times[1]) == '2005-02-14 18:03:00+00:00'

    with raises(ValueError):
        _ = dt_iso('2005-045')


def test_dt_doy():
    """Test parsing DOY time pattern."""
    assert str(dt_doy('2005-045T18:02:29.123')) == '2005-02-14 18:02:29.123000+00:00'
    assert str(dt_doy('2005-045 18:02:29')) == '2005-02-14 18:02:29+00:00'
    assert str(dt_doy('2005-045:18:02')) == '2005-02-14 18:02:00+00:00'
    assert str(dt_doy('2005-045')) == '2005-02-14 00:00:00+00:00'

    times = dt_doy('from 2005-045T18:02:29 to 2005-045T18:03')

    assert len(times) == 2
    assert str(times[0]) == '2005-02-14 18:02:29+00:00'
    assert str(times[1]) == '2005-02-14 18:03:00+00:00'

    with raises(ValueError):
        _ = dt_doy('2005-02-14')


def test_dt_date():
    """Test date pattern."""
    assert str(dt_date('Feb 14, 2005')) == '2005-02-14 00:00:00+00:00'
    assert str(dt_date('Febr 14, 2005')) == '2005-02-14 00:00:00+00:00'
    assert str(dt_date('Feb 14, 2005', eod=True)) == '2005-02-14 23:59:59+00:00'
    assert str(dt_date('to Feb 14, 2005')) == '2005-02-14 23:59:59+00:00'

    times = dt_date('from Feb 14, 2005 through March 12, 2006')

    assert len(times) == 2
    assert str(times[0]) == '2005-02-14 00:00:00+00:00'
    assert str(times[1]) == '2006-03-12 23:59:59+00:00'

    with raises(ValueError):
        _ = dt_date('2005-02-14')


def test_pds_time():
    """Test PDS time parsing."""
    assert str(pds_time('May 17, 2007')) == '2007-05-17 00:00:00+00:00'
    assert str(pds_time('2010-274T00:00:00')) == '2010-10-01 00:00:00+00:00'
    assert str(pds_time('2011-10-01T00:02:04.244')) == '2011-10-01 00:02:04.244000+00:00'

    t0, t1 = pds_time('… May 17, 2007 through Jun 30, 2007')
    assert str(t0) == '2007-05-17 00:00:00+00:00'
    assert str(t1) == '2007-06-30 23:59:59+00:00'

    t0, t1 = pds_time('… 2010-274T00:00:00 through 2010-365T23:59:59')
    assert str(t0) == '2010-10-01 00:00:00+00:00'
    assert str(t1) == '2010-12-31 23:59:59+00:00'

    t0, t1 = pds_time('… 2011-10-01T00:02:04.244 through 2011-12-31T12:28:45.128')
    assert str(t0) == '2011-10-01 00:02:04.244000+00:00'
    assert str(t1) == '2011-12-31 12:28:45.128000+00:00'

    t0, t1 = pds_time('2005015T175855_2005016T184233/')
    assert str(t0) == '2005-01-15 17:58:55+00:00'
    assert str(t1) == '2005-01-16 18:42:33+00:00'

    with raises(ValueError):
        _ = pds_time('No data available')


def test_cassini_time():
    """Test Cassini time parsing."""
    assert cassini_time('v1487096932_1.qub') == 1487096932.0
    assert cassini_time(1483230358.172) == 1483230358.172

    with raises(ValueError):
        _ = cassini_time('v123_1')

    with raises(ValueError):
        _ = cassini_time(123)


def test_cassini2utc():
    """Test Cassini time to UTC converter."""
    assert str(cassini2utc('v1487096932_1')) == '2005-02-14 18:02:29'
    assert str(cassini2utc(1483230358.172)) == '2005-01-01 00:00:00'


def test_utc2cassini():
    """Test UTC to Cassini time converter."""
    assert utc2cassini('2005-02-14T18:02:29') == approx(1487096932.068, abs=1e-3)

    times = utc2cassini('May 17, 2007 through Jun 30, 2007')

    assert len(times) == 2
    assert times[0] == approx(1558053238.602, abs=1e-3)
    assert times[1] == approx(1561941262.879, abs=1e-3)


def test_pds_folder():
    """Test convert PDS folder as string."""
    assert pds_folder('2005015T175855') == '2005-015T17:58:55'
    assert pds_folder('2005015T175855_2005016T184233/') == \
        '2005-015T17:58:55 2005-016T18:42:33'


def test_dyear():
    """Test decimal year."""
    assert dyear('2005-01-01') == 2005.0
    assert dyear('2005-12-31') == 2005.9973
    assert dyear('2004-12-31') == 2004.9973

    assert dyear(dt(2005, 1, 1)) == 2005.0
    assert dyear(dt(2005, 12, 31)) == 2005.9973
    assert dyear(dt(2004, 12, 31)) == 2004.9973

[((640, 694), 'pyvims.pds.times.dt_iso', 'dt_iso', (['"""from 2005-02-14T18:02:29 to 2005-02-14T18:03"""'], {}), "('from 2005-02-14T18:02:29 to 2005-02-14T18:03')\n", (646, 694), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((1270, 1320), 'pyvims.pds.times.dt_doy', 'dt_doy', (['"""from 2005-045T18:02:29 to 2005-045T18:03"""'], {}), "('from 2005-045T18:02:29 to 2005-045T18:03')\n", (1276, 1320), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((1886, 1937), 'pyvims.pds.times.dt_date', 'dt_date', (['"""from Feb 14, 2005 through March 12, 2006"""'], {}), "('from Feb 14, 2005 through March 12, 2006')\n", (1893, 1937), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((2451, 2498), 'pyvims.pds.times.pds_time', 'pds_time', (['"""… May 17, 2007 through Jun 30, 2007"""'], {}), "('… May 17, 2007 through Jun 30, 2007')\n", (2459, 2498), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((2613, 2670), 'pyvims.pds.times.pds_time', 'pds_time', (['"""… 2010-274T00:00:00 through 2010-365T23:59:59"""'], {}), "('… 2010-274T00:00:00 through 2010-365T23:59:59')\n", (2621, 2670), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((2785, 2854), 'pyvims.pds.times.pds_time', 'pds_time', (['"""… 2011-10-01T00:02:04.244 through 2011-12-31T12:28:45.128"""'], {}), "('… 2011-10-01T00:02:04.244 through 2011-12-31T12:28:45.128')\n", (2793, 2854), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((2983, 3025), 'pyvims.pds.times.pds_time', 'pds_time', (['"""2005015T175855_2005016T184233/"""'], {}), "('2005015T175855_2005016T184233/')\n", (2991, 3025), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((3884, 3932), 'pyvims.pds.times.utc2cassini', 'utc2cassini', (['"""May 17, 2007 through Jun 30, 2007"""'], {}), "('May 17, 2007 through Jun 30, 2007')\n", (3895, 3932), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((845, 863), 'pytest.raises', 'raises', (['ValueError'], {}), '(ValueError)\n', (851, 863), False, 'from pytest import approx, raises\n'), ((877, 895), 'pyvims.pds.times.dt_iso', 'dt_iso', (['"""2005-045"""'], {}), "('2005-045')\n", (883, 895), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((1471, 1489), 'pytest.raises', 'raises', (['ValueError'], {}), '(ValueError)\n', (1477, 1489), False, 'from pytest import approx, raises\n'), ((1503, 1523), 'pyvims.pds.times.dt_doy', 'dt_doy', (['"""2005-02-14"""'], {}), "('2005-02-14')\n", (1509, 1523), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((2088, 2106), 'pytest.raises', 'raises', (['ValueError'], {}), '(ValueError)\n', (2094, 2106), False, 'from pytest import approx, raises\n'), ((2120, 2141), 'pyvims.pds.times.dt_date', 'dt_date', (['"""2005-02-14"""'], {}), "('2005-02-14')\n", (2127, 2141), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((3136, 3154), 'pytest.raises', 'raises', (['ValueError'], {}), '(ValueError)\n', (3142, 3154), False, 'from pytest import approx, raises\n'), ((3168, 3197), 'pyvims.pds.times.pds_time', 'pds_time', (['"""No data available"""'], {}), "('No data available')\n", (3176, 3197), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((3273, 3306), 'pyvims.pds.times.cassini_time', 'cassini_time', (['"""v1487096932_1.qub"""'], {}), "('v1487096932_1.qub')\n", (3285, 3306), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((3334, 3362), 'pyvims.pds.times.cassini_time', 'cassini_time', (['(1483230358.172)'], {}), '(1483230358.172)\n', (3346, 3362), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((3391, 3409), 'pytest.raises', 'raises', (['ValueError'], {}), '(ValueError)\n', (3397, 3409), False, 'from pytest import approx, raises\n'), ((3423, 3445), 'pyvims.pds.times.cassini_time', 'cassini_time', (['"""v123_1"""'], {}), "('v123_1')\n", (3435, 3445), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((3456, 3474), 'pytest.raises', 'raises', (['ValueError'], {}), '(ValueError)\n', (3462, 3474), False, 'from pytest import approx, raises\n'), ((3488, 3505), 'pyvims.pds.times.cassini_time', 'cassini_time', (['(123)'], {}), '(123)\n', (3500, 3505), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((3800, 3834), 'pyvims.pds.times.utc2cassini', 'utc2cassini', (['"""2005-02-14T18:02:29"""'], {}), "('2005-02-14T18:02:29')\n", (3811, 3834), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((3838, 3871), 'pytest.approx', 'approx', (['(1487096932.068)'], {'abs': '(0.001)'}), '(1487096932.068, abs=0.001)\n', (3844, 3871), False, 'from pytest import approx, raises\n'), ((3984, 4017), 'pytest.approx', 'approx', (['(1558053238.602)'], {'abs': '(0.001)'}), '(1558053238.602, abs=0.001)\n', (3990, 4017), False, 'from pytest import approx, raises\n'), ((4040, 4073), 'pytest.approx', 'approx', (['(1561941262.879)'], {'abs': '(0.001)'}), '(1561941262.879, abs=0.001)\n', (4046, 4073), False, 'from pytest import approx, raises\n'), ((4154, 4182), 'pyvims.pds.times.pds_folder', 'pds_folder', (['"""2005015T175855"""'], {}), "('2005015T175855')\n", (4164, 4182), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((4217, 4261), 'pyvims.pds.times.pds_folder', 'pds_folder', (['"""2005015T175855_2005016T184233/"""'], {}), "('2005015T175855_2005016T184233/')\n", (4227, 4261), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((4373, 4392), 'pyvims.pds.times.dyear', 'dyear', (['"""2005-01-01"""'], {}), "('2005-01-01')\n", (4378, 4392), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((4414, 4433), 'pyvims.pds.times.dyear', 'dyear', (['"""2005-12-31"""'], {}), "('2005-12-31')\n", (4419, 4433), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((4458, 4477), 'pyvims.pds.times.dyear', 'dyear', (['"""2004-12-31"""'], {}), "('2004-12-31')\n", (4463, 4477), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((335, 368), 'pyvims.pds.times.dt_iso', 'dt_iso', (['"""2005-02-14T18:02:29.123"""'], {}), "('2005-02-14T18:02:29.123')\n", (341, 368), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((423, 452), 'pyvims.pds.times.dt_iso', 'dt_iso', (['"""2005-02-14 18:02:29"""'], {}), "('2005-02-14 18:02:29')\n", (429, 452), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((500, 526), 'pyvims.pds.times.dt_iso', 'dt_iso', (['"""2005-02-14:18:02"""'], {}), "('2005-02-14:18:02')\n", (506, 526), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((574, 594), 'pyvims.pds.times.dt_iso', 'dt_iso', (['"""2005-02-14"""'], {}), "('2005-02-14')\n", (580, 594), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((973, 1004), 'pyvims.pds.times.dt_doy', 'dt_doy', (['"""2005-045T18:02:29.123"""'], {}), "('2005-045T18:02:29.123')\n", (979, 1004), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((1059, 1086), 'pyvims.pds.times.dt_doy', 'dt_doy', (['"""2005-045 18:02:29"""'], {}), "('2005-045 18:02:29')\n", (1065, 1086), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((1134, 1158), 'pyvims.pds.times.dt_doy', 'dt_doy', (['"""2005-045:18:02"""'], {}), "('2005-045:18:02')\n", (1140, 1158), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((1206, 1224), 'pyvims.pds.times.dt_doy', 'dt_doy', (['"""2005-045"""'], {}), "('2005-045')\n", (1212, 1224), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((1590, 1613), 'pyvims.pds.times.dt_date', 'dt_date', (['"""Feb 14, 2005"""'], {}), "('Feb 14, 2005')\n", (1597, 1613), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((1661, 1685), 'pyvims.pds.times.dt_date', 'dt_date', (['"""Febr 14, 2005"""'], {}), "('Febr 14, 2005')\n", (1668, 1685), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((1733, 1766), 'pyvims.pds.times.dt_date', 'dt_date', (['"""Feb 14, 2005"""'], {'eod': '(True)'}), "('Feb 14, 2005', eod=True)\n", (1740, 1766), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((1814, 1840), 'pyvims.pds.times.dt_date', 'dt_date', (['"""to Feb 14, 2005"""'], {}), "('to Feb 14, 2005')\n", (1821, 1840), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((2213, 2237), 'pyvims.pds.times.pds_time', 'pds_time', (['"""May 17, 2007"""'], {}), "('May 17, 2007')\n", (2221, 2237), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((2285, 2314), 'pyvims.pds.times.pds_time', 'pds_time', (['"""2010-274T00:00:00"""'], {}), "('2010-274T00:00:00')\n", (2293, 2314), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((2362, 2397), 'pyvims.pds.times.pds_time', 'pds_time', (['"""2011-10-01T00:02:04.244"""'], {}), "('2011-10-01T00:02:04.244')\n", (2370, 2397), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((3593, 3621), 'pyvims.pds.times.cassini2utc', 'cassini2utc', (['"""v1487096932_1"""'], {}), "('v1487096932_1')\n", (3604, 3621), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((3663, 3690), 'pyvims.pds.times.cassini2utc', 'cassini2utc', (['(1483230358.172)'], {}), '(1483230358.172)\n', (3674, 3690), False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((4509, 4523), 'datetime.datetime', 'dt', (['(2005)', '(1)', '(1)'], {}), '(2005, 1, 1)\n', (4511, 4523), True, 'from datetime import datetime as dt\n'), ((4552, 4568), 'datetime.datetime', 'dt', (['(2005)', '(12)', '(31)'], {}), '(2005, 12, 31)\n', (4554, 4568), True, 'from datetime import datetime as dt\n'), ((4600, 4616), 'datetime.datetime', 'dt', (['(2004)', '(12)', '(31)'], {}), '(2004, 12, 31)\n', (4602, 4616), True, 'from datetime import datetime as dt\n')]