-
Notifications
You must be signed in to change notification settings - Fork 1
/
timeframe.py
78 lines (54 loc) · 2.33 KB
/
timeframe.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Copyright: Ren Tatsumoto <tatsu at autistici.org>
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import time
from anki.cards import CardId
from aqt import mw
from .config import config
class TimeFrame:
def __init__(self, hours: float = 0, seconds: float = 0, milliseconds: float = 0):
self._milliseconds = milliseconds + self.seconds_to_milliseconds(seconds) + self.hours_to_milliseconds(hours)
@classmethod
def seconds_to_milliseconds(cls, seconds: float) -> float:
return seconds * 1000
@classmethod
def hours_to_milliseconds(cls, hours: float) -> float:
return cls.seconds_to_milliseconds(hours * 60 * 60)
@classmethod
def milliseconds_to_hours(cls, milliseconds: float) -> float:
return milliseconds / 1000.0 / 3600.0
def __sub__(self, other: 'TimeFrame') -> 'TimeFrame':
return TimeFrame(milliseconds=self._milliseconds - other._milliseconds)
def __add__(self, other: 'TimeFrame') -> 'TimeFrame':
return TimeFrame(milliseconds=self._milliseconds + other._milliseconds)
def milliseconds(self) -> int:
return int(self._milliseconds)
def hours(self) -> int:
return int(self.milliseconds_to_hours(self._milliseconds))
def current_time() -> TimeFrame:
return TimeFrame(seconds=time.time())
def next_day_start() -> TimeFrame:
"""
Returns point in time when the next anki day starts.
For me it's 4AM on the next day.
"""
return TimeFrame(seconds=mw.col.sched.dayCutoff)
def this_day_start() -> TimeFrame:
return next_day_start() - TimeFrame(hours=24)
def threshold_time() -> TimeFrame:
if config['count_from_daystart'] is True:
return this_day_start()
else:
return current_time() - TimeFrame(hours=config['timeframe'])
def time_passed() -> TimeFrame:
if config['count_from_daystart'] is True:
return current_time() - this_day_start()
else:
return TimeFrame(hours=config['timeframe'])
def agains_in_the_timeframe(card_id: CardId) -> int:
# id: epoch-milliseconds timestamp of when you did the review
# ease: which button you pushed to score your recall. ('again' == 1)
return mw.col.db.scalar(
"select count() from revlog where ease = 1 and cid = ? and id >= ?",
card_id,
threshold_time().milliseconds()
)