summaryrefslogtreecommitdiffhomepage
path: root/apioforum/fuzzy.py
diff options
context:
space:
mode:
Diffstat (limited to 'apioforum/fuzzy.py')
-rw-r--r--apioforum/fuzzy.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/apioforum/fuzzy.py b/apioforum/fuzzy.py
new file mode 100644
index 0000000..58f8c6b
--- /dev/null
+++ b/apioforum/fuzzy.py
@@ -0,0 +1,35 @@
+# fuzzy datetime things
+
+times = (
+ ("year","years",365*24*60*60), # leap years aren't real
+ ("day","days",24*60*60),
+ ("hour","hours",60*60),
+ ("minute","minutes",60),
+ ("second","seconds",1),
+)
+
+from datetime import datetime, timedelta
+
+def fuzzy(seconds,ago=True):
+ if isinstance(seconds,timedelta):
+ seconds = seconds.total_seconds()
+ elif isinstance(seconds,datetime):
+ seconds = (seconds-datetime.now()).total_seconds()
+
+ fmt = "{}"
+ if ago:
+ fmt = "in {}" if seconds > 0 else "{} ago"
+ seconds = abs(seconds)
+ for t in times:
+ if seconds >= t[2]:
+ rounded = round((seconds / t[2])*100)/100
+ if int(rounded) == rounded:
+ rounded = int(rounded)
+ if rounded == 1:
+ word = t[0]
+ else:
+ word = t[1]
+ return fmt.format(f'{rounded} {word}')
+ else:
+ return "now"
+