Skip to content
Snippets Groups Projects
timecode.py 786 B
# encoding: utf-8
__copyright__ = "Copyright (C) 2015 University of Wisconsin SSEC. All rights reserved."

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_to_timestamp(days, millis, micros, epoch):
    """
    CDS to unix timestamp seconds.
    """
    return epoch + (86400 * days) + (millis / 1000.) + (micros / 1000000)


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)