Convert all datetimes to UTC when doing comparisons
This commit is contained in:
parent
29eef159d5
commit
4210f5c776
3 changed files with 22 additions and 10 deletions
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue