aboutsummaryrefslogtreecommitdiffhomepage
path: root/apioforum/fuzzy.py
blob: 8396b8f3fa06ef740e7aaf2262ffaa9b74350581 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# fuzzy datetime things

units = (
    ("y", "year","years",365*24*60*60), # leap years aren't real
    ("d", "day","days",24*60*60),
    ("h", "hour","hours",60*60),
    ("m", "minute","minutes",60),
    ("s", "second","seconds",1),
)

from datetime import datetime, timedelta, timezone

def fuzzy(seconds, ago=False):
    if isinstance(seconds, timedelta):
        seconds = seconds.total_seconds()
    elif isinstance(seconds, datetime):
        seconds = (seconds.replace(tzinfo=timezone.utc) - datetime.now(tz=timezone.utc)).total_seconds()

    components_used = 0
    fmt = "{}"
    buf = ""
    if ago:
        fmt = "in {}" if seconds > 0 else "{} ago"
    elif seconds > 0: fmt = "in {}"
    seconds = abs(seconds)
    for short, _, _, unit_length in units:
        if seconds >= unit_length:
            components_used += 1
            qty = seconds // unit_length
            buf += str(int(qty)) + short
            seconds -= qty * unit_length
        if components_used == 2: break
    if not buf: return "now"

    return fmt.format(buf)