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 datetime
import re
from supybot import conf, utils, plugins, ircutils, callbacks from supybot import conf, utils, plugins, ircutils, callbacks
from supybot.commands import * from supybot.commands import *
@ -133,15 +134,22 @@ class Geography(callbacks.Plugin):
if timezone is None: if timezone is None:
continue 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 # Extract a human-friendly name, depending on the type of
# the timezone object: # the timezone object:
if hasattr(timezone, "key"): if hasattr(timezone, "key"):
# instance of zoneinfo.ZoneInfo # instance of zoneinfo.ZoneInfo
irc.reply(timezone.key) irc.reply(format("%s (currently UTC%s)", timezone.key, offset))
return return
elif hasattr(timezone, "zone"): elif hasattr(timezone, "zone"):
# instance of pytz.timezone # instance of pytz.timezone
irc.reply(timezone.zone) irc.reply(format("%s (currently UTC%s)", timezone.zone, offset))
return return
else: else:
# probably datetime.timezone built from a constant offset # probably datetime.timezone built from a constant offset

View File

@ -70,7 +70,9 @@ class GeographyTimezoneTestCase(PluginTestCase):
tz = pytz.timezone("Europe/Paris") tz = pytz.timezone("Europe/Paris")
with patch.object(wikidata, "timezone_from_uri", return_value=tz): 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") @skipIf(not zoneinfo, "Python is older than 3.9")
@mock @mock
@ -78,7 +80,9 @@ class GeographyTimezoneTestCase(PluginTestCase):
tz = zoneinfo.ZoneInfo("Europe/Paris") tz = zoneinfo.ZoneInfo("Europe/Paris")
with patch.object(wikidata, "timezone_from_uri", return_value=tz): 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") @skipIf(not zoneinfo, "Python is older than 3.9")
@mock @mock
@ -95,7 +99,9 @@ class GeographyTimezoneTestCase(PluginTestCase):
@skipIf(not network, "Network test") @skipIf(not network, "Network test")
def testTimezoneIntegration(self): 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") self.assertResponse("timezone Saint-Denis, La Réunion", "UTC+04:00")
@ -173,7 +179,7 @@ class GeographyWikidataTestCase(SupyTestCase):
@skipIf(not network, "Network test") @skipIf(not network, "Network test")
def testParentAndIgnoreSelf(self): def testParentAndIgnoreSelf(self):
# The queried object has a TZ property, but it's useless to us; # 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( self.assertEqual(
# New York City, NY # New York City, NY
wikidata.timezone_from_uri("http://www.wikidata.org/entity/Q60"), wikidata.timezone_from_uri("http://www.wikidata.org/entity/Q60"),