We can allow the at and until commands now.

This commit is contained in:
Jeremy Fincher 2004-09-30 07:41:05 +00:00
parent 27fe6554c7
commit a21d181a50

View File

@ -40,9 +40,13 @@ __contributors__ = {}
import supybot.plugins as plugins import supybot.plugins as plugins
import time import time
TIME = time
import dateutil.parser
import supybot.conf as conf import supybot.conf as conf
import supybot.utils as utils import supybot.utils as utils
from supybot.commands import wrap
import supybot.privmsgs as privmsgs import supybot.privmsgs as privmsgs
import supybot.registry as registry import supybot.registry as registry
import supybot.callbacks as callbacks import supybot.callbacks as callbacks
@ -63,6 +67,21 @@ conf.registerChannelValue(conf.supybot.plugins.Time, 'format',
time module to see what formats are accepted. If you set this variable to time module to see what formats are accepted. If you set this variable to
the empty string, the timestamp will not be shown.""")) the empty string, the timestamp will not be shown."""))
def parse(s):
todo = []
s = s.replace('noon', '12:00')
s = s.replace('midnight', '00:00')
if 'tomorrow' in s:
todo.append(lambda i: i + 86400)
s = s.replace('tomorrow', '')
if 'next week' in s:
todo.append(lambda i: i + 86400*7)
s = s.replace('next week', '')
i = int(time.mktime(dateutil.parser.parse(s, fuzzy=True).timetuple()))
for f in todo:
i = f(i)
return i
class Time(callbacks.Privmsg): class Time(callbacks.Privmsg):
def seconds(self, irc, msg, args): def seconds(self, irc, msg, args):
"""[<years>y] [<weeks>w] [<days>d] [<hours>h] [<minutes>m] [<seconds>s] """[<years>y] [<weeks>w] [<days>d] [<hours>h] [<minutes>m] [<seconds>s]
@ -98,21 +117,35 @@ class Time(callbacks.Privmsg):
seconds += i seconds += i
irc.reply(str(seconds)) irc.reply(str(seconds))
def _at(self, irc, msg, args): def at(self, irc, msg, args):
"""<time string> """<time string>
Returns the number of seconds since epoch <time string> is. Returns the number of seconds since epoch <time string> is.
<time string> can be any number of natural formats; just try something <time string> can be any number of natural formats; just try something
and see if it will work. and see if it will work.
""" """
now = int(time.time())
s = privmsgs.getArgs(args) s = privmsgs.getArgs(args)
new = parse(s)
if new != now:
irc.reply(new)
else:
irc.error('That\'s right now!')
def _until(self, irc, msg, args): def until(self, irc, msg, args):
"""<time string> """<time string>
Returns the number of seconds until <time string>. Returns the number of seconds until <time string>.
""" """
now = int(time.time())
s = privmsgs.getArgs(args) s = privmsgs.getArgs(args)
new = parse(s)
if new != now:
if new - now < 0:
new += 86400
irc.reply(new-now)
else:
irc.error('That\'s right now!')
def ctime(self, irc, msg, args): def ctime(self, irc, msg, args):
"""[<seconds since epoch>] """[<seconds since epoch>]
@ -130,28 +163,20 @@ class Time(callbacks.Privmsg):
seconds = time.time() seconds = time.time()
irc.reply(time.ctime(seconds)) irc.reply(time.ctime(seconds))
def time(self, irc, msg, args): def time(self, irc, msg, args, channel, format, seconds):
"""[<format>] [<seconds since epoch>] """[<format>] [<seconds since epoch>]
Returns the current time in <format> format, or, if <format> is not Returns the current time in <format> format, or, if <format> is not
given, uses the configurable format for the current channel. If no given, uses the configurable format for the current channel. If no
<seconds since epoch> time is given, the current time is used. <seconds since epoch> time is given, the current time is used.
""" """
channel = privmsgs.getChannel(msg, args, raiseError=False)
(format, seconds) = privmsgs.getArgs(args, required=0, optional=2)
if not format: if not format:
if channel: if channel:
format = self.registryValue('format', channel) format = self.registryValue('format', channel)
else: else:
format = self.registryValue('format') format = self.registryValue('format')
if not seconds:
seconds = time.time()
else:
try:
seconds = float(seconds)
except ValueError:
irc.errorInvalid('seconds', seconds, Raise=True)
irc.reply(time.strftime(format, time.localtime(seconds))) irc.reply(time.strftime(format, time.localtime(seconds)))
time = wrap(time, ['channel?', 'nonInt?', ('?int', TIME.time)])
Class = Time Class = Time