Fix timezone handling in Python 2 and 3.2.

This commit is contained in:
Valentin Lorentz 2015-08-26 17:42:42 +02:00
parent 935abed77b
commit d1acdb6f92
2 changed files with 27 additions and 2 deletions

View File

@ -152,8 +152,8 @@ class IrcMsg(object):
if 'time' in self.server_tags: if 'time' in self.server_tags:
s = self.server_tags['time'] s = self.server_tags['time']
date = datetime.datetime.strptime(s, '%Y-%m-%dT%H:%M:%S.%fZ') date = datetime.datetime.strptime(s, '%Y-%m-%dT%H:%M:%S.%fZ')
date = date.replace(tzinfo=datetime.timezone.utc) date = minisix.make_datetime_utc(date)
self.time = date.timestamp() self.time = minisix.datetime__timestamp(date)
else: else:
self.time = time.time() self.time = time.time()
except (IndexError, ValueError): except (IndexError, ValueError):

View File

@ -45,6 +45,17 @@ if sys.version_info[0] >= 3:
u = lambda x:x u = lambda x:x
L = lambda x:x L = lambda x:x
def make_datetime_utc(dt):
import datetime
return dt.replace(tzinfo=datetime.timezone.utc)
if sys.version_info >= (3, 3):
def datetime__timestamp(dt):
return dt.timestamp()
else:
def datetime__timestamp(dt):
import datetime
return (dt - datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc)).total_seconds()
else: else:
PY2 = True PY2 = True
PY3 = False PY3 = False
@ -67,3 +78,17 @@ else:
u = lambda x:x.decode('utf8') u = lambda x:x.decode('utf8')
L = lambda x:long(x) L = lambda x:long(x)
def make_datetime_utc(dt):
from .. import log
log.warning('Timezones are not available on this version of '
'Python and may lead to incorrect results. You should '
'consider upgrading to Python 3.')
return dt.replace(tzinfo=None)
def datetime__timestamp(dt):
from .. import log
import datetime
log.warning('Timezones are not available on this version of '
'Python and may lead to incorrect results. You should '
'consider upgrading to Python 3.')
return (dt - datetime.datetime(1970, 1, 1)).total_seconds()