Merging nepi-3.1-icn into nepi-3-dev
[nepi.git] / src / nepi / util / timefuncs.py
index 625dd70..f7fbc85 100644 (file)
@@ -35,7 +35,7 @@ def stformat(sdate):
     """
     return datetime.datetime.strptime(sdate, _strf).date()
 
-def tsfromat(date = None):
+def tsformat(date = None):
     """ Formats a datetime object to a string with format YYYYMMddHHMMSSffff.
     If no date is given, the current date is used.
     
@@ -56,13 +56,16 @@ def tdiff(date1, date2):
     """
     return date1 - date2
 
+def _get_total_seconds(td): 
+    return (td.microseconds + (td.seconds + td.days * 24 * 3600) * 1e6) / 1e6
+
 def tdiffsec(date1, date2):
     """ Returns the date difference ( date1 - date2 ) in seconds,
     where date1 and date 2 are datetime objects 
     
     """
     diff = tdiff(date1, date2)
-    return diff.total_seconds()
+    return _get_total_seconds(diff)
 
 def stabsformat(sdate, dbase = None):
     """ Constructs a datetime object from a string date.
@@ -110,3 +113,11 @@ def stabsformat(sdate, dbase = None):
 
     return None
 
+def compute_delay_ms(timestamp2, timestamp1):
+    d1 = datetime.datetime.fromtimestamp(float(timestamp1))
+    d2 = datetime.datetime.fromtimestamp(float(timestamp2))
+    delay = d2 - d1
+
+    # round up resolution - round up to miliseconds
+    return delay.total_seconds() * 1000
+