From: martinko Date: Tue, 8 Apr 2014 09:45:12 +0000 (+0200) Subject: skytools.timeutil: fixed for Python versions less than 2.7 X-Git-Tag: 3.2.1^2~4^2 X-Git-Url: http://waps.l3s.uni-hannover.de/gitweb/?a=commitdiff_plain;h=c4640cc4f5097a3b50d5ecca5ae503bcfacddb8f;p=skytools.git skytools.timeutil: fixed for Python versions less than 2.7 --- 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')