import time


def utc_to_gps(utcsec):
    """
    Convert UTC seconds to GPS seconds.

    Parameters
    ----------
    utcsec: int
      Time in utc seconds.

    Returns
    -------
    Time in GPS seconds.

    Notes
    -----
    The code is ported from Offline.

    Examples
    --------
    >>> utc_to_gps(315964800) # Jan 6th, 1980
    0
    """


    def seconds(year, month, day):
        return time.mktime((year, month, day, 0, 0, 0, 0, 0, 0)) - time.timezone

    kUTCGPSOffset0 = (10 * 365 + 7) * 24 * 3600
    kLeapSecondList = (
        (seconds(1981, 7, 1), 1),  # 1 JUL 1981
        (seconds(1982, 7, 1), 2),  # 1 JUL 1982
        (seconds(1983, 7, 1), 3),  # 1 JUL 1983
        (seconds(1985, 7, 1), 4),  # 1 JUL 1985
        (seconds(1988, 1, 1), 5),  # 1 JAN 1988
        (seconds(1990, 1, 1), 6),  # 1 JAN 1990
        (seconds(1991, 1, 1), 7),  # 1 JAN 1991
        (seconds(1992, 7, 1), 8),  # 1 JUL 1992
        (seconds(1993, 7, 1), 9),  # 1 JUL 1993
        (seconds(1994, 7, 1), 10),  # 1 JUL 1994
        (seconds(1996, 1, 1), 11),  # 1 JAN 1996
        (seconds(1997, 7, 1), 12),  # 1 JUL 1997
        (seconds(1999, 1, 1), 13),  # 1 JAN 1999
        # DV: 2000 IS a leap year since it is divisible by 400,
        #     ie leap years here are 2000 and 2004 -> leap days = 6
        (seconds(2006, 1, 1), 14),  # 1 JAN 2006
        (seconds(2009, 1, 1), 15),  # 1 JAN 2009, from IERS Bulletin C No. 36
        (seconds(2012, 7, 1), 16),  # 1 JUL 2012, from IERS Bulletin C No. 43
        # 1 JUL 2015, from IERS Bulletin C No. 49
        # (https://hpiers.obspm.fr/iers/bul/bulc/bulletinc.49)
        (seconds(2015, 7, 1), 17),
        (seconds(2017, 1, 1), 18)   # 1 JAN 2017, from IERS Bulletin C No. 52
    )

    leapSeconds = 0
    for x in reversed(kLeapSecondList):
        if utcsec >= x[0]:
            leapSeconds = x[1]
            break

    return utcsec - kUTCGPSOffset0 + leapSeconds