Skip to main content

luna.clock

Clock Objects

class Clock(ABC)

Abstract base class representing a clock used to obtain datetime information. It defines a property datetime that must be implemented by subclasses to return the current datetime.

datetime

@property
@abstractmethod
def datetime() -> datetime

Abstract property to be implemented by subclasses to return the current datetime.

NowClock Objects

class NowClock(Clock)

Implementation of Clock that returns the current system time in UTC. It provides the real-time datetime from the system's perspective.

datetime

@property
def datetime() -> datetime

Returns the current system time in UTC.

VirtualClock Objects

class VirtualClock(Clock)

Implementation of Clock that allows for arbitrary manipulation of time. This clock is particularly useful for testing purposes where control over the passage of time is required.

  • dt: The initial datetime to set for the clock.

set_datetime

def set_datetime(dt: datetime)

Sets the current datetime of the clock.

  • dt: The datetime to set the clock to.

datetime

@property
def datetime() -> datetime

Returns the current datetime set in the VirtualClock.

incr

def incr(td: timedelta | None = None)

Increments the current datetime of the VirtualClock by a specified timedelta.

  • td: The timedelta to add to the current datetime. Defaults to one second if not specified.

OffsetClock Objects

class OffsetClock(Clock)

Implementation of Clock that adds a specified offset to the time returned by another Clock instance. This clock is used to simulate time offsets relative to a given base clock.

  • clock: The base Clock instance to add the offset to.
  • offset: The timedelta offset to add to the base clock's time.

datetime

@property
def datetime() -> datetime

Returns the datetime from the base clock with the specified offset added.