[90] | 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 - 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 |
|
---|
| 50 | def getDuration(self) -> int :
|
---|
| 51 | '''
|
---|
| 52 | @return duration in milliseconds.
|
---|
| 53 | '''
|
---|
| 54 | return self._duration;
|
---|
| 55 |
|
---|
| 56 | def getTerminationTime(self) -> datetime:
|
---|
| 57 | return datetime.fromtimestamp( (int(datetime.timestamp(self._start)*1000) + self._duration)/1000.);
|
---|
| 58 |
|
---|
| 59 |
|
---|
| 60 | def __repr__(self):
|
---|
| 61 | return "ProgressTime[" + str(datetime.timestamp(self._start))+ " , " + str(self._duration) + "ms]"
|
---|
| 62 |
|
---|
| 63 | def __hash__(self):
|
---|
| 64 | return hash((self._duration, self._start))
|
---|
| 65 |
|
---|
| 66 | def __eq__(self, other):
|
---|
| 67 | return isinstance(other, self.__class__) \
|
---|
| 68 | and self._duration == other._duration \
|
---|
| 69 | and self._start == other._start
|
---|
| 70 |
|
---|