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:
parent
ba8b393b76
commit
4adfb9cfc1
|
@ -8,6 +8,7 @@ from datetime import datetime
|
||||||
from glob import glob
|
from glob import glob
|
||||||
from os.path import basename
|
from os.path import basename
|
||||||
|
|
||||||
|
from tsstats.utils import tz_aware_datime
|
||||||
from tsstats.client import Client, Clients
|
from tsstats.client import Client, Clients
|
||||||
|
|
||||||
re_log_filename = re.compile(r'ts3server_(?P<date>\d{4}-\d\d-\d\d)'
|
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)
|
logger.debug('No match: "%s"', line)
|
||||||
continue
|
continue
|
||||||
match = match.groupdict()
|
match = match.groupdict()
|
||||||
logdatetime = datetime.strptime(match['timestamp'],
|
logdatetime = tz_aware_datime(datetime.strptime(match['timestamp'],
|
||||||
log_timestamp_format)
|
log_timestamp_format))
|
||||||
message = match['message']
|
message = match['message']
|
||||||
if message.startswith('client'):
|
if message.startswith('client'):
|
||||||
match = re_dis_connect.match(message)
|
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:
|
if online_dc:
|
||||||
def _reconnect(client):
|
def _reconnect(client):
|
||||||
client.disconnect(datetime.utcnow())
|
client.disconnect(tz_aware_datime(datetime.utcnow()))
|
||||||
client.connected += 1
|
client.connected += 1
|
||||||
[_reconnect(client) for client in clients if client.connected]
|
[_reconnect(client) for client in clients if client.connected]
|
||||||
logger.debug('Finished parsing of %s', log_file.name)
|
logger.debug('Finished parsing of %s', log_file.name)
|
||||||
|
|
|
@ -8,7 +8,9 @@ from os.path import dirname, join
|
||||||
from jinja2 import ChoiceLoader, Environment, FileSystemLoader, PackageLoader
|
from jinja2 import ChoiceLoader, Environment, FileSystemLoader, PackageLoader
|
||||||
|
|
||||||
from tsstats.log import Server
|
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')
|
logger = logging.getLogger('tsstats')
|
||||||
|
|
||||||
|
@ -104,6 +106,6 @@ def render_servers(servers, output, title='TeamspeakStats',
|
||||||
logger.debug('Rendering template %s', template)
|
logger.debug('Rendering template %s', template)
|
||||||
template.stream(title=title, servers=prepared_servers,
|
template.stream(title=title, servers=prepared_servers,
|
||||||
debug=logger.level <= logging.DEBUG,
|
debug=logger.level <= logging.DEBUG,
|
||||||
creation_time=datetime.utcnow())\
|
creation_time=tz_aware_datime(datetime.utcnow()))\
|
||||||
.dump(output, encoding='utf-8')
|
.dump(output, encoding='utf-8')
|
||||||
logger.debug('Wrote rendered template to %s', output)
|
logger.debug('Wrote rendered template to %s', output)
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
import datetime
|
||||||
|
|
||||||
|
|
||||||
def sort_clients(clients, key_l):
|
def sort_clients(clients, key_l):
|
||||||
|
@ -49,3 +50,31 @@ def filter_threshold(clients, threshold):
|
||||||
:rtype: list
|
:rtype: list
|
||||||
'''
|
'''
|
||||||
return list(filter(lambda c: c[1] > threshold, clients))
|
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)
|
||||||
|
|
Loading…
Reference in New Issue