mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-12-25 12:12:54 +01:00
Time: Add support for UTC offsets as tztime arguments.
This commit is contained in:
parent
4f3d6fc39f
commit
21ea999e3c
@ -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."""
|
||||||
try:
|
parsed_utc_tz = re.match(
|
||||||
timezone = utils.time.iana_timezone(timezone)
|
"UTC(?P<hours>[-+][0-9]+)(:(?P<minutes>[0-6][0-9]))?", tz)
|
||||||
except utils.time.UnknownTimeZone:
|
if parsed_utc_tz:
|
||||||
irc.error(_('Unknown timezone'))
|
groups = parsed_utc_tz.groupdict()
|
||||||
except utils.time.MissingTimezoneLibrary:
|
tz = timezone(timedelta(
|
||||||
irc.error(_(
|
hours=int(groups["hours"]),
|
||||||
'Timezone-related commands are not available. '
|
minutes=int(groups["minutes"] or "00")
|
||||||
'Your administrator need to either upgrade Python to '
|
))
|
||||||
'version 3.9 or greater, or install pytz.'))
|
|
||||||
except utils.time.TimezoneException as e:
|
|
||||||
irc.error(e.args[0])
|
|
||||||
else:
|
else:
|
||||||
format = self.registryValue("format", msg.channel, irc.network)
|
try:
|
||||||
irc.reply(datetime.now(timezone).strftime(format))
|
tz = utils.time.iana_timezone(tz)
|
||||||
|
except utils.time.UnknownTimeZone:
|
||||||
|
irc.error(_('Unknown timezone'), Raise=True)
|
||||||
|
except utils.time.MissingTimezoneLibrary:
|
||||||
|
irc.error(_(
|
||||||
|
'Timezone-related commands are not available. '
|
||||||
|
'Your administrator need to either upgrade Python to '
|
||||||
|
'version 3.9 or greater, or install pytz.'),
|
||||||
|
Raise=True)
|
||||||
|
except utils.time.TimezoneException as e:
|
||||||
|
irc.error(e.args[0], Raise=True)
|
||||||
|
|
||||||
|
format = self.registryValue("format", msg.channel, irc.network)
|
||||||
|
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):
|
||||||
|
@ -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')
|
||||||
|
Loading…
Reference in New Issue
Block a user