2019-05-25 20:40:06 +00:00
|
|
|
#--depends-on commands
|
|
|
|
#--depends-on location
|
|
|
|
|
2019-09-08 17:17:32 +00:00
|
|
|
import datetime, enum
|
2019-09-05 11:25:17 +00:00
|
|
|
import pytz
|
2019-04-28 11:12:00 +00:00
|
|
|
from src import ModuleManager, utils
|
|
|
|
|
2019-09-08 17:17:32 +00:00
|
|
|
NOLOCATION_USER = "%s doesn't have a location set"
|
|
|
|
NOLOCATION_NAME = "Unknown location '%s'"
|
|
|
|
|
|
|
|
class LocationType(enum.Enum):
|
|
|
|
USER = 1
|
|
|
|
NAME = 2
|
2019-04-28 11:12:00 +00:00
|
|
|
|
|
|
|
class Module(ModuleManager.BaseModule):
|
|
|
|
_name = "Time"
|
|
|
|
|
2019-07-27 20:09:45 +00:00
|
|
|
def _find_setting(self, event):
|
2019-10-18 14:03:19 +00:00
|
|
|
query = None
|
2019-11-14 15:48:18 +00:00
|
|
|
target_user = None
|
2019-07-27 20:09:45 +00:00
|
|
|
|
2019-04-28 11:12:00 +00:00
|
|
|
if event["args"]:
|
2019-10-18 14:03:19 +00:00
|
|
|
query = event["args"]
|
2019-09-08 17:17:32 +00:00
|
|
|
if len(event["args_split"]) == 1 and event["server"].has_user_id(
|
|
|
|
event["args_split"][0]):
|
|
|
|
target_user = event["server"].get_user(event["args_split"][0])
|
2019-10-18 14:03:19 +00:00
|
|
|
else:
|
|
|
|
target_user = event["user"]
|
2019-09-08 17:17:32 +00:00
|
|
|
|
|
|
|
if target_user:
|
|
|
|
location = target_user.get_setting("location", None)
|
|
|
|
if location:
|
|
|
|
return (LocationType.USER, target_user.nickname,
|
|
|
|
location["timezone"])
|
2019-10-18 14:03:19 +00:00
|
|
|
|
|
|
|
if query:
|
|
|
|
location = self.exports.get_one("get-location")(query)
|
|
|
|
if location:
|
|
|
|
return (LocationType.NAME, location["name"],
|
|
|
|
location["timezone"])
|
2019-09-08 17:17:32 +00:00
|
|
|
else:
|
2019-10-18 14:03:19 +00:00
|
|
|
return LocationType.NAME, event["args"], None
|
2019-04-28 11:12:00 +00:00
|
|
|
|
2019-12-30 17:10:16 +00:00
|
|
|
def _timezoned(self, dt, timezone):
|
2019-12-30 17:18:54 +00:00
|
|
|
dt = dt.astimezone(pytz.timezone(timezone))
|
2019-12-30 17:10:16 +00:00
|
|
|
utc_offset = (dt.utcoffset().total_seconds()/60)/60
|
|
|
|
tz = "UTC"
|
|
|
|
if not utc_offset == 0.0:
|
|
|
|
if utc_offset > 0:
|
|
|
|
tz += "+"
|
|
|
|
tz += "%g" % utc_offset
|
2020-01-30 21:12:31 +00:00
|
|
|
return "%s %s" % (utils.datetime.format.datetime_human(dt), tz)
|
2019-04-28 11:12:00 +00:00
|
|
|
|
2019-07-27 20:09:45 +00:00
|
|
|
@utils.hook("received.command.time")
|
|
|
|
@utils.kwarg("help", "Get the time for you or someone else")
|
|
|
|
@utils.kwarg("usage", "[nickname]")
|
|
|
|
@utils.kwarg("require_setting", "location")
|
|
|
|
@utils.kwarg("require_setting_unless", "1")
|
|
|
|
def time(self, event):
|
2019-09-08 17:17:32 +00:00
|
|
|
type, name, timezone = self._find_setting(event)
|
|
|
|
|
|
|
|
if not timezone == None:
|
2019-12-30 17:19:08 +00:00
|
|
|
human = self._timezoned(datetime.datetime.now(), timezone)
|
2019-09-08 17:17:32 +00:00
|
|
|
|
|
|
|
out = None
|
|
|
|
if type == LocationType.USER:
|
2019-12-30 17:19:08 +00:00
|
|
|
out = "Time for %s: %s" % (name, human)
|
2019-09-08 17:17:32 +00:00
|
|
|
else:
|
2019-12-30 17:19:08 +00:00
|
|
|
out = "It is %s in %s" % (human, name)
|
2019-09-08 17:17:32 +00:00
|
|
|
event["stdout"].write(out)
|
2019-04-28 11:12:00 +00:00
|
|
|
else:
|
2019-09-08 17:17:32 +00:00
|
|
|
out = None
|
|
|
|
if type == LocationType.USER:
|
|
|
|
out = NOLOCATION_USER
|
|
|
|
else:
|
|
|
|
out = NOLOCATION_NAME
|
|
|
|
|
|
|
|
event["stderr"].write(out % name)
|
2019-12-30 17:10:16 +00:00
|
|
|
|
2020-02-19 17:29:10 +00:00
|
|
|
@utils.export("time-localise")
|
2019-12-30 17:10:16 +00:00
|
|
|
def time_localise(self, user, dt):
|
|
|
|
location = user.get_setting("location", None)
|
|
|
|
timezone = "UTC"
|
|
|
|
if not location == None:
|
|
|
|
timezone = location["timezone"]
|
|
|
|
return self._timezoned(dt, timezone)
|