Neuron-PHP

FrozenClock
in package
implements IClock

Frozen clock implementation for testing.

Provides a fixed point in time that doesn't change until explicitly set. Perfect for testing time-dependent logic deterministically.

Table of Contents

Interfaces

IClock
Interface for time/clock operations abstraction.

Properties

$frozenMicrotime  : float
$frozenTime  : int

Methods

__construct()  : mixed
Create a frozen clock at the specified time
advance()  : void
Advance the frozen time by specified seconds
advanceMicroseconds()  : void
Advance the frozen time by specified microseconds
date()  : string
Format a timestamp as a date string
microtime()  : string|float
Get current timestamp with microseconds
now()  : DateTimeImmutable
Get current DateTime object
setTime()  : void
Set the frozen time to a new value
sleep()  : void
Sleep for specified seconds
time()  : int
Get current Unix timestamp
usleep()  : void
Sleep for specified microseconds

Properties

Methods

__construct()

Create a frozen clock at the specified time

public __construct([int|null $timestamp = null ][, float|null $microtime = null ]) : mixed
Parameters
$timestamp : int|null = null

Unix timestamp to freeze at (null = current time)

$microtime : float|null = null

Microtime to freeze at (null = current microtime)

advance()

Advance the frozen time by specified seconds

public advance(int $seconds) : void
Parameters
$seconds : int

Number of seconds to advance

advanceMicroseconds()

Advance the frozen time by specified microseconds

public advanceMicroseconds(int $microseconds) : void
Parameters
$microseconds : int

Number of microseconds to advance

date()

Format a timestamp as a date string

public date(string $format[, int|null $timestamp = null ]) : string
Parameters
$format : string

Date format string

$timestamp : int|null = null

Unix timestamp (null = current time)

Tags
inheritDoc
Return values
string

Formatted date string

microtime()

Get current timestamp with microseconds

public microtime([bool $asFloat = false ]) : string|float
Parameters
$asFloat : bool = false

If true, returns float; otherwise returns string

Tags
inheritDoc
Return values
string|float

Timestamp with microseconds

now()

Get current DateTime object

public now() : DateTimeImmutable
Tags
inheritDoc
Return values
DateTimeImmutable

Current date/time

setTime()

Set the frozen time to a new value

public setTime(int $timestamp[, float|null $microtime = null ]) : void
Parameters
$timestamp : int

Unix timestamp

$microtime : float|null = null

Microtime (null = use timestamp as float)

sleep()

Sleep for specified seconds

public sleep(int $seconds) : void
Parameters
$seconds : int

Number of seconds to sleep

Tags
inheritDoc

Note: Does NOT actually sleep, just advances the frozen time

time()

Get current Unix timestamp

public time() : int
Tags
inheritDoc
Return values
int

Current timestamp in seconds since Unix epoch

usleep()

Sleep for specified microseconds

public usleep(int $microseconds) : void
Parameters
$microseconds : int

Number of microseconds to sleep

Tags
inheritDoc

Note: Does NOT actually sleep, just advances the frozen time


        
On this page

Search results