Merge pull request #982 from GLolol/time-1

Time: refactor code, make tztime respect plugins.Time.format
This commit is contained in:
Valentin Lorentz 2015-01-03 20:18:38 +01:00
commit 7012143c1d

View File

@ -63,6 +63,11 @@ try:
except ImportError: except ImportError:
tzlocal = None tzlocal = None
try:
import pytz
except ImportError:
pytz = None
class Time(callbacks.Plugin): class Time(callbacks.Plugin):
"""This plugin allows you to use different time-related functions.""" """This plugin allows you to use different time-related functions."""
@internationalizeDocstring @internationalizeDocstring
@ -155,17 +160,15 @@ class Time(callbacks.Plugin):
@internationalizeDocstring @internationalizeDocstring
def time(self, irc, msg, args, channel, format, seconds): def time(self, irc, msg, args, channel, format, seconds):
"""[<format>] [<seconds since epoch>] """[<channel>] [<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. If
<channel> is given without <format>, uses the format for <channel>.
""" """
if not format: if not format:
if channel: format = self.registryValue('format', channel or msg.args[0])
format = self.registryValue('format', channel)
else:
format = self.registryValue('format')
if tzlocal: if tzlocal:
irc.reply(datetime.fromtimestamp(seconds, tzlocal()).strftime(format)) irc.reply(datetime.fromtimestamp(seconds, tzlocal()).strftime(format))
else: else:
@ -190,20 +193,17 @@ class Time(callbacks.Plugin):
def tztime(self, irc, msg, args, timezone): def tztime(self, irc, msg, args, timezone):
"""<region>/<city> """<region>/<city>
Takes a city and its region, and returns the locale 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: if pytz is None:
import pytz
except ImportError:
irc.error(_('Python-tz is required by the command, but is not ' irc.error(_('Python-tz is required by the command, but is not '
'installed on this computer.')) 'installed on this computer.'), Raise=True)
return
try: try:
timezone = pytz.timezone(timezone) timezone = pytz.timezone(timezone)
except pytz.UnknownTimeZoneError: except pytz.UnknownTimeZoneError:
irc.error(_('Unknown timezone')) irc.error(_('Unknown timezone'), Raise=True)
return format = self.registryValue("format", msg.args[0])
irc.reply(datetime.now(timezone).strftime('%F %T%z')) irc.reply(datetime.now(timezone).strftime(format))
tztime = wrap(tztime, ['text']) tztime = wrap(tztime, ['text'])