blob: 09f6b6868ace2f4d565476ee899921584e006bf9 [file] [log] [blame]
Wei-Yu Chen49950b92021-11-08 19:19:18 +08001"""
2Copyright 2020 The Magma Authors.
3
4This source code is licensed under the BSD-style license found in the
5LICENSE file in the root directory of this source tree.
6
7Unless required by applicable law or agreed to in writing, software
8distributed under the License is distributed on an "AS IS" BASIS,
9WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10See the License for the specific language governing permissions and
11limitations under the License.
12"""
13
14from datetime import datetime, timedelta
15
16
17class StateMachineTimer():
18 def __init__(self, seconds_remaining: int) -> None:
19 self.start_time = datetime.now()
20 self.seconds = seconds_remaining
21
22 def is_done(self) -> bool:
23 time_elapsed = datetime.now() - self.start_time
24 if time_elapsed > timedelta(seconds=self.seconds):
25 return True
26 return False
27
28 def seconds_elapsed(self) -> int:
29 time_elapsed = datetime.now() - self.start_time
30 return int(time_elapsed.total_seconds())
31
32 def seconds_remaining(self) -> int:
33 return max(0, self.seconds - self.seconds_elapsed())