Skip to content

Commit

Permalink
[tools, clock] Add helpers for time zone and format
Browse files Browse the repository at this point in the history
Also start using it in the clock module.
  • Loading branch information
embolalia committed Feb 2, 2014
1 parent f655afd commit 3acff29
Showing 1 changed file with 8 additions and 36 deletions.
44 changes: 8 additions & 36 deletions clock.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#coding: utf8
"""
clock.py - Willie Clock Module
Copyright 2008-9, Sean B. Palmer, inamidst.com
Expand All @@ -6,9 +7,12 @@
http://willie.dfbta.net
"""
from __future__ import unicode_literals

import pytz
import datetime
from willie.module import commands, example, OP
from willie.tools import get_timezone, format_time


def configure(config):
Expand All @@ -30,42 +34,10 @@ def setup(bot):
@example('.t America/New_York')
def f_time(bot, trigger):
"""Returns the current time."""
tz = trigger.group(2)
if tz:
tz = tz.strip()
#We have a tz. If it's in all_timezones, we don't need to do anything
#more, because we know it's valid. Otherwise, we have to check if it's
#supposed to be a user, or just invalid
if tz not in pytz.all_timezones:
if bot.db and tz in bot.db.preferences:
tz = bot.db.preferences.get(tz, 'tz')
if not tz:
bot.say("I'm sorry, I don't know %s's timezone"
% trigger.group(2))
return
else:
bot.say("I'm sorry, I don't know about the %s timezone or"
" user. Try one from http://dft.ba/-tz" % tz)
return
#We don't have a timzeone. Is there one set? If not, just use UTC
elif bot.db:
if trigger.nick in bot.db.preferences:
tz = bot.db.preferences.get(trigger.nick, 'tz')
if not tz and trigger.sender in bot.db.preferences:
tz = bot.db.preferences.get(trigger.sender, 'tz')
if not tz and bot.config.has_option('clock', 'tz'):
tz = bot.config.clock.tz

now = datetime.datetime.now(pytz.timezone(tz or 'UTC'))

tformat = ''
if bot.db:
if trigger.nick in bot.db.preferences:
tformat = bot.db.preferences.get(trigger.nick, 'time_format')
if not tformat and trigger.sender in bot.db.preferences:
tformat = bot.db.preferences.get(trigger.sender, 'time_format')

bot.say(now.strftime(tformat or "%F - %T%Z"))
zone = get_timezone(bot.db, bot.config, trigger.group(2), trigger.nick,
trigger.sender)
time = format_time(bot.db, bot.config, zone, trigger.nick, trigger.sender)
bot.say(time)


@commands('settz')
Expand Down

0 comments on commit 3acff29

Please sign in to comment.