from datetime import datetime, timedelta

JPSS_EPOCH = datetime(1958, 1, 1)
UNIX_EPOCH = datetime(1970, 1, 1)


def unixtime(dt):
    """
    Datetime to Unix timestamp.
    """
    if dt > UNIX_EPOCH:
        return (dt - UNIX_EPOCH).total_seconds()
    return (UNIX_EPOCH - dt).total_seconds()


def cds_stamp(days, millis, micros, epoch=JPSS_EPOCH):
    """
    CCSDS Day Segmented timecode to UTC datetime.
    """
    return JPSS_EPOCH + timedelta(days=days, microseconds=1000 * millis + micros)