# Path: tecs/rinex/common.py
# def validate_epoch(epoch):
#     """validate_epoch(epoch) -> datetime
# 
#     do some checks:
# 
#     - sometimes the seconds or minutes value >= 60, to return datetime.datetime
#       we need to check this;
#     - converts YY to YYYY (datetime.datetime threats 92 and 1992 in different
#       ways.
# 
#     Parameters
#     ----------
#     epoch : list
#         epoch = [year, month, day, hour, min, sec, microsec]
# 
#     Returns
#     -------
#     datetime : datetime.datetime
#     """
#     epoch = epoch[:]
# 
#     # YY -> YYYY
#     if epoch[0] < 100:
#         if epoch[0] >= 89:
#             epoch[0] += 1900
#         elif epoch[0] < 89:
#             epoch[0] += 2000
# 
#     delta = datetime.timedelta(0)
# 
#     # epoch[-2] - seconds; epoch[-3] - minutes
#     # we do all calculation in seconds so we use multiplier
#     for i, ier in [(-2, 1), (-3, 60)]:
#         if 60 <= epoch[i] <= 120:
#             sec = (epoch[i] - 59) * ier
#             delta += datetime.timedelta(seconds=sec)
#             epoch[i] = 59
# 
#     epoch = datetime.datetime(*epoch) + delta
# 
#     return epoch


import datetime
from nose.plugins.attrib import attr
from nose.tools import assert_raises
from tecs.rinex.common import validate_epoch


@attr('rinex.common')
def test_validate_epoch():
    """validate_epoch
    """
    epoch = (
        [2000, 1, 1, 1, 0, 0, 0],
        [1988, 1, 2, 3, 61, 14, 1250],  # min == 61
        [92, 11, 16, 7, 34, 60, 0],  # sec == 60
        [2, 11, 16, 7, 120, 0, 0]  # min == 120
    )

    test_epoch = (
        [2000, 1, 1, 1, 0, 0, 0],
        [1988, 1, 2, 4, 1, 14, 1250],
        [1992, 11, 16, 7, 35, 0, 0],
        [2002, 11, 16, 9, 0, 0, 0]
    )

    for i, test in enumerate(test_epoch):
        test = datetime.datetime(*test)
        result = validate_epoch(epoch[i])

        err = '{} != {}'.format(test, result)
        assert test == result, err

    epoch = [1999, 6, 8, 21, 0, 121, 0]

    with assert_raises(ValueError):

# Path: tecs/rinex/common.py
# def validate_epoch(epoch):
#     """validate_epoch(epoch) -> datetime
# 
#     do some checks:
# 
#     - sometimes the seconds or minutes value >= 60, to return datetime.datetime
#       we need to check this;
#     - converts YY to YYYY (datetime.datetime threats 92 and 1992 in different
#       ways.
# 
#     Parameters
#     ----------
#     epoch : list
#         epoch = [year, month, day, hour, min, sec, microsec]
# 
#     Returns
#     -------
#     datetime : datetime.datetime
#     """
#     epoch = epoch[:]
# 
#     # YY -> YYYY
#     if epoch[0] < 100:
#         if epoch[0] >= 89:
#             epoch[0] += 1900
#         elif epoch[0] < 89:
#             epoch[0] += 2000
# 
#     delta = datetime.timedelta(0)
# 
#     # epoch[-2] - seconds; epoch[-3] - minutes
#     # we do all calculation in seconds so we use multiplier
#     for i, ier in [(-2, 1), (-3, 60)]:
#         if 60 <= epoch[i] <= 120:
#             sec = (epoch[i] - 59) * ier
#             delta += datetime.timedelta(seconds=sec)
#             epoch[i] = 59
# 
#     epoch = datetime.datetime(*epoch) + delta
# 
#     return epoch


# Path: tecs/tests/test_rinex_common.py
import datetime
from nose.plugins.attrib import attr
from nose.tools import assert_raises
from tecs.rinex.common import validate_epoch



@attr('rinex.common')
def test_validate_epoch():
    """validate_epoch
    """
    epoch = (
        [2000, 1, 1, 1, 0, 0, 0],
        [1988, 1, 2, 3, 61, 14, 1250],  # min == 61
        [92, 11, 16, 7, 34, 60, 0],  # sec == 60
        [2, 11, 16, 7, 120, 0, 0]  # min == 120
    )

    test_epoch = (
        [2000, 1, 1, 1, 0, 0, 0],
        [1988, 1, 2, 4, 1, 14, 1250],
        [1992, 11, 16, 7, 35, 0, 0],
        [2002, 11, 16, 9, 0, 0, 0]
    )

    for i, test in enumerate(test_epoch):
        test = datetime.datetime(*test)
        result = validate_epoch(epoch[i])

        err = '{} != {}'.format(test, result)
        assert test == result, err

    epoch = [1999, 6, 8, 21, 0, 121, 0]

    with assert_raises(ValueError):

        validate_epoch(epoch)