remove dateutils dependency, change tools pathing

This commit is contained in:
iou1name 2020-01-31 13:05:40 -05:00
parent 987854e900
commit 7111348d94
7 changed files with 55 additions and 75 deletions

View File

@ -3,4 +3,4 @@ It's like Sopel, except rewritten from scratch using Twisted as a base and over
## Requirements ## Requirements
Python 3.6+ Python 3.6+
Python packages: `twisted python-dateutil requests bs4 wolframalpha emoji pillow ipython numpy numpngw numexpr` Python packages: `twisted requests bs4 wolframalpha emoji pillow ipython numpy numpngw numexpr`

View File

@ -4,8 +4,9 @@ Various things related to Banished Quest.
""" """
from datetime import datetime from datetime import datetime
import tools
import config
from module import commands from module import commands
from tools.time import relativeTime
@commands('bq') @commands('bq')
def BQstatus(bot, trigger): def BQstatus(bot, trigger):
@ -13,8 +14,8 @@ def BQstatus(bot, trigger):
Displays the current status of BQ. Displays the current status of BQ.
""" """
status = "\x0304DEAD" status = "\x0304DEAD"
deathdate = "[2017-02-16 00:19:00]" deathdate = datetime(2017, 2, 16, 0, 19, 0)
msg = "Banished Quest status: " + status + "\nTime since death: " msg = "Banished Quest status: " + status + "\nTime since death: "
msg += relativeTime(datetime.now(), deathdate) + " ago " msg += tools.relative_time(datetime.now(), deathdate) + " ago "
msg += deathdate msg += deathdate.strftime(config.default_time_format)
bot.msg(msg) bot.msg(msg)

View File

@ -12,7 +12,6 @@ from requests.structures import CaseInsensitiveDict
import tools import tools
import config import config
from tools.time import relativeTime
from module import commands, example, hook, require_chanmsg, rate from module import commands, example, hook, require_chanmsg, rate
@ -91,9 +90,9 @@ def seen(bot, trigger):
return bot.msg(f"I haven't seen \x0308{args.nick}") return bot.msg(f"I haven't seen \x0308{args.nick}")
timestamp = datetime.fromtimestamp(timestamp) timestamp = datetime.fromtimestamp(timestamp)
reltime = tools.relative_time(datetime.now(), timestamp)
t_format = config.default_time_format t_format = config.default_time_format
timestamp = datetime.strftime(timestamp, t_format) timestamp = datetime.strftime(timestamp, t_format)
reltime = relativeTime(datetime.now(), timestamp)
if args.first: if args.first:
msg = "First" msg = "First"

View File

@ -9,8 +9,8 @@ import threading
from datetime import datetime from datetime import datetime
from sqlite3 import OperationalError from sqlite3 import OperationalError
import tools
import config import config
from tools.time import relativeTime
from module import commands, example, hook from module import commands, example, hook
def load_database(bot): def load_database(bot):
@ -110,7 +110,7 @@ def tell_hook(bot, trigger):
teller, unixtime, message = tell teller, unixtime, message = tell
telldate = datetime.fromtimestamp(unixtime) telldate = datetime.fromtimestamp(unixtime)
reltime = relativeTime(datetime.now(), telldate) reltime = tools.relative_time(datetime.now(), telldate)
t_format = config.default_time_format t_format = config.default_time_format
telldate = datetime.strftime(telldate, t_format) telldate = datetime.strftime(telldate, t_format)

View File

@ -7,7 +7,6 @@ import datetime
from twisted.internet import defer from twisted.internet import defer
from module import commands from module import commands
import tools.time
def setup(bot): def setup(bot):

View File

@ -6,6 +6,7 @@ import re
import argparse import argparse
import threading import threading
from collections import defaultdict from collections import defaultdict
from datetime import datetime
op_level = { op_level = {
"voice": 1, "voice": 1,
@ -213,3 +214,48 @@ class FulviaArgparse(argparse.ArgumentParser):
def error(self, message): def error(self, message):
raise argparse.ArgumentError(None, message) raise argparse.ArgumentError(None, message)
def relative_time(time_1, time_2):
"""
Returns a relative timestamp between `time_1` and `time_2`. Inputs
must be datetime objects.
"""
assert type(time_1) == datetime, "time_1 must be datetime object"
assert type(time_2) == datetime, "time_2 must be datetime object"
diff = time_1 - time_2
msg = []
if diff.days >= 365:
if diff.days // 365 > 1:
msg.append(f"{diff.days // 365} years")
else:
msg.append(f"{diff.days // 365} year")
if diff.days:
if diff.days > 1:
msg.append(f"{diff.days - (diff.days // 365)*365} days")
else:
msg.append(f"{diff.days} - (diff.days // 365)*365 day")
if not msg:
if diff.hours:
if diff.hours > 1:
msg.append(f"{diff.hours} hours")
else:
msg.append(f"{diff.hours} hour")
if diff.minutes:
if diff.minutes > 1:
msg.append(f"{diff.minutes} minutes")
else:
msg.append(f"{diff.minutes} minute")
if not diff.hours:
if diff.seconds > 1:
msg.append(f"{diff.seconds} seconds")
else:
msg.append(f"{diff.seconds} second")
msg = ", ".join(msg)
return msg

View File

@ -1,65 +0,0 @@
#!/usr/bin/env python3
"""
Tools for working with time.
"""
from datetime import datetime
from dateutil.relativedelta import relativedelta
import config
def relativeTime(time_1, time_2):
"""
Returns the relative time difference between 'time_1' and 'time_2'.
If either 'time_1' or 'time_2' is a string, it will be converted to a
datetime object according to the 'default_time_format' variable in the
config.
"""
t_format = config.default_time_format
if type(time_1) == str:
time_1 = datetime.strptime(time_1, t_format)
if type(time_2) == str:
time_2 = datetime.strptime(time_2, t_format)
msg = []
diff = relativedelta(time_1, time_2)
if diff.years:
if diff.years > 1:
msg.append(f"{diff.years} years")
else:
msg.append(f"{diff.years} year")
if diff.months:
if diff.months > 1:
msg.append(f"{diff.months} months")
else:
msg.append(f"{diff.months} month")
if diff.days:
if diff.days > 1:
msg.append(f"{diff.days} days")
else:
msg.append(f"{diff.days} day")
if not msg:
if diff.hours:
if diff.hours > 1:
msg.append(f"{diff.hours} hours")
else:
msg.append(f"{diff.hours} hour")
if diff.minutes:
if diff.minutes > 1:
msg.append(f"{diff.minutes} minutes")
else:
msg.append(f"{diff.minutes} minute")
if not diff.hours:
if diff.seconds > 1:
msg.append(f"{diff.seconds} seconds")
else:
msg.append(f"{diff.seconds} second")
msg = ", ".join(msg)
return msg