Mercurial > code > home > repos > gcalendarwatch
annotate datetimemath.py @ 43:b5d3d9a8c83d
new graph of just the events happening now
author | drewp@bigasterisk.com |
---|---|
date | Mon, 19 Feb 2024 13:53:46 -0800 |
parents | e2209226b001 |
children |
rev | line source |
---|---|
28
e2209226b001
rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff
changeset
|
1 import datetime |
e2209226b001
rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff
changeset
|
2 from typing import Iterable, List, Tuple, cast |
e2209226b001
rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff
changeset
|
3 |
e2209226b001
rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff
changeset
|
4 import dateutil.parser |
e2209226b001
rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff
changeset
|
5 from dateutil.tz.tz import tzlocal |
e2209226b001
rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff
changeset
|
6 |
e2209226b001
rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff
changeset
|
7 from localtypes import Record |
e2209226b001
rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff
changeset
|
8 |
e2209226b001
rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff
changeset
|
9 |
e2209226b001
rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff
changeset
|
10 def dayRange(days: float) -> Tuple[datetime.datetime, datetime.datetime]: |
e2209226b001
rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff
changeset
|
11 now = datetime.datetime.now(tzlocal()) |
e2209226b001
rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff
changeset
|
12 start = now - datetime.timedelta(hours=12) |
e2209226b001
rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff
changeset
|
13 end = now + datetime.timedelta(days=days) |
e2209226b001
rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff
changeset
|
14 return start, end |
e2209226b001
rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff
changeset
|
15 |
e2209226b001
rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff
changeset
|
16 |
e2209226b001
rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff
changeset
|
17 def limitDays(recs: Iterable[Record], days: float) -> List[Record]: |
e2209226b001
rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff
changeset
|
18 start, end = dayRange(days) |
e2209226b001
rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff
changeset
|
19 start = start - datetime.timedelta(hours=12) |
e2209226b001
rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff
changeset
|
20 # incomplete |
e2209226b001
rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff
changeset
|
21 return [r for r in recs if r['startTime'] < end and r['endTime'] > start] |
e2209226b001
rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff
changeset
|
22 |
e2209226b001
rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff
changeset
|
23 |
e2209226b001
rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff
changeset
|
24 # just a typing fix |
e2209226b001
rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff
changeset
|
25 def parse(s: str, **kw) -> datetime.datetime: |
e2209226b001
rewrite with starlette and background_loop
drewp@bigasterisk.com
parents:
diff
changeset
|
26 return cast(datetime.datetime, dateutil.parser.parse(s, **kw)) |