[tor-commits] [stem/master] Fix datetime_to_unix() for python 2.6

atagar at torproject.org atagar at torproject.org
Fri Jan 1 21:43:55 UTC 2016


commit 4729e7d8fac464b2c7375b0bc07ff094d549f6b4
Author: Damian Johnson <atagar at torproject.org>
Date:   Fri Jan 1 13:24:21 2016 -0800

    Fix datetime_to_unix() for python 2.6
    
    Datetime's total_seconds() method was added in python 2.7. I'm a tad unsure why
    I need an hour offset for this workaround but meh. Not timezones, so guess I'm
    just missing some off-by-one error.
    
      ======================================================================
      ERROR: test_datetime_to_unix
      ----------------------------------------------------------------------
      Traceback (most recent call last):
        File "/home/atagar/Desktop/stem/test/unit/util/__init__.py", line 24, in test_datetime_to_unix
          self.assertEqual(1344251971.0, datetime_to_unix(datetime.datetime(2012, 8, 6, 11, 19, 31)))
        File "/home/atagar/Desktop/stem/stem/util/__init__.py", line 37, in datetime_to_unix
          return (timestamp - datetime.datetime(1970, 1, 1)).total_seconds()
      AttributeError: 'datetime.timedelta' object has no attribute 'total_seconds'
    
      ----------------------------------------------------------------------
      Ran 1 test in 0.001s
    
      FAILED (errors=1)
---
 stem/util/__init__.py |    7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/stem/util/__init__.py b/stem/util/__init__.py
index 2ee9b9b..5f91801 100644
--- a/stem/util/__init__.py
+++ b/stem/util/__init__.py
@@ -7,6 +7,8 @@ Utility functions used by the stem library.
 
 import datetime
 
+import stem.prereq
+
 __all__ = [
   'conf',
   'connection',
@@ -34,4 +36,7 @@ def datetime_to_unix(timestamp):
   :returns: **float** for the unix timestamp of the given datetime object
   """
 
-  return (timestamp - datetime.datetime(1970, 1, 1)).total_seconds()
+  if stem.prereq._is_python_26():
+    return int(timestamp.strftime('%s')) - int(datetime.datetime(1970, 1, 1).strftime('%s')) + 3600
+  else:
+    return (timestamp - datetime.datetime(1970, 1, 1)).total_seconds()





More information about the tor-commits mailing list