diff --git a/edosl0util/headers.py b/edosl0util/headers.py index 85b7227490a14e537707fedccc68b0a5947125f3..df27606c3c35cada2275e784cda3ecfb24f26d6f 100644 --- a/edosl0util/headers.py +++ b/edosl0util/headers.py @@ -21,7 +21,7 @@ from edos.ccsds import ( GROUP_STANDALONE ) -from edosl0util.timecode import cds_stamp +from edosl0util.timecode import cds_stamp, cds_to_timestamp class BaseStruct(c.BigEndianStructure): @@ -52,8 +52,13 @@ class AquaCucTimecode(BaseStruct): ] EPOCH = datetime(1958, 1, 1) + EPOCH_SECS = (EPOCH - datetime(1970, 1, 1)).total_seconds() SUB_SECOND_UNITS = 15.2 + def astimestamp(self): + tc = self.timecode + return cds_to_timestamp(tc.days, tc.milliseconds, tc.microseconds, self.EPOCH_SECS) + def asdatetime(self): """ Return converted to UTC where leap seconds are as defined in `leap_seconds`. @@ -75,6 +80,10 @@ class DaySegmentedTimecode(BaseStruct): ] EPOCH = datetime(1958, 1, 1) + EPOCH_SECS = (EPOCH - datetime(1970, 1, 1)).total_seconds() + + def astimestamp(self): + return cds_to_timestamp(self.days, self.milliseconds, self.microseconds, self.EPOCH_SECS) def asdatetime(self): return cds_stamp(self.days, self.milliseconds, self.microseconds) diff --git a/edosl0util/timecode.py b/edosl0util/timecode.py index 51ac4a3d233e4f1d45205c3329691fc05c3406e5..3c8438446b0d17ef21f803d83da5d4cfd0f22f74 100644 --- a/edosl0util/timecode.py +++ b/edosl0util/timecode.py @@ -14,8 +14,15 @@ def unixtime(dt): 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) \ No newline at end of file + return JPSS_EPOCH + timedelta(days=days, microseconds=1000 * millis + micros)