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
This commit is contained in:
Thor77 2017-02-15 21:33:49 +01:00
parent ba8b393b76
commit 4adfb9cfc1
3 changed files with 37 additions and 5 deletions

View File

@ -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<date>\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)

View File

@ -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)

View File

@ -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)