Commit 02d9fd9a authored by mathieui's avatar mathieui

Fix #2462 (wrong timezone in the logs)

Now everything in the logs is in UTC time, and is converted when read

(also, actually return the logs after loading them instead of not doing
anything)
parent a0c71551
...@@ -218,6 +218,53 @@ def datetime_tuple(timestamp): ...@@ -218,6 +218,53 @@ def datetime_tuple(timestamp):
ret += tz ret += tz
return ret return ret
def get_utc_time(local_time=None):
"""
Get the current time in UTC
:param datetime local_time: The current local time
:return: The current UTC time
>>> delta = timedelta(seconds=-3600)
>>> d = datetime.now()
>>> time.timezone = -3600; time.altzone = -3600
>>> get_utc_time(local_time=d) == d + delta
True
"""
if local_time is None:
local_time = datetime.now()
isdst = time.localtime().tm_isdst
else:
isdst = time.localtime(int(local_time.timestamp())).tm_isdst
if time.daylight and isdst:
tz = timedelta(seconds=time.altzone)
else:
tz = timedelta(seconds=time.timezone)
utc_time = local_time + tz
return utc_time
def get_local_time(utc_time):
"""
Get the local time from an UTC time
>>> delta = timedelta(seconds=-3600)
>>> d = datetime.now()
>>> time.timezone = -3600; time.altzone = -3600
>>> get_local_time(d) == d - delta
True
"""
isdst = time.localtime(int(utc_time.timestamp())).tm_isdst
if time.daylight and isdst:
tz = timedelta(seconds=time.altzone)
else:
tz = timedelta(seconds=time.timezone)
local_time = utc_time - tz
return local_time
def find_delayed_tag(message): def find_delayed_tag(message):
""" """
Check if a message is delayed or not. Check if a message is delayed or not.
......
...@@ -5,11 +5,13 @@ ...@@ -5,11 +5,13 @@
# Poezio is free software: you can redistribute it and/or modify # Poezio is free software: you can redistribute it and/or modify
# it under the terms of the zlib license. See the COPYING file. # it under the terms of the zlib license. See the COPYING file.
from os import environ, makedirs
import mmap import mmap
import os import os
import re import re
from os import environ, makedirs
from datetime import datetime from datetime import datetime
import common
from config import config from config import config
from xhtml import clean_text from xhtml import clean_text
from theming import dump_tuple, get_theme from theming import dump_tuple, get_theme
...@@ -155,7 +157,9 @@ class Logger(object): ...@@ -155,7 +157,9 @@ class Logger(object):
log.debug('format? %s', tup) log.debug('format? %s', tup)
continue continue
time = [int(i) for index, i in enumerate(tup) if index < 6] time = [int(i) for index, i in enumerate(tup) if index < 6]
message = {'lines': [], 'history': True, 'time': datetime(*time)} message = {'lines': [],
'history': True,
'time': common.get_local_time(datetime(*time))}
size = int(tup[6]) size = int(tup[6])
if len(tup) == 8: #info line if len(tup) == 8: #info line
message['lines'].append(color+tup[7]) message['lines'].append(color+tup[7])
...@@ -195,9 +199,9 @@ class Logger(object): ...@@ -195,9 +199,9 @@ class Logger(object):
try: try:
msg = clean_text(msg) msg = clean_text(msg)
if date is None: if date is None:
str_time = datetime.now().strftime('%Y%m%dT%H:%M:%SZ') str_time = common.get_utc_time().strftime('%Y%m%dT%H:%M:%SZ')
else: else:
str_time = date.strftime('%Y%m%dT%H:%M:%SZ') str_time = common.get_utc_time(date).strftime('%Y%m%dT%H:%M:%SZ')
if typ == 1: if typ == 1:
prefix = 'MR' prefix = 'MR'
else: else:
...@@ -244,7 +248,7 @@ class Logger(object): ...@@ -244,7 +248,7 @@ class Logger(object):
exc_info=True) exc_info=True)
return False return False
try: try:
str_time = datetime.now().strftime('%Y%m%dT%H:%M:%SZ') str_time = common.get_utc_time().strftime('%Y%m%dT%H:%M:%SZ')
message = clean_text(message) message = clean_text(message)
lines = message.split('\n') lines = message.split('\n')
first_line = lines.pop(0) first_line = lines.pop(0)
......
...@@ -455,6 +455,7 @@ class ChatTab(Tab): ...@@ -455,6 +455,7 @@ class ChatTab(Tab):
def load_logs(self, log_nb): def load_logs(self, log_nb):
logs = logger.get_logs(safeJID(self.get_name()).bare, log_nb) logs = logger.get_logs(safeJID(self.get_name()).bare, log_nb)
return logs
def log_message(self, txt, nickname, time=None, typ=1): def log_message(self, txt, nickname, time=None, typ=1):
""" """
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment