Time: Add support for UTC offsets as tztime arguments.

This commit is contained in:
Valentin Lorentz 2022-04-04 22:07:06 +02:00
parent 4f3d6fc39f
commit 21ea999e3c
2 changed files with 28 additions and 15 deletions

View File

@ -28,10 +28,11 @@
# POSSIBILITY OF SUCH DAMAGE. # POSSIBILITY OF SUCH DAMAGE.
### ###
import re
import sys import sys
import time import time
TIME = time # For later use. TIME = time # For later use.
from datetime import datetime from datetime import datetime, timedelta, timezone
import supybot.conf as conf import supybot.conf as conf
import supybot.log as log import supybot.log as log
@ -194,25 +195,35 @@ class Time(callbacks.Plugin):
elapsed = wrap(elapsed, ['int']) elapsed = wrap(elapsed, ['int'])
@internationalizeDocstring @internationalizeDocstring
def tztime(self, irc, msg, args, timezone): def tztime(self, irc, msg, args, tz):
"""<region>/<city> (or <region>/<state>/<city>) """<region>/<city> (or <region>/<state>/<city>)
Takes a city and its region, and returns its local time. This Takes a city and its region, and returns its local time. This
command uses the IANA Time Zone Database.""" command uses the IANA Time Zone Database."""
parsed_utc_tz = re.match(
"UTC(?P<hours>[-+][0-9]+)(:(?P<minutes>[0-6][0-9]))?", tz)
if parsed_utc_tz:
groups = parsed_utc_tz.groupdict()
tz = timezone(timedelta(
hours=int(groups["hours"]),
minutes=int(groups["minutes"] or "00")
))
else:
try: try:
timezone = utils.time.iana_timezone(timezone) tz = utils.time.iana_timezone(tz)
except utils.time.UnknownTimeZone: except utils.time.UnknownTimeZone:
irc.error(_('Unknown timezone')) irc.error(_('Unknown timezone'), Raise=True)
except utils.time.MissingTimezoneLibrary: except utils.time.MissingTimezoneLibrary:
irc.error(_( irc.error(_(
'Timezone-related commands are not available. ' 'Timezone-related commands are not available. '
'Your administrator need to either upgrade Python to ' 'Your administrator need to either upgrade Python to '
'version 3.9 or greater, or install pytz.')) 'version 3.9 or greater, or install pytz.'),
Raise=True)
except utils.time.TimezoneException as e: except utils.time.TimezoneException as e:
irc.error(e.args[0]) irc.error(e.args[0], Raise=True)
else:
format = self.registryValue("format", msg.channel, irc.network) format = self.registryValue("format", msg.channel, irc.network)
irc.reply(datetime.now(timezone).strftime(format)) irc.reply(datetime.now(tz).strftime(format))
tztime = wrap(tztime, ['text']) tztime = wrap(tztime, ['text'])
def ddate(self, irc, msg, args, year=None, month=None, day=None): def ddate(self, irc, msg, args, year=None, month=None, day=None):

View File

@ -97,6 +97,8 @@ class TimeTestCase(PluginTestCase):
self.assertNotError('tztime Europe/Paris') self.assertNotError('tztime Europe/Paris')
self.assertNotError('tztime America/Indiana/Knox') self.assertNotError('tztime America/Indiana/Knox')
self.assertNotError('tztime UTC') self.assertNotError('tztime UTC')
self.assertNotError('tztime UTC+10')
self.assertNotError('tztime UTC+5:30')
self.assertError('tztime Europe/Gniarf') self.assertError('tztime Europe/Gniarf')
@skipIf(not has_dateutil, 'python-dateutil is missing') @skipIf(not has_dateutil, 'python-dateutil is missing')