1 | from datetime import datetime
|
---|
2 |
|
---|
3 | from geniusweb.progress.Progress import Progress
|
---|
4 |
|
---|
5 |
|
---|
6 | class ProgressTime (Progress):
|
---|
7 | '''
|
---|
8 | keeps track of progress towards the Deadline and the time/rounds left.
|
---|
9 | immutable. However, the time progresses nevertheless since this refers to the
|
---|
10 | system clocks. immutable.
|
---|
11 | '''
|
---|
12 |
|
---|
13 | def __init__(self, duration: int, start:datetime):
|
---|
14 | '''
|
---|
15 | @param d the duration, eg {@link DeadlineTime#getDuration()}. Must be
|
---|
16 | > 0.
|
---|
17 | @param start the start Date (unix time in millisecs since 1970). See
|
---|
18 | {@link System#currentTimeMillis()}. WARNING
|
---|
19 | due to a bug in python for windows, this value must be
|
---|
20 | at least 100000 seconds above jan. 1, 1970.
|
---|
21 | '''
|
---|
22 | self._start = start;
|
---|
23 | self._duration = 1 if duration == None or duration <= 0 else duration
|
---|
24 |
|
---|
25 | def get(self, currentTimeMs:int) -> float:
|
---|
26 | delta = currentTimeMs - 1000 * datetime.timestamp(self._start);
|
---|
27 | # double should have ~53 digits of precision, and we're computing
|
---|
28 | # deltas here.
|
---|
29 | # 2^53 millis seems plenty for our purposes so no need to use
|
---|
30 | # BigDecimal here.
|
---|
31 | ratio:float = delta / self._duration;
|
---|
32 | if ratio > 1:
|
---|
33 | ratio = 1
|
---|
34 | elif ratio < 0:
|
---|
35 | ratio = 0
|
---|
36 | return ratio
|
---|
37 |
|
---|
38 | def isPastDeadline(self, currentTimeMs:int) -> bool:
|
---|
39 | return currentTimeMs > int(datetime.timestamp(self._start) * 1000) + self._duration;
|
---|
40 |
|
---|
41 | def getStart(self) -> datetime:
|
---|
42 | '''
|
---|
43 | @return time measured in milliseconds, between the start time and
|
---|
44 | midnight, January 1, 1970 UTCas returned from
|
---|
45 | {@link System#currentTimeMillis()}
|
---|
46 | '''
|
---|
47 | return self._start;
|
---|
48 |
|
---|
49 | def getDuration(self) -> int:
|
---|
50 | '''
|
---|
51 | @return duration in milliseconds.
|
---|
52 | '''
|
---|
53 | return self._duration;
|
---|
54 |
|
---|
55 | def getTerminationTime(self) -> datetime:
|
---|
56 | return datetime.fromtimestamp((int(datetime.timestamp(self._start) * 1000) + self._duration) / 1000.);
|
---|
57 |
|
---|
58 | def __repr__(self):
|
---|
59 | return "ProgressTime[" + str(int(1000 * datetime.timestamp(self._start))) + " , " + str(self._duration) + "ms]"
|
---|
60 |
|
---|
61 | def __hash__(self):
|
---|
62 | return hash((self._duration, self._start))
|
---|
63 |
|
---|
64 | def __eq__(self, other):
|
---|
65 | return isinstance(other, self.__class__) \
|
---|
66 | and self._duration == other._duration \
|
---|
67 | and self._start == other._start
|
---|
68 |
|
---|