|
| 1 | +import time |
| 2 | +from datetime import datetime, timezone |
| 3 | +from enum import Enum |
| 4 | + |
| 5 | + |
| 6 | +class TemporalMode(Enum): |
| 7 | + REAL_TIME = 0 |
| 8 | + ARCHIVE = 1 |
| 9 | + |
| 10 | + |
| 11 | +class State(Enum): |
| 12 | + UNINITIALIZED = 0 |
| 13 | + INTIALIZED = 1 |
| 14 | + STOPPED = 2 |
| 15 | + BUFFERING = 3 |
| 16 | + PLAYING = 4 |
| 17 | + FAST_FORWARDING = 5 |
| 18 | + REWINDING = 6 |
| 19 | + |
| 20 | + |
| 21 | +class TimeUtils: |
| 22 | + @staticmethod |
| 23 | + def to_epoch_time(a_time: datetime | str) -> float: |
| 24 | + """ |
| 25 | + Convert a datetime or string to epoch time |
| 26 | + :param a_time: |
| 27 | + :return: |
| 28 | + """ |
| 29 | + if isinstance(a_time, str): |
| 30 | + return time.mktime(datetime.strptime(a_time, "%Y-%m-%d %H:%M:%S").timetuple()) |
| 31 | + elif isinstance(a_time, datetime): |
| 32 | + return time.mktime(a_time.timetuple()) |
| 33 | + |
| 34 | + @staticmethod |
| 35 | + def to_utc_time(a_time: float | str) -> datetime: |
| 36 | + """ |
| 37 | + Convert epoch time or string to UTC time object |
| 38 | + :param a_time: |
| 39 | + :return: |
| 40 | + """ |
| 41 | + if isinstance(a_time, str): |
| 42 | + return datetime.strptime(a_time, "%Y-%m-%d %H:%M:%S").replace(tzinfo=timezone.utc) |
| 43 | + elif isinstance(a_time, float): |
| 44 | + return datetime.fromtimestamp(a_time, tz=timezone.utc) |
| 45 | + |
| 46 | + @staticmethod |
| 47 | + def current_epoch_time(): |
| 48 | + """ |
| 49 | + Get the current time in epoch format |
| 50 | + :return: |
| 51 | + """ |
| 52 | + return time.time() |
| 53 | + |
| 54 | + @staticmethod |
| 55 | + def current_utc_time() -> datetime: |
| 56 | + """ |
| 57 | + Get the current time in UTC timezone |
| 58 | + :return: |
| 59 | + """ |
| 60 | + return datetime.now(timezone.utc) |
| 61 | + |
| 62 | + @staticmethod |
| 63 | + def time_to_iso(a_time: datetime | float) -> str: |
| 64 | + """ |
| 65 | + Convert a datetime object to iso format |
| 66 | + :param a_time: datetime object in UTC timezone or epoch time (float) |
| 67 | + :return: |
| 68 | + """ |
| 69 | + if isinstance(a_time, float): |
| 70 | + return datetime.fromtimestamp(a_time, tz=timezone.utc).isoformat() + "Z" |
| 71 | + elif isinstance(a_time, datetime): |
| 72 | + return a_time.isoformat() + "Z" |
| 73 | + |
| 74 | + |
| 75 | +class Time: |
| 76 | + _epoch_time: float |
| 77 | + |
| 78 | + def __init__(self, epoch_time: float = None, utc_time: datetime = None): |
| 79 | + if epoch_time is not None: |
| 80 | + self._epoch_time = epoch_time |
| 81 | + elif utc_time is not None: |
| 82 | + self._epoch_time = TimeUtils.to_epoch_time(utc_time) |
| 83 | + |
| 84 | + @property |
| 85 | + def epoch_time(self): |
| 86 | + return self._epoch_time |
| 87 | + |
| 88 | + @epoch_time.setter |
| 89 | + def epoch_time(self, epoch_time: float): |
| 90 | + if hasattr(self, "_epoch_time"): |
| 91 | + raise AttributeError("Epoch time should not be changed once set") |
| 92 | + |
| 93 | + def get_epoch_time(self): |
| 94 | + return self._epoch_time |
| 95 | + |
| 96 | + def get_utc_time(self): |
| 97 | + return TimeUtils.to_utc_time(self._epoch_time) |
| 98 | + |
| 99 | + |
| 100 | +class IndeterminateTime(Enum): |
| 101 | + NOW = "now" |
| 102 | + # LATEST = "latest" |
| 103 | + # FIRST = "first" |
| 104 | + |
| 105 | + |
| 106 | +class TimePeriod: |
| 107 | + _start_time: Time | IndeterminateTime |
| 108 | + _end_time: Time | IndeterminateTime |
| 109 | + |
| 110 | + def __init__(self, start_time: Time | IndeterminateTime, end_time: Time | IndeterminateTime): |
| 111 | + if isinstance(start_time, Time) and isinstance(end_time, Time): |
| 112 | + if start_time.get_epoch_time() > end_time.get_epoch_time(): |
| 113 | + raise ValueError("Start time cannot be later than end time") |
| 114 | + |
| 115 | + if isinstance(start_time, IndeterminateTime) and isinstance(end_time, IndeterminateTime): |
| 116 | + raise ValueError("Start time and end time cannot be indeterminate at the same time") |
| 117 | + |
| 118 | + self._start_time = start_time |
| 119 | + self._end_time = end_time |
| 120 | + |
| 121 | + def get_start_time(self): |
| 122 | + return self._start_time |
| 123 | + |
| 124 | + def get_end_time(self): |
| 125 | + return self._end_time |
| 126 | + |
| 127 | + def set_start_time(self, start_time: Time): |
| 128 | + self._start_time = start_time |
| 129 | + |
| 130 | + def set_end_time(self, end_time: Time): |
| 131 | + self._end_time = end_time |
| 132 | + |
| 133 | + def is_indeterminate_start_time(self): |
| 134 | + return isinstance(self._start_time, IndeterminateTime) |
| 135 | + |
| 136 | + def is_indeterminate_end_time(self): |
| 137 | + return isinstance(self._end_time, IndeterminateTime) |
0 commit comments