[tor-commits] [ooni-probe/master] Remove dead/unnecessary code from otime

art at torproject.org art at torproject.org
Fri Apr 29 09:42:25 UTC 2016


commit 7528c07975edbd45776086ccaf329e0aed923d35
Author: Arturo Filastò <arturo at filasto.net>
Date:   Tue Mar 22 17:33:12 2016 +0100

    Remove dead/unnecessary code from otime
    
    * Minor naming changes to functions
---
 ooni/nettest.py          |  2 +-
 ooni/otime.py            | 80 +++---------------------------------------------
 ooni/tasks.py            |  2 +-
 ooni/tests/test_otime.py | 14 ---------
 ooni/utils/log.py        |  2 +-
 5 files changed, 8 insertions(+), 92 deletions(-)

diff --git a/ooni/nettest.py b/ooni/nettest.py
index 9c5e171..6cd9f57 100644
--- a/ooni/nettest.py
+++ b/ooni/nettest.py
@@ -206,7 +206,7 @@ class NetTestLoader(object):
 
         options = sanitize_options(self.options)
         self.testDetails = {
-            'test_start_time': otime.UTCTimestampNow(),
+            'test_start_time': otime.timestampNowLongUTC(),
             'probe_asn': config.probe_ip.geodata['asn'],
             'probe_cc': config.probe_ip.geodata['countrycode'],
             'probe_ip': config.probe_ip.geodata['ip'],
diff --git a/ooni/otime.py b/ooni/otime.py
index 37ada02..ffa95d0 100644
--- a/ooni/otime.py
+++ b/ooni/otime.py
@@ -1,17 +1,4 @@
-from datetime import datetime, timedelta, tzinfo
-
-class UTC(tzinfo):
-    """UTC"""
-    ZERO = timedelta(0)
-
-    def utcoffset(self, dt):
-        return self.ZERO
-
-    def tzname(self, dt):
-        return "UTC"
-
-    def dst(self, dt):
-        return self.ZERO
+from datetime import datetime
 
 def prettyDateNow():
     """
@@ -19,72 +6,15 @@ def prettyDateNow():
     """
     return datetime.now().ctime()
 
-def utcPrettyDateNow():
+def prettyDateNowUTC():
     """
     Returns a good looking string for utc time.
     """
     return datetime.utcnow().ctime()
 
-class InvalidTimestampFormat(Exception):
-    pass
-
-def fromTimestamp(s):
-    """
-    Converts a string that is output from the timestamp function back to a
-    datetime object
-
-    Args:
-        s (str): a ISO8601 formatted string.
-            ex. 1912-06-23T101234Z"
-
-    Note: we currently only support parsing strings that are generated from the
-        timestamp function and have no intention in supporting the full standard.
-    """
-    try:
-        date_part, time_part = s.split('T')
-        hours, minutes, seconds = time_part[:2], time_part[2:4], time_part[4:6]
-        year, month, day = date_part.split('-')
-    except:
-        raise InvalidTimestampFormat(s)
-
-    return datetime(int(year), int(month), int(day), int(hours), int(minutes),
-            int(seconds))
-
-def timestamp(t=None):
+def timestampNowLongUTC():
     """
-    The timestamp for ooni reports follows ISO 8601 in
-    UTC time format.
-    We do not inlcude ':' and include seconds.
-
-    Example:
-
-        if the current date is "10:12:34 AM, June 23 1912" (datetime(1912, 6,
-            23, 10, 12, 34))
-
-        the timestamp will be:
-
-           "1912-06-23T101234Z"
-
-    Args:
-        t (datetime): a datetime object representing the
-            time to be represented (*MUST* be expressed
-            in UTC).
-
-        If not specified will default to the current time
-        in UTC.
+    Returns a timestamp in the format of %Y-%m-%d %H:%M:%S in Universal Time
+    Coordinates.
     """
-    if not t:
-        t = datetime.utcnow()
-    ISO8601 = "%Y-%m-%dT%H%M%SZ"
-    return t.strftime(ISO8601)
-
-
-def epochToTimestamp(seconds):
-    return timestamp(datetime.fromtimestamp(seconds, UTC()))
-
-
-def epochToUTC(seconds):
-    return float(datetime.utcfromtimestamp(seconds).strftime("%s"))
-
-def UTCTimestampNow():
     return datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
diff --git a/ooni/tasks.py b/ooni/tasks.py
index 381902a..ac128d6 100644
--- a/ooni/tasks.py
+++ b/ooni/tasks.py
@@ -140,7 +140,7 @@ class Measurement(TaskWithTimeout):
 
     def run(self):
         if 'measurement_start_time' not in self.testInstance.report.keys():
-            self.testInstance.report['measurement_start_time'] = otime.UTCTimestampNow()
+            self.testInstance.report['measurement_start_time'] = otime.timestampNowLongUTC()
         return self.netTestMethod()
 
 
diff --git a/ooni/tests/test_otime.py b/ooni/tests/test_otime.py
deleted file mode 100644
index f3997eb..0000000
--- a/ooni/tests/test_otime.py
+++ /dev/null
@@ -1,14 +0,0 @@
-import unittest
-from datetime import datetime
-from ooni import otime
-
-test_date = datetime(2002, 6, 26, 22, 45, 49)
-
-
-class TestOtime(unittest.TestCase):
-    def test_timestamp(self):
-        self.assertEqual(otime.timestamp(test_date), "2002-06-26T224549Z")
-
-    def test_fromTimestamp(self):
-        time_stamp = otime.timestamp(test_date)
-        self.assertEqual(test_date, otime.fromTimestamp(time_stamp))
diff --git a/ooni/utils/log.py b/ooni/utils/log.py
index beba78f..d3036f0 100644
--- a/ooni/utils/log.py
+++ b/ooni/utils/log.py
@@ -64,7 +64,7 @@ class OONILogger(object):
 
         txlog.msg("Starting %s on %s (%s UTC)" % (application_name,
                                                   otime.prettyDateNow(),
-                                                  otime.utcPrettyDateNow()))
+                                                  otime.prettyDateNowUTC()))
 
         self.fileObserver = txlog.FileLogObserver(daily_logfile)
         self.stdoutObserver = LogWithNoPrefix(sys.stdout)





More information about the tor-commits mailing list