summaryrefslogtreecommitdiff
path: root/progress.py
blob: db5648cf12c6b0628e8432b5da479f6078554c60 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#! /usr/bin/env python3

from datetime import datetime, timedelta
from calendar import *


class Progress():
    """Displays the time left in the day/workweek/month/year as a
    percentage. Inspired by https://twitter.com/year_progress

    Days are workdays (8am-5pm).

    """

    def __init__(self):
        self.now = datetime.now()
        weekdays_left = timedelta(7 - self.now.isoweekday())
        cal = Calendar()
        month_max = max(list(cal.itermonthdays(self.now.year, self.now.month)))
        year_max = 366 if isleap(self.now.year) else 365
        self.dura = {
            "day": timedelta(hours=9), # a workday is 9 hours long
            "week": timedelta(days=7),
            "month": timedelta(days=month_max),
            "year": timedelta(days=year_max),
        }
        self.end = {
            "year": datetime(self.now.year + 1, 1, 1),
            # day ends at hour 17 (5pm), and since it's 9 hours long, it starts
            # at 8am
            "day": datetime(self.now.year, self.now.month, self.now.day, hour=17),
            "week": datetime(self.now.year, self.now.month, self.now.day)
            + weekdays_left,
            "month": datetime(self.now.year, self.now.month, month_max),
        }

    def calc(self, key):
        """This calculates the time left in the duration specified by `key`, which
        should be a string of either day, week, month, or year.

        """
        return 1 - ((self.end[key] - self.now) / self.dura[key])

    def __str__(self):
        """String representation of the progress calculation. I just print this to my
        emacs minibuffer.

        """
        return "d: {d:.0%} | w: {w:.0%} | m: {m:.0%} | y: {y:.0%}".format(
            d=self.calc("day"),
            w=self.calc("week"),
            m=self.calc("month"),
            y=self.calc("year"),
        )


if __name__ == "__main__":
    print(str(Progress()))