add time-localise export in user_time, to timezone localise a datetime

This commit is contained in:
jesopo 2019-12-30 17:10:16 +00:00
parent 9dc6069e06
commit e1f8e8a524

View file

@ -15,6 +15,9 @@ class LocationType(enum.Enum):
class Module(ModuleManager.BaseModule): class Module(ModuleManager.BaseModule):
_name = "Time" _name = "Time"
def on_load(self):
self.exports.add("time-localise", self.time_localise)
def _find_setting(self, event): def _find_setting(self, event):
query = None query = None
target_user = None target_user = None
@ -41,6 +44,15 @@ class Module(ModuleManager.BaseModule):
else: else:
return LocationType.NAME, event["args"], None return LocationType.NAME, event["args"], None
def _timezoned(self, dt, timezone):
dt = dt.replace(tzinfo=pytz.timezone(timezone))
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
return "%s %s" % (utils.datetime.datetime_human(dt), tz)
@utils.hook("received.command.time") @utils.hook("received.command.time")
@utils.kwarg("help", "Get the time for you or someone else") @utils.kwarg("help", "Get the time for you or someone else")
@ -51,14 +63,7 @@ class Module(ModuleManager.BaseModule):
type, name, timezone = self._find_setting(event) type, name, timezone = self._find_setting(event)
if not timezone == None: if not timezone == None:
dt = datetime.datetime.now(tz=pytz.timezone(timezone)) human = self._timezoned_now(datetime.datetime.now(), timezone)
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
human = utils.datetime.datetime_human(dt)
out = None out = None
if type == LocationType.USER: if type == LocationType.USER:
@ -74,3 +79,10 @@ class Module(ModuleManager.BaseModule):
out = NOLOCATION_NAME out = NOLOCATION_NAME
event["stderr"].write(out % name) event["stderr"].write(out % name)
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)