summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormathieui <mathieui@mathieui.net>2020-05-22 17:13:11 +0200
committermathieui <mathieui@mathieui.net>2020-05-23 16:13:44 +0200
commit4210f5c776dab2439cecb1e19dc3997109cab400 (patch)
tree847060410690cf0da16fe6c6bd95aec0ab49e07d
parent29eef159d50c836fbd7a27770775d63700dc7f19 (diff)
downloadpoezio-4210f5c776dab2439cecb1e19dc3997109cab400.tar.gz
poezio-4210f5c776dab2439cecb1e19dc3997109cab400.tar.bz2
poezio-4210f5c776dab2439cecb1e19dc3997109cab400.tar.xz
poezio-4210f5c776dab2439cecb1e19dc3997109cab400.zip
Convert all datetimes to UTC when doing comparisons
-rw-r--r--poezio/common.py17
-rw-r--r--poezio/mam.py11
-rw-r--r--poezio/tabs/muctab.py4
3 files changed, 22 insertions, 10 deletions
diff --git a/poezio/common.py b/poezio/common.py
index 7cddc306..98870dda 100644
--- a/poezio/common.py
+++ b/poezio/common.py
@@ -8,7 +8,11 @@
Various useful functions.
"""
-from datetime import datetime, timedelta
+from datetime import (
+ datetime,
+ timedelta,
+ timezone,
+)
from pathlib import Path
from typing import Dict, List, Optional, Tuple, Union
@@ -488,3 +492,14 @@ def unique_prefix_of(a: str, b: str) -> str:
return a[:i+1]
# both are equal, return a
return a
+
+
+def to_utc(time: datetime) -> datetime:
+ """Convert a datetime-aware time zone into raw UTC"""
+ tzone = datetime.now().astimezone().tzinfo
+ if time.tzinfo is not None: # Convert to UTC
+ time = time.astimezone(tz=timezone.utc)
+ else: # Assume local tz, convert to URC
+ time = time.replace(tzinfo=tzone).astimezone(tz=timezone.utc)
+ # Return an offset-naive datetime
+ return time.replace(tzinfo=None)
diff --git a/poezio/mam.py b/poezio/mam.py
index ee5b1be8..4171de5e 100644
--- a/poezio/mam.py
+++ b/poezio/mam.py
@@ -25,6 +25,7 @@ from poezio.theming import get_theme
from poezio import tabs
from poezio import xhtml, colors
from poezio.config import config
+from poezio.common import to_utc
from poezio.text_buffer import TextBuffer, HistoryGap
from poezio.ui.types import (
BaseMessage,
@@ -89,7 +90,6 @@ def make_line(
user=None,
)
-
async def get_mam_iterator(
core,
groupchat: bool,
@@ -181,14 +181,11 @@ async def fetch_history(tab: tabs.Tab,
break
if end is None:
end = datetime.now()
- tzone = datetime.now().astimezone().tzinfo
- end = end.replace(tzinfo=tzone).astimezone(tz=timezone.utc)
- end = end.replace(tzinfo=None)
+ end = to_utc(end)
end = datetime.strftime(end, '%Y-%m-%dT%H:%M:%SZ')
if start is not None:
- start = start.replace(tzinfo=tzone).astimezone(tz=timezone.utc)
- start = start.replace(tzinfo=None)
+ start = to_utc(start)
start = datetime.strftime(start, '%Y-%m-%dT%H:%M:%SZ')
mam_iterator = await get_mam_iterator(
@@ -224,7 +221,7 @@ async def on_new_tab_open(tab: tabs.Tab) -> None:
amount = 2 * tab.text_win.height
end = datetime.now()
for message in tab._text_buffer.messages:
- if isinstance(message, Message) and message.time < end:
+ if isinstance(message, Message) and to_utc(message.time) < to_utc(end):
end = message.time
break
end = end - timedelta(microseconds=1)
diff --git a/poezio/tabs/muctab.py b/poezio/tabs/muctab.py
index 0cb3d859..5939db31 100644
--- a/poezio/tabs/muctab.py
+++ b/poezio/tabs/muctab.py
@@ -31,7 +31,7 @@ from poezio import multiuserchat as muc
from poezio import timed_events
from poezio import windows
from poezio import xhtml
-from poezio.common import safeJID
+from poezio.common import safeJID, to_utc
from poezio.config import config
from poezio.core.structs import Command
from poezio.decorators import refresh_wrapper, command_args_parser
@@ -158,7 +158,7 @@ class MucTab(ChatTab):
"""
status = self.core.get_status()
if self.last_connection:
- delta = datetime.now() - self.last_connection
+ delta = to_utc(datetime.now()) - to_utc(self.last_connection)
seconds = delta.seconds + delta.days * 24 * 3600
else:
seconds = self._text_buffer.find_last_message()