Wei-Yu Chen | ad55cb8 | 2022-02-15 20:07:01 +0800 | [diff] [blame^] | 1 | # 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 Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame] | 5 | |
| 6 | from datetime import datetime, timedelta |
| 7 | |
| 8 | |
| 9 | class 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()) |