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
|
#! /usr/bin/env python3
from datetime import datetime, timedelta
from calendar import *
class Progress(object):
"""Displays the time left in the day/workweek/month/year as a
percentage. Inspired by https://twitter.com/year_progress
"""
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=8),
"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": datetime(self.now.year, self.now.month, self.now.day, hour=16),
"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):
return 1 - ((self.end[key] - self.now) / self.dura[key])
def __str__(self):
return "d: {d:.1%} | w: {w:.1%} | m: {m:.1%} | y: {y:.1%}".format(
d=self.calc("day"),
w=self.calc("week"),
m=self.calc("month"),
y=self.calc("year"),
)
if __name__ == "__main__":
print(str(Progress()))
|