From 3c0472720fa4ba6d23b17027ddd53741f43a4f53 Mon Sep 17 00:00:00 2001 From: izzy84075 Date: Tue, 10 Feb 2015 15:00:10 -0800 Subject: [PATCH 1/2] Add the option to use the bot's preferred timezone for logs. --- willie/modules/chanlogs.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/willie/modules/chanlogs.py b/willie/modules/chanlogs.py index 9fc3a8c2da..86d66b2440 100644 --- a/willie/modules/chanlogs.py +++ b/willie/modules/chanlogs.py @@ -13,6 +13,12 @@ import threading import sys from datetime import datetime +try: + from pytz import timezone + from pytz import all_timezones + import pytz +except ImportError: + pytz = None import willie.module import willie.tools from willie.config import ConfigurationError @@ -36,9 +42,22 @@ def configure(config): config.add_option("chanlogs", "by_day", "Split log files by day", default=True) config.add_option("chanlogs", "privmsg", "Record private messages", default=False) config.add_option("chanlogs", "microseconds", "Microsecond precision", default=False) + config.add_option("chanlogs", "localtime", "Attempt to use preferred timezone", default=False) # could ask if user wants to customize message templates, # but that seems unnecessary +def get_datetime(bot): + """ + Returns a datetime object of the current time. + """ + dt = datetime.utcnow() + if pytz: + dt = dt.replace(tzinfo = timezone('UTC')) + if bot.config.chanlogs.localtime: + dt = dt.astimezone(timezone(bot.config.clock.tz)) + if not bot.config.chanlogs.microseconds: + dt = dt.replace(microsecond=0) + return dt def get_fpath(bot, trigger, channel=None): """ @@ -49,9 +68,7 @@ def get_fpath(bot, trigger, channel=None): channel = channel or trigger.sender channel = channel.lstrip("#") - dt = datetime.utcnow() - if not bot.config.chanlogs.microseconds: - dt = dt.replace(microsecond=0) + dt = get_datetime(bot) if bot.config.chanlogs.by_day: fname = "{channel}-{date}.log".format(channel=channel, date=dt.date().isoformat()) else: @@ -60,9 +77,7 @@ def get_fpath(bot, trigger, channel=None): def _format_template(tpl, bot, trigger, **kwargs): - dt = datetime.utcnow() - if not bot.config.chanlogs.microseconds: - dt = dt.replace(microsecond=0) + dt = get_datetime(bot) formatted = tpl.format( trigger=trigger, datetime=dt.isoformat(), From bc76fcd60772a53bb0d68d1fe8b07082baaa6e73 Mon Sep 17 00:00:00 2001 From: izzy84075 Date: Tue, 10 Feb 2015 15:19:20 -0800 Subject: [PATCH 2/2] Fix style. --- willie/modules/chanlogs.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/willie/modules/chanlogs.py b/willie/modules/chanlogs.py index 86d66b2440..0331cd3816 100644 --- a/willie/modules/chanlogs.py +++ b/willie/modules/chanlogs.py @@ -46,19 +46,21 @@ def configure(config): # could ask if user wants to customize message templates, # but that seems unnecessary + def get_datetime(bot): """ Returns a datetime object of the current time. """ dt = datetime.utcnow() if pytz: - dt = dt.replace(tzinfo = timezone('UTC')) + dt = dt.replace(tzinfo=timezone('UTC')) if bot.config.chanlogs.localtime: dt = dt.astimezone(timezone(bot.config.clock.tz)) if not bot.config.chanlogs.microseconds: dt = dt.replace(microsecond=0) return dt + def get_fpath(bot, trigger, channel=None): """ Returns a string corresponding to the path to the file where the message