From 4adfb9cfc1c2b8ff6646d8dfee412796b63080e0 Mon Sep 17 00:00:00 2001 From: Thor77 Date: Wed, 15 Feb 2017 21:33:49 +0100 Subject: [PATCH] Make all datetime-objects timezone-aware Because the tool is using utc-timestamps everywhere, this emphasizes this fact (by default) in the output. If you don't want timezones behind each datetime in your output, just remove the "%Z" from the `datetimeformat`. Fix #9 --- tsstats/log.py | 7 ++++--- tsstats/template.py | 6 ++++-- tsstats/utils.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/tsstats/log.py b/tsstats/log.py index d391869..54745f1 100644 --- a/tsstats/log.py +++ b/tsstats/log.py @@ -8,6 +8,7 @@ from datetime import datetime from glob import glob from os.path import basename +from tsstats.utils import tz_aware_datime from tsstats.client import Client, Clients re_log_filename = re.compile(r'ts3server_(?P\d{4}-\d\d-\d\d)' @@ -133,8 +134,8 @@ def _parse_details(log_path, ident_map=None, clients=None, online_dc=True): logger.debug('No match: "%s"', line) continue match = match.groupdict() - logdatetime = datetime.strptime(match['timestamp'], - log_timestamp_format) + logdatetime = tz_aware_datime(datetime.strptime(match['timestamp'], + log_timestamp_format)) message = match['message'] if message.startswith('client'): match = re_dis_connect.match(message) @@ -173,7 +174,7 @@ def _parse_details(log_path, ident_map=None, clients=None, online_dc=True): ] if online_dc: def _reconnect(client): - client.disconnect(datetime.utcnow()) + client.disconnect(tz_aware_datime(datetime.utcnow())) client.connected += 1 [_reconnect(client) for client in clients if client.connected] logger.debug('Finished parsing of %s', log_file.name) diff --git a/tsstats/template.py b/tsstats/template.py index 59cc13e..8003478 100644 --- a/tsstats/template.py +++ b/tsstats/template.py @@ -8,7 +8,9 @@ from os.path import dirname, join from jinja2 import ChoiceLoader, Environment, FileSystemLoader, PackageLoader from tsstats.log import Server -from tsstats.utils import filter_threshold, seconds_to_text, sort_clients +from tsstats.utils import ( + filter_threshold, seconds_to_text, sort_clients, tz_aware_datime +) logger = logging.getLogger('tsstats') @@ -104,6 +106,6 @@ def render_servers(servers, output, title='TeamspeakStats', logger.debug('Rendering template %s', template) template.stream(title=title, servers=prepared_servers, debug=logger.level <= logging.DEBUG, - creation_time=datetime.utcnow())\ + creation_time=tz_aware_datime(datetime.utcnow()))\ .dump(output, encoding='utf-8') logger.debug('Wrote rendered template to %s', output) diff --git a/tsstats/utils.py b/tsstats/utils.py index e935624..d11f233 100644 --- a/tsstats/utils.py +++ b/tsstats/utils.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +import datetime def sort_clients(clients, key_l): @@ -49,3 +50,31 @@ def filter_threshold(clients, threshold): :rtype: list ''' return list(filter(lambda c: c[1] > threshold, clients)) + + +class UTC(datetime.tzinfo): + ''' + Reimplementation of `timezone.utc` for Python2-Compatibility + ''' + + def utcoffset(self, dt): + return datetime.timedelta(0) + + def dst(self, dt): + return datetime.timedelta(0) + + def tzname(self, dt): + return 'UTC' + + +def tz_aware_datime(datetime, timezone=UTC()): + ''' + Make `datetime` aware of it's timezone (UTC by default) + + :param datetime: Target datetime + :param timezone: Target timezone + + :type datetime: datetime.datetime + :type timezone: datetime.timezone + ''' + return datetime.replace(tzinfo=timezone)