# Copyright (c) Meta Platforms, Inc. and affiliates.# All rights reserved.## This source code is licensed under the terms described in the LICENSE file in# the root directory of this source tree.importtime
[docs]classTimeManager:""" A class for managing time in the simulation. """
[docs]def__init__(self):current_time=time.time()# This is the actual real time, not the simulated time, with which we measure actual time passed.self.real_start_time:float|None=current_time# This is the simulated start time, that we can set manually to a given time.self.start_time:float|None=current_time# The offset added to the time passed. This is used to take into account the pauses.self.offset:float=0# A flag indicating whether the time is paused or not.self.is_paused:bool=False# The real time at which the time was paused.self.pause_real_start_time:float|None=None# The time passed before the time was paused.self.pause_passed_time:float|None=None# The offset added to the time passed when the time was paused.self.pause_offset:float=0
[docs]deftime(self)->float:""" Returns the current time, which is the sum of the start time and the time passed. Returns: float: The current time. """ifself.start_timeisNone:raiseException("start_time cannot be None")returnself.start_time+self.time_passed()
[docs]deftime_passed(self)->float:""" Returns the time passed since the start time, taking into account any pauses. Returns: float: The time passed. """ifself.real_start_timeisNone:raiseException("real_start_time cannot be null")ifself.is_pausedandself.pause_passed_time:returnself.pause_passed_time+self.pause_offsetreturnself.real_time_passed()+self.offset
[docs]defreal_time_passed(self)->float:""" Returns the real time passed since the start time, without considering any pauses. Returns: float: The real time passed. """ifself.real_start_timeisNone:raiseException("real_start_time cannot be null")returntime.time()-self.real_start_time
[docs]defreset(self,start_time:float|None=None)->None:""" Resets the start time to the current time, or to the specified `start_time` if provided. Args: start_time (float | None): The new start time. Defaults to current time. """current_time=time.time()ifstart_timeisNone:self.start_time=current_timeelse:self.start_time=start_timeself.real_start_time=current_time# Reset other variablesself.offset=0self.is_paused=Falseself.pause_real_start_time=Noneself.pause_passed_time=None
[docs]defpause(self)->None:""" Pauses the time. """ifnotself.is_paused:self.pause_real_start_time=time.time()self.pause_passed_time=self.time_passed()self.is_paused=Trueself.pause_offset=0
[docs]defresume(self)->None:""" Resumes the time. """ifself.is_paused:# Compute total time timer was paused, to offset the timepause_duration=time.time()-self.pause_real_start_time# type: ignore# Susbtract the pause time from the offset to make sure we take it into accountself.offset-=pause_durationself.offset+=self.pause_offsetself.pause_offset=0self.is_paused=Falseself.pause_passed_time=Noneself.pause_real_start_time=None