Time: improvements to 'tztime'

- Import pytz on load, not every time the command is called
- Respect plugins.time.format
- Use irc.error(e, Raise=True) instead of return
This commit is contained in:
GLolol 2014-12-31 23:49:25 -05:00
parent a178e4a3d0
commit a1d949c861
1 changed files with 11 additions and 9 deletions

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
@ -190,20 +195,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'])