forked from errbotio/err-time
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeBot.py
More file actions
68 lines (61 loc) · 2.24 KB
/
timeBot.py
File metadata and controls
68 lines (61 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import datetime, pytz
import re
# Backward compatibility
from errbot.version import VERSION
from errbot.utils import version2array
if version2array(VERSION) >= [1,6,0]:
from errbot import botcmd, BotPlugin
else:
from errbot.botplugin import BotPlugin
from errbot.jabberbot import botcmd
tzs = pytz.all_timezones_set
fmt = '%Y-%m-%d %H:%M:%S %Z%z'
def find_tz(city):
for x in pytz.all_timezones_set:
if city in x:
return x
return None
def get_all_tznames():
country_timezones = {}
for (country, tzlist) in pytz.country_timezones.items():
country_name = pytz.country_names[country]
cities = []
for timezone in tzlist:
city = re.sub(r'^[^/]*/', r'', timezone)
city = re.sub(country_name + '/', '', city)
city = re.sub(r'^([^/]*)/(.*)', r'\2, \1', city)
city = re.sub(r'_', r' ', city)
cities.append(city)
country_timezones[country_name] = cities
return country_timezones
class TimeBot(BotPlugin):
@botcmd(split_args_with = ' ')
def time(self, mess, args):
""" Shows the current time for given city.
Example: !time San Francisco
"""
if not args:
return 'Am I supposed to guess the location?...'
if len(args) == 1 and args[0].lower() == 'utc':
tz_name = 'UTC'
else:
city = '_'.join([word.capitalize() for word in args])
tz_name = find_tz(city)
if not tz_name:
return 'Sorry cannot find this city, you can list them with !tzlist'
tz = pytz.timezone(tz_name)
local_time = datetime.datetime.now(tz)
return 'Current time in %s : %s' % (tz_name, local_time.strftime(fmt))
@botcmd
def tzlist(self, mess, args):
""" List all the known cities
"""
country_timezones = get_all_tznames()
answer = ''
for country in sorted(country_timezones):
answer+=country + ':\n'
for city in sorted(country_timezones[country]):
answer+='\t' + city + '\n'
# Send the output to the user to prevent spamming the channel
direct_to_user = self.build_identifier(str(mess.frm.nick))
self.send(direct_to_user, answer)