summaryrefslogtreecommitdiff
path: root/src/common.py
diff options
context:
space:
mode:
authormathieui <mathieui@mathieui.net>2014-03-31 22:56:29 +0200
committermathieui <mathieui@mathieui.net>2014-04-01 00:03:29 +0200
commit02d9fd9ad4816f357c614a230b34d7e3fcdcdcf9 (patch)
treee01b0d03cf9527a8615cf8a9ecc3194366178419 /src/common.py
parenta0c7155140f50b2b1c22e7c1347be1024d4e3af8 (diff)
downloadpoezio-02d9fd9ad4816f357c614a230b34d7e3fcdcdcf9.tar.gz
poezio-02d9fd9ad4816f357c614a230b34d7e3fcdcdcf9.tar.bz2
poezio-02d9fd9ad4816f357c614a230b34d7e3fcdcdcf9.tar.xz
poezio-02d9fd9ad4816f357c614a230b34d7e3fcdcdcf9.zip
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)
Diffstat (limited to 'src/common.py')
-rw-r--r--src/common.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/common.py b/src/common.py
index 8fbf34fb..5467c985 100644
--- a/src/common.py
+++ b/src/common.py
@@ -218,6 +218,53 @@ def datetime_tuple(timestamp):
ret += tz
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):
"""
Check if a message is delayed or not.