summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormartinko2014-04-08 09:45:12 +0000
committermartinko2014-04-08 09:45:12 +0000
commitc4640cc4f5097a3b50d5ecca5ae503bcfacddb8f (patch)
tree5750ed444d957a7ea6e2af16dd7be14c2b703a45
parent809b0a811f58a6747058c761301514f4fc40946f (diff)
skytools.timeutil: fixed for Python versions less than 2.7
-rw-r--r--python/skytools/timeutil.py14
1 files changed, 14 insertions, 0 deletions
diff --git a/python/skytools/timeutil.py b/python/skytools/timeutil.py
index 2ea63082..c04756b8 100644
--- a/python/skytools/timeutil.py
+++ b/python/skytools/timeutil.py
@@ -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')