Geography: Print current UTC offset next to timezones.

This commit is contained in:
Valentin Lorentz 2022-01-16 20:45:41 +01:00
parent 880efbbd27
commit da549e4fde
2 changed files with 20 additions and 6 deletions

View File

@ -29,6 +29,7 @@
###
import datetime
import re
from supybot import conf, utils, plugins, ircutils, callbacks
from supybot.commands import *
@ -133,15 +134,22 @@ class Geography(callbacks.Plugin):
if timezone is None:
continue
offset = str(datetime.datetime.now(tz=timezone).utcoffset())
if not offset.startswith("-"):
offset = "+" + offset
# hide seconds and minutes if they are zero
offset = re.sub("(:00)+$", "", offset)
# Extract a human-friendly name, depending on the type of
# the timezone object:
if hasattr(timezone, "key"):
# instance of zoneinfo.ZoneInfo
irc.reply(timezone.key)
irc.reply(format("%s (currently UTC%s)", timezone.key, offset))
return
elif hasattr(timezone, "zone"):
# instance of pytz.timezone
irc.reply(timezone.zone)
irc.reply(format("%s (currently UTC%s)", timezone.zone, offset))
return
else:
# probably datetime.timezone built from a constant offset

View File

@ -70,7 +70,9 @@ class GeographyTimezoneTestCase(PluginTestCase):
tz = pytz.timezone("Europe/Paris")
with patch.object(wikidata, "timezone_from_uri", return_value=tz):
self.assertResponse("timezone Foo Bar", "Europe/Paris")
self.assertRegexp(
"timezone Foo Bar", r"Europe/Paris \(currently UTC\+[12]\)"
)
@skipIf(not zoneinfo, "Python is older than 3.9")
@mock
@ -78,7 +80,9 @@ class GeographyTimezoneTestCase(PluginTestCase):
tz = zoneinfo.ZoneInfo("Europe/Paris")
with patch.object(wikidata, "timezone_from_uri", return_value=tz):
self.assertResponse("timezone Foo Bar", "Europe/Paris")
self.assertRegexp(
"timezone Foo Bar", r"Europe/Paris \(currently UTC\+[12]\)"
)
@skipIf(not zoneinfo, "Python is older than 3.9")
@mock
@ -95,7 +99,9 @@ class GeographyTimezoneTestCase(PluginTestCase):
@skipIf(not network, "Network test")
def testTimezoneIntegration(self):
self.assertResponse("timezone Metz, France", "Europe/Paris")
self.assertRegexp(
"timezone Metz, France", r"Europe/Paris \(currently UTC\+[12]\)"
)
self.assertResponse("timezone Saint-Denis, La Réunion", "UTC+04:00")
@ -173,7 +179,7 @@ class GeographyWikidataTestCase(SupyTestCase):
@skipIf(not network, "Network test")
def testParentAndIgnoreSelf(self):
# The queried object has a TZ property, but it's useless to us;
# however it is part of an object that has a useful one."""
# however it is part of an object that has a useful one.
self.assertEqual(
# New York City, NY
wikidata.timezone_from_uri("http://www.wikidata.org/entity/Q60"),