Skip to content

Commit

Permalink
Merge pull request #737 from izzy84075/chanlogs-localtime2
Browse files Browse the repository at this point in the history
Add the option to use the bot's preferred timezone for logs.
  • Loading branch information
embolalia committed Feb 15, 2015
2 parents bff2860 + bc76fcd commit d68cd93
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions willie/modules/chanlogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,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
Expand All @@ -39,10 +45,25 @@ 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):
"""
Returns a string corresponding to the path to the file where the message
Expand All @@ -53,9 +74,7 @@ def get_fpath(bot, trigger, channel=None):
channel = channel.lstrip("#")
channel = BAD_CHARS.sub('__')

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:
Expand All @@ -64,9 +83,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(),
Expand Down

0 comments on commit d68cd93

Please sign in to comment.