Implement time zone aware expiration comparisons

Instead of using a naive time comparison, check times relative to
the user's time zone (shifted to UTC) and the expiration times
embedded in NWS documents (implicitly UTC).

Drop the ugly hack of the 24-hour expiry delay, but keep it at 1
hour to still provide some protection against premature filtering in
case someone switches into or out of DST on a different date than
NWS expects it to happen.
This commit is contained in:
Jeremy Stanley
2024-05-11 00:57:27 +00:00
parent c9bc48f456
commit 5b44af7bec

View File

@@ -341,26 +341,20 @@ def get_alert(
muted = True muted = True
else: else:
muted = False muted = False
expirycheck = re.search(r"Expires:([0-9]{12})", alert)
if expirycheck:
# only report alerts and forecasts that expired less than
# offset ago
import datetime, zoneinfo
expiration = datetime.datetime.fromisoformat(
"%sT%sZ" % (expirycheck[1][:8], expirycheck[1][-4:]))
now = datetime.datetime.now(tz=zoneinfo.ZoneInfo("UTC"))
# TODO: make this offset configurable
if now - expiration > datetime.timedelta(hours=1):
return ""
lines = alert.split("\n") lines = alert.split("\n")
import time
# TODO: make this offset configurable
# TODO: adjust offset relative to the difference between the user's
# local time and the zone's local time (will need to extend
# the schema in the zones file to store each tz
offset = 86400 # one day
# report alerts and forecasts that expired less than offset ago;
# this is a cheap hack since expiration times seem to be relative
# to the zone's local time zone, and converting from the user's
# would get complicated, but also there can sometimes be a lag
# between expiration and the next update
valid_time = time.strftime(
"%Y%m%d%H%M", time.localtime(time.time() - offset))
output = [] output = []
for line in lines: for line in lines:
if line.startswith("Expires:") \
and "Expires:" + valid_time > line:
return ""
if muted and line.startswith("National Weather Service"): if muted and line.startswith("National Weather Service"):
muted = False muted = False
line = "" line = ""