Time: Add support for omitting space in @seconds

This commit is contained in:
Valentin Lorentz 2022-08-02 12:59:27 +02:00
parent cd0f9f2628
commit 8f837a676d
2 changed files with 17 additions and 5 deletions

View File

@ -72,10 +72,13 @@ try:
except ImportError:
tzlocal = None
_SECONDS_SPLIT_RE = re.compile('(?<=[a-z]) ?')
class Time(callbacks.Plugin):
"""This plugin allows you to use different time-related functions."""
@internationalizeDocstring
def seconds(self, irc, msg, args):
def seconds(self, irc, msg, args, text):
"""[<years>y] [<weeks>w] [<days>d] [<hours>h] [<minutes>m] [<seconds>s]
Returns the number of seconds in the number of <years>, <weeks>,
@ -84,11 +87,13 @@ class Time(callbacks.Plugin):
Useful for scheduling events at a given number of seconds in the
future.
"""
if not args:
raise callbacks.ArgumentError
seconds = 0
for arg in args:
if not arg or arg[-1] not in 'ywdhms':
if not text:
raise callbacks.ArgumentError
for arg in _SECONDS_SPLIT_RE.split(text):
if not arg:
continue
if arg[-1] not in 'ywdhms':
raise callbacks.ArgumentError
(s, kind) = arg[:-1], arg[-1]
try:
@ -108,6 +113,7 @@ class Time(callbacks.Plugin):
elif kind == 's':
seconds += i
irc.reply(str(seconds))
seconds = wrap(seconds, ['text'])
@internationalizeDocstring
def at(self, irc, msg, args, s=None):

View File

@ -77,16 +77,22 @@ class TimeTestCase(PluginTestCase):
self.assertResponse('seconds 10s', '10')
self.assertResponse('seconds 1m', '60')
self.assertResponse('seconds 1m 1s', '61')
self.assertResponse('seconds 1m1s', '61')
self.assertResponse('seconds 1h', '3600')
self.assertResponse('seconds 1h 1s', '3601')
self.assertResponse('seconds 1h1s', '3601')
self.assertResponse('seconds 1d', '86400')
self.assertResponse('seconds 1d 1s', '86401')
self.assertResponse('seconds 1d1s', '86401')
self.assertResponse('seconds 2s', '2')
self.assertResponse('seconds 2m', '120')
self.assertResponse('seconds 2d 2h 2m 2s', '180122')
self.assertResponse('seconds 2d2h2m2s', '180122')
self.assertResponse('seconds 1s', '1')
self.assertResponse('seconds 1y 1s', '31536001')
self.assertResponse('seconds 1y1s', '31536001')
self.assertResponse('seconds 1w 1s', '604801')
self.assertResponse('seconds 1w1s', '604801')
def testNoErrors(self):
self.assertNotError('ctime')