blob: 03f0e053a1620a04d8e58a1bd97eccbe30370b46 [file] [log] [blame]
Wei-Yu Chenad55cb82022-02-15 20:07:01 +08001# SPDX-FileCopyrightText: 2020 The Magma Authors.
2# SPDX-FileCopyrightText: 2022 Open Networking Foundation <support@opennetworking.org>
3#
4# SPDX-License-Identifier: BSD-3-Clause
Wei-Yu Chen49950b92021-11-08 19:19:18 +08005
6from datetime import datetime, timedelta
7
8
9class StateMachineTimer():
10 def __init__(self, seconds_remaining: int) -> None:
11 self.start_time = datetime.now()
12 self.seconds = seconds_remaining
13
14 def is_done(self) -> bool:
15 time_elapsed = datetime.now() - self.start_time
16 if time_elapsed > timedelta(seconds=self.seconds):
17 return True
18 return False
19
20 def seconds_elapsed(self) -> int:
21 time_elapsed = datetime.now() - self.start_time
22 return int(time_elapsed.total_seconds())
23
24 def seconds_remaining(self) -> int:
25 return max(0, self.seconds - self.seconds_elapsed())