[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [sbws/maint-1.1] new: util: Add function to know if timestamp is old



commit e83872584eee1fddb36babab25089a98f10b0d85
Author: juga0 <juga@xxxxxxxxxx>
Date:   Sun Jun 16 12:54:42 2019 +0000

    new: util: Add function to know if timestamp is old
    
    Part of #30727.
---
 sbws/util/timestamp.py | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/sbws/util/timestamp.py b/sbws/util/timestamp.py
index ff3efd8..4c3fb34 100644
--- a/sbws/util/timestamp.py
+++ b/sbws/util/timestamp.py
@@ -1,5 +1,7 @@
 """Util functions to convert between timestamp formats"""
-from datetime import datetime
+from datetime import datetime, timedelta
+
+from ..globals import MEASUREMENTS_PERIOD
 
 
 def dt_obj_to_isodt_str(dt):
@@ -71,3 +73,22 @@ def unixts_to_str(unixts):
     # than int or float
     assert isinstance(unixts, int) or isinstance(unixts, float)
     return str(unixts)
+
+
+# XXX: tech-debt: replace all the code that check whether a
+# measurement or relay is older than the measurement period by this.
+def is_old(timestamp, measurements_period=MEASUREMENTS_PERIOD):
+    """Whether the given timestamp is older that the given measurements
+    period.
+    """
+    if not isinstance(timestamp, datetime):
+        if isinstance(timestamp, str):
+            # This will raise an exception if the string is not correctly
+            # formatted.
+            timestamp = isostr_to_dt_obj(timestamp)
+        else:
+            # This will raise an exception if the type is not int or float or
+            # is not actually a timestamp
+            timestamp = unixts_to_dt_obj(timestamp)
+    oldest_date = datetime.utcnow() - timedelta(measurements_period)
+    return timestamp > oldest_date



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits