3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-12-24 11:42:51 +01:00

stats: prettier formatting for the uptime command

Closes #381.
This commit is contained in:
James Lu 2017-01-30 00:06:19 -08:00
parent 1c19d82f53
commit de99be720e
2 changed files with 37 additions and 5 deletions

View File

@ -569,3 +569,8 @@ automode:
# Setting this to op (+o) for Automode makes it appear more like a standard IRC service, and lessens
# the risk of mode overrides being dropped.
joinmodes: 'o'
stats:
# Determines the time format that the Stats plugin should use for showing dates + times.
# Defaults to "%a, %d %b %Y %H:%M:%S +0000" (the RFC 2812 standard) if not specified.
time_format: "%c"

View File

@ -4,12 +4,27 @@ stats.py: Simple statistics for PyLink IRC Services.
import time
import datetime
from pylinkirc import utils, world
from pylinkirc import utils, world, conf
from pylinkirc.log import log
from pylinkirc.coremods import permissions
def _timesince(before, now):
return str(datetime.timedelta(seconds=now-before))
def timediff(before, now):
"""
Returns the time difference between "before" and "now" as a formatted string.
"""
td = datetime.timedelta(seconds=now-before)
days = td.days
hours, leftover = divmod(td.seconds, 3600)
minutes, seconds = divmod(leftover, 60)
# XXX: I would make this more configurable but it's a lot of work for little gain,
# since there's no strftime for time differences.
return '%d day%s, %02d:%02d:%02d' % (td.days, 's' if td.days != 1 else '',
hours, minutes, seconds)
# From RFC 2822: https://tools.ietf.org/html/rfc2822.html#section-3.3
DEFAULT_TIME_FORMAT = "%a, %d %b %Y %H:%M:%S +0000"
@utils.add_cmd
def uptime(irc, source, args):
@ -31,6 +46,18 @@ def uptime(irc, source, args):
current_time = int(time.time())
irc.reply("PyLink uptime: \x02%s\x02, Connected to %s: \x02%s\x02" % \
(_timesince(world.start_ts, current_time), network, _timesince(irc.start_ts, current_time)))
time_format = conf.conf.get('stats', {}).get('time_format', DEFAULT_TIME_FORMAT)
irc.reply("PyLink uptime: \x02%s\x02 (started on %s)" %
(timediff(world.start_ts, current_time),
time.strftime(time_format, time.gmtime(world.start_ts))
)
)
irc.reply("Connected to %s: \x02%s\x02 (connected on %s)" %
(network,
timediff(irc.start_ts, current_time),
time.strftime(time_format, time.gmtime(irc.start_ts))
)
)