summaryrefslogtreecommitdiff
path: root/jimbrella/utils.py
blob: ce57bf982d1540c2f0083f271f8e178cfbb64347 (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
36
37
38
39
40
41
42
from datetime import datetime, timedelta, tzinfo

# identity function
identity = lambda x: x


def human_datetime(time: datetime) -> str:
    return "{:%Y-%m-%d %H:%M:%S}".format(time)


def human_timedelta(delta: timedelta) -> str:
    if delta.days == 0:
        days = ""
    elif delta.days == 1:
        days = "1 day, "
    else:
        days = f"{delta.days} days, "

    hours = delta.seconds // 3600
    minutes = (delta.seconds - (hours * 3600)) // 60
    return days + f"{hours:0>2}:{minutes:0>2}"  # zero-pad to two digits


class UTCPlus8(tzinfo):
    def __init__(self):
        super().__init__()

    def utcoffset(self, dt):
        return timedelta(hours=8)

    def dst(self, dt):
        return timedelta(0)

    def tzname(self, dt):
        return "+8:00"


utc_plus_8 = UTCPlus8()


def local_now():
    return datetime.now(tz=utc_plus_8)