"""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

[((19, 12, 19, 66), 'pyvims.pds.times.dt_iso', 'dt_iso', ({(19, 19, 19, 65): '"""from 2005-02-14T18:02:29 to 2005-02-14T18:03"""'}, {}), "('from 2005-02-14T18:02:29 to 2005-02-14T18:03')", False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((36, 12, 36, 62), 'pyvims.pds.times.dt_doy', 'dt_doy', ({(36, 19, 36, 61): '"""from 2005-045T18:02:29 to 2005-045T18:03"""'}, {}), "('from 2005-045T18:02:29 to 2005-045T18:03')", False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((53, 12, 53, 63), 'pyvims.pds.times.dt_date', 'dt_date', ({(53, 20, 53, 62): '"""from Feb 14, 2005 through March 12, 2006"""'}, {}), "('from Feb 14, 2005 through March 12, 2006')", False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((69, 13, 69, 62), 'pyvims.pds.times.pds_time', 'pds_time', ({(69, 22, 69, 61): '"""… May 17, 2007 through Jun 30, 2007"""'}, {}), "('… May 17, 2007 through Jun 30, 2007')", False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((73, 13, 73, 72), 'pyvims.pds.times.pds_time', 'pds_time', ({(73, 22, 73, 71): '"""… 2010-274T00:00:00 through 2010-365T23:59:59"""'}, {}), "('… 2010-274T00:00:00 through 2010-365T23:59:59')", False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((77, 13, 77, 84), 'pyvims.pds.times.pds_time', 'pds_time', ({(77, 22, 77, 83): '"""… 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')", False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((81, 13, 81, 55), 'pyvims.pds.times.pds_time', 'pds_time', ({(81, 22, 81, 54): '"""2005015T175855_2005016T184233/"""'}, {}), "('2005015T175855_2005016T184233/')", False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((111, 12, 111, 60), 'pyvims.pds.times.utc2cassini', 'utc2cassini', ({(111, 24, 111, 59): '"""May 17, 2007 through Jun 30, 2007"""'}, {}), "('May 17, 2007 through Jun 30, 2007')", False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((25, 9, 25, 27), 'pytest.raises', 'raises', ({(25, 16, 25, 26): 'ValueError'}, {}), '(ValueError)', False, 'from pytest import approx, raises\n'), ((26, 12, 26, 30), 'pyvims.pds.times.dt_iso', 'dt_iso', ({(26, 19, 26, 29): '"""2005-045"""'}, {}), "('2005-045')", False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((42, 9, 42, 27), 'pytest.raises', 'raises', ({(42, 16, 42, 26): 'ValueError'}, {}), '(ValueError)', False, 'from pytest import approx, raises\n'), ((43, 12, 43, 32), 'pyvims.pds.times.dt_doy', 'dt_doy', ({(43, 19, 43, 31): '"""2005-02-14"""'}, {}), "('2005-02-14')", False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((59, 9, 59, 27), 'pytest.raises', 'raises', ({(59, 16, 59, 26): 'ValueError'}, {}), '(ValueError)', False, 'from pytest import approx, raises\n'), ((60, 12, 60, 33), 'pyvims.pds.times.dt_date', 'dt_date', ({(60, 20, 60, 32): '"""2005-02-14"""'}, {}), "('2005-02-14')", False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((85, 9, 85, 27), 'pytest.raises', 'raises', ({(85, 16, 85, 26): 'ValueError'}, {}), '(ValueError)', False, 'from pytest import approx, raises\n'), ((86, 12, 86, 41), 'pyvims.pds.times.pds_time', 'pds_time', ({(86, 21, 86, 40): '"""No data available"""'}, {}), "('No data available')", False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((91, 11, 91, 44), 'pyvims.pds.times.cassini_time', 'cassini_time', ({(91, 24, 91, 43): '"""v1487096932_1.qub"""'}, {}), "('v1487096932_1.qub')", False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((92, 11, 92, 39), 'pyvims.pds.times.cassini_time', 'cassini_time', ({(92, 24, 92, 38): '(1483230358.172)'}, {}), '(1483230358.172)', False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((94, 9, 94, 27), 'pytest.raises', 'raises', ({(94, 16, 94, 26): 'ValueError'}, {}), '(ValueError)', False, 'from pytest import approx, raises\n'), ((95, 12, 95, 34), 'pyvims.pds.times.cassini_time', 'cassini_time', ({(95, 25, 95, 33): '"""v123_1"""'}, {}), "('v123_1')", False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((97, 9, 97, 27), 'pytest.raises', 'raises', ({(97, 16, 97, 26): 'ValueError'}, {}), '(ValueError)', False, 'from pytest import approx, raises\n'), ((98, 12, 98, 29), 'pyvims.pds.times.cassini_time', 'cassini_time', ({(98, 25, 98, 28): '123'}, {}), '(123)', False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((109, 11, 109, 45), 'pyvims.pds.times.utc2cassini', 'utc2cassini', ({(109, 23, 109, 44): '"""2005-02-14T18:02:29"""'}, {}), "('2005-02-14T18:02:29')", False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((109, 49, 109, 81), 'pytest.approx', 'approx', (), '', False, 'from pytest import approx, raises\n'), ((114, 23, 114, 55), 'pytest.approx', 'approx', (), '', False, 'from pytest import approx, raises\n'), ((115, 23, 115, 55), 'pytest.approx', 'approx', (), '', False, 'from pytest import approx, raises\n'), ((120, 11, 120, 39), 'pyvims.pds.times.pds_folder', 'pds_folder', ({(120, 22, 120, 38): '"""2005015T175855"""'}, {}), "('2005015T175855')", False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((121, 11, 121, 55), 'pyvims.pds.times.pds_folder', 'pds_folder', ({(121, 22, 121, 54): '"""2005015T175855_2005016T184233/"""'}, {}), "('2005015T175855_2005016T184233/')", False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((127, 11, 127, 30), 'pyvims.pds.times.dyear', 'dyear', ({(127, 17, 127, 29): '"""2005-01-01"""'}, {}), "('2005-01-01')", False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((128, 11, 128, 30), 'pyvims.pds.times.dyear', 'dyear', ({(128, 17, 128, 29): '"""2005-12-31"""'}, {}), "('2005-12-31')", False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((129, 11, 129, 30), 'pyvims.pds.times.dyear', 'dyear', ({(129, 17, 129, 29): '"""2004-12-31"""'}, {}), "('2004-12-31')", False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((14, 15, 14, 48), 'pyvims.pds.times.dt_iso', 'dt_iso', ({(14, 22, 14, 47): '"""2005-02-14T18:02:29.123"""'}, {}), "('2005-02-14T18:02:29.123')", False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((15, 15, 15, 44), 'pyvims.pds.times.dt_iso', 'dt_iso', ({(15, 22, 15, 43): '"""2005-02-14 18:02:29"""'}, {}), "('2005-02-14 18:02:29')", False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((16, 15, 16, 41), 'pyvims.pds.times.dt_iso', 'dt_iso', ({(16, 22, 16, 40): '"""2005-02-14:18:02"""'}, {}), "('2005-02-14:18:02')", False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((17, 15, 17, 35), 'pyvims.pds.times.dt_iso', 'dt_iso', ({(17, 22, 17, 34): '"""2005-02-14"""'}, {}), "('2005-02-14')", False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((31, 15, 31, 46), 'pyvims.pds.times.dt_doy', 'dt_doy', ({(31, 22, 31, 45): '"""2005-045T18:02:29.123"""'}, {}), "('2005-045T18:02:29.123')", False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((32, 15, 32, 42), 'pyvims.pds.times.dt_doy', 'dt_doy', ({(32, 22, 32, 41): '"""2005-045 18:02:29"""'}, {}), "('2005-045 18:02:29')", False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((33, 15, 33, 39), 'pyvims.pds.times.dt_doy', 'dt_doy', ({(33, 22, 33, 38): '"""2005-045:18:02"""'}, {}), "('2005-045:18:02')", False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((34, 15, 34, 33), 'pyvims.pds.times.dt_doy', 'dt_doy', ({(34, 22, 34, 32): '"""2005-045"""'}, {}), "('2005-045')", False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((48, 15, 48, 38), 'pyvims.pds.times.dt_date', 'dt_date', ({(48, 23, 48, 37): '"""Feb 14, 2005"""'}, {}), "('Feb 14, 2005')", False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((49, 15, 49, 39), 'pyvims.pds.times.dt_date', 'dt_date', ({(49, 23, 49, 38): '"""Febr 14, 2005"""'}, {}), "('Febr 14, 2005')", False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((50, 15, 50, 48), 'pyvims.pds.times.dt_date', 'dt_date', (), '', False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((51, 15, 51, 41), 'pyvims.pds.times.dt_date', 'dt_date', ({(51, 23, 51, 40): '"""to Feb 14, 2005"""'}, {}), "('to Feb 14, 2005')", False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((65, 15, 65, 39), 'pyvims.pds.times.pds_time', 'pds_time', ({(65, 24, 65, 38): '"""May 17, 2007"""'}, {}), "('May 17, 2007')", False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((66, 15, 66, 44), 'pyvims.pds.times.pds_time', 'pds_time', ({(66, 24, 66, 43): '"""2010-274T00:00:00"""'}, {}), "('2010-274T00:00:00')", False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((67, 15, 67, 50), 'pyvims.pds.times.pds_time', 'pds_time', ({(67, 24, 67, 49): '"""2011-10-01T00:02:04.244"""'}, {}), "('2011-10-01T00:02:04.244')", False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((103, 15, 103, 43), 'pyvims.pds.times.cassini2utc', 'cassini2utc', ({(103, 27, 103, 42): '"""v1487096932_1"""'}, {}), "('v1487096932_1')", False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((104, 15, 104, 42), 'pyvims.pds.times.cassini2utc', 'cassini2utc', ({(104, 27, 104, 41): '(1483230358.172)'}, {}), '(1483230358.172)', False, 'from pyvims.pds.times import cassini2utc, cassini_time, dt_date, dt_doy, dt_iso, dyear, pds_folder, pds_time, utc2cassini\n'), ((131, 17, 131, 31), 'datetime.datetime', 'dt', ({(131, 20, 131, 24): '(2005)', (131, 26, 131, 27): '(1)', (131, 29, 131, 30): '(1)'}, {}), '(2005, 1, 1)', True, 'from datetime import datetime as dt\n'), ((132, 17, 132, 33), 'datetime.datetime', 'dt', ({(132, 20, 132, 24): '(2005)', (132, 26, 132, 28): '(12)', (132, 30, 132, 32): '(31)'}, {}), '(2005, 12, 31)', True, 'from datetime import datetime as dt\n'), ((133, 17, 133, 33), 'datetime.datetime', 'dt', ({(133, 20, 133, 24): '(2004)', (133, 26, 133, 28): '(12)', (133, 30, 133, 32): '(31)'}, {}), '(2004, 12, 31)', True, 'from datetime import datetime as dt\n')]