Skip to content

Commit 794ed29

Browse files
♻️ refactor(calendar.all): Drop direct dependency on arrow.
1 parent 6d69fd7 commit 794ed29

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

.bin/calendar._all

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
#!/usr/bin/env python3
22

3-
import arrow
3+
from zoneinfo import ZoneInfo
4+
from datetime import datetime, timedelta
45
import re
6+
import subprocess
57

6-
REGEX_URL = 'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\), ]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
8+
TZ = subprocess.check_output(['timezone.current']).decode().strip()
9+
FMT = '%Y-%m-%d %H:%M:%S'
10+
REGEX_URL = 'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*(), ]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
711
EXCLUDED_URLS = [
812
'https://g.co/calendar',
913
]
1014

1115
from events import _range
12-
from events import log
1316
from events import _cache_load_url
1417
from events import calendars
1518
from events import FRESH
16-
from events import _humanize
1719
from events import _duration
18-
from events import _shorthumanize
19-
from events import _shortduration
20-
from events import _tinyhumanize
21-
from events import _tinyduration
2220

2321
def _icon(event):
2422

@@ -46,14 +44,22 @@ for _calendar in calendars():
4644
for event in calendar_events:
4745
icons[event] = _calendar['icon']
4846

49-
now = arrow.now()
50-
today = now.floor('day')
51-
tomorrow = today.shift(days=+1)
52-
dayaftertomorrow = tomorrow.shift(days=+1)
53-
nextweek = now.floor('week').shift(weeks=+1)
54-
weekafternextweek = nextweek.shift(weeks=+1)
55-
nextmonth = now.floor('month').shift(months=+1)
56-
monthafternextmonth = nextmonth.shift(months=+1)
47+
def _add_months(dt: datetime, months: int) -> datetime:
48+
total_month = dt.month - 1 + months
49+
year = dt.year + total_month // 12
50+
month = total_month % 12 + 1
51+
return dt.replace(year=year, month=month)
52+
53+
now = datetime.now(ZoneInfo(TZ))
54+
today = now.replace(hour=0, minute=0, second=0, microsecond=0)
55+
tomorrow = today + timedelta(days=1)
56+
dayaftertomorrow = tomorrow + timedelta(days=1)
57+
startofthisweek = today - timedelta(days=today.weekday())
58+
nextweek = startofthisweek + timedelta(weeks=1)
59+
weekafternextweek = nextweek + timedelta(weeks=1)
60+
startofthismonth = today.replace(day=1)
61+
nextmonth = _add_months(startofthismonth, 1)
62+
monthafternextmonth = _add_months(nextmonth, 1)
5763

5864
_fresh = set(filter(lambda x: x.end >= now, allevents))
5965

@@ -79,10 +85,8 @@ for name, begin, end in timesections:
7985

8086
events = set(filter(predicate, _fresh)) - used
8187

82-
A = 'the beginning of the Universe' if begin is None else begin.format(
83-
'YYYY-MM-DD HH:mm:ss')
84-
B = 'the end of the Universe' if end is None else end.format(
85-
'YYYY-MM-DD HH:mm:ss')
88+
A = 'the beginning of the Universe' if begin is None else begin.strftime(FMT)
89+
B = 'the end of the Universe' if end is None else end.strftime(FMT)
8690

8791
title = (name + ', from {} to {}').format(A, B)
8892

@@ -115,7 +119,7 @@ ENDC = '\033[0m'
115119

116120

117121
print()
118-
print( ' > Generated on {}'.format(now.format('ddd, DD MMM YYYY HH:mm:ss Z')) )
122+
print( ' > Generated on {}'.format(now.strftime('%a, %d %b %Y %H:%M:%S %Z')) )
119123

120124
for title, events in sections:
121125

0 commit comments

Comments
 (0)