skytools.timeutil: fixed for Python versions less than 2.7
authormartinko <gamato@users.sf.net>
Tue, 8 Apr 2014 09:45:12 +0000 (11:45 +0200)
committermartinko <gamato@users.sf.net>
Tue, 8 Apr 2014 09:45:12 +0000 (11:45 +0200)
python/skytools/timeutil.py

index 2ea63082a2233c65d61c693a612165e4294cb35e..c04756b89ac2bff0d2522509051f5de9214d8cb7 100644 (file)
@@ -16,6 +16,20 @@ from datetime import datetime, timedelta, tzinfo
 
 __all__ = ['parse_iso_timestamp', 'FixedOffsetTimezone', 'datetime_to_timestamp']
 
+try:
+    timedelta.total_seconds # new in 2.7
+except AttributeError:
+    def total_seconds(td):
+        return float (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
+
+    import ctypes
+    _get_dict = ctypes.pythonapi._PyObject_GetDictPtr
+    _get_dict.restype = ctypes.POINTER(ctypes.py_object)
+    _get_dict.argtypes = [ctypes.py_object]
+    d = _get_dict(timedelta)[0]
+    d['total_seconds'] = total_seconds
+
+
 class FixedOffsetTimezone(tzinfo):
     """Fixed offset in minutes east from UTC."""
     __slots__ = ('__offset', '__name')