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

[tor-commits] [stem/master] Fix datetime_to_unix() for python2.6



commit 5570f17af56125a5d7c0e81d4aeacd44dca40860
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Sun Oct 7 15:48:34 2018 -0700

    Fix datetime_to_unix() for python2.6
    
    Huh. With a 2018 date our datetime_to_unix() python2.6 workaround was off by an
    hour, breaking one of the unit tests I added for stem.client's NETINFO cells.
    
    Honestly not entirely sure what's up, but this is a far cleaner way of
    converting datetimes to unix timestamps. Works too.
---
 stem/util/__init__.py      | 3 ++-
 test/unit/util/__init__.py | 1 +
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/stem/util/__init__.py b/stem/util/__init__.py
index 91d96bcf..fba506eb 100644
--- a/stem/util/__init__.py
+++ b/stem/util/__init__.py
@@ -119,7 +119,8 @@ def datetime_to_unix(timestamp):
   """
 
   if stem.prereq._is_python_26():
-    return int(timestamp.strftime('%s')) - int(datetime.datetime(1970, 1, 1).strftime('%s')) + 3600
+    delta = (timestamp - datetime.datetime(1970, 1, 1))
+    return delta.days * 86400 + delta.seconds
   else:
     return (timestamp - datetime.datetime(1970, 1, 1)).total_seconds()
 
diff --git a/test/unit/util/__init__.py b/test/unit/util/__init__.py
index 89190f7e..c29901b1 100644
--- a/test/unit/util/__init__.py
+++ b/test/unit/util/__init__.py
@@ -23,4 +23,5 @@ __all__ = [
 class TestBaseUtil(unittest.TestCase):
   def test_datetime_to_unix(self):
     self.assertEqual(1344251971.0, datetime_to_unix(datetime.datetime(2012, 8, 6, 11, 19, 31)))
+    self.assertEqual(1515894416.0, datetime_to_unix(datetime.datetime(2018, 1, 14, 1, 46, 56)))
     self.assertTrue((time.time() - datetime_to_unix(datetime.datetime.utcnow())) < 2)

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