-
Notifications
You must be signed in to change notification settings - Fork 6
/
timeBot.py
60 lines (48 loc) · 1.72 KB
/
timeBot.py
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
import datetime
import pytz
from errbot import BotPlugin, botcmd
FMT = "%Y-%m-%d %H:%M:%S %Z%z"
def find_tz(city):
for timezone in pytz.all_timezones_set:
if city in timezone:
return timezone
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:
raw_name = timezone.rpartition("/")[2]
name = raw_name.replace("_", " ")
cities.append(name)
country_timezones[country_name] = cities
return country_timezones
class TimeBot(BotPlugin):
@botcmd(split_args_with=" ")
def time(self, msg, 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 f"Current time in {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"
return answer