From 461847974bae79a4a659e33053c825634d77985e Mon Sep 17 00:00:00 2001
From: Bruce Flynn <brucef@ssec.wisc.edu>
Date: Mon, 28 Sep 2015 20:11:42 +0000
Subject: [PATCH] Ability to get unix timestamp from timecode

---
 edosl0util/headers.py  | 11 ++++++++++-
 edosl0util/timecode.py |  9 ++++++++-
 2 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/edosl0util/headers.py b/edosl0util/headers.py
index 85b7227..df27606 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 51ac4a3..3c84384 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)
-- 
GitLab