Skip to content

Latest commit

 

History

History
404 lines (311 loc) · 29.4 KB

phpdoc.md

File metadata and controls

404 lines (311 loc) · 29.4 KB

Table of contents


Interface: \RedMatter\Chrono\Clock\CalendarClockInterface

Visibility Function
public elapse(\RedMatter\Chrono\Duration\Duration $duration) : bool
When the function returns, the clock would have elapsed by the specified duration.

Returns false on failure or interruptions; otherwise true.

public now() : \RedMatter\Chrono\Time\CalendarTime
Get current calendar-time.

Time readings returned are subject to changes for clock synchronization. Difference between two values obtained after a specific duration need not be the same at all times.


Interface: \RedMatter\Chrono\Clock\SynchronisedClockInterface

SynchronisedClockInterface is to be used in cases where the logic would rely on both calendar and monotonic clocks.

Visibility Function
public elapse(\RedMatter\Chrono\Duration\Duration $duration) : bool
When the function returns, the clock would have elapsed by the specified duration.

Returns false on failure or interruptions; otherwise true.

public getCalendarTime() : \RedMatter\Chrono\Time\CalendarTime
Get time from the embedded Clock.
public getSteadyTime() : \RedMatter\Chrono\Time\SteadyTime
Get time from the embedded SteadyClock.
public perform(\callable $f, \RedMatter\Chrono\Clock\Duration/null $d=null) : mixed
Perform $f

When the function returns, the clock would have elapsed by the duration it took for $f to finish.

NOTE: This is meant for time sensitive use cases (from unit-testing perspective) where both the clocks should absolutely be in sync.

Returns the return value from $f


Class: \RedMatter\Chrono\Clock\CalendarClock

Wraps \microtime to get calendar time.

Visibility Function
public elapse(\RedMatter\Chrono\Duration\Duration $duration) : bool
When the function returns, the clock would have elapsed by the specified duration.

Returns false on failure or interruptions; otherwise true.

public now() : \RedMatter\Chrono\Time\CalendarTime
Get current calendar-time.

Time readings returned are subject to changes for clock synchronization. Difference between two values obtained after a specific duration need not be the same at all times.

This class implements \RedMatter\Chrono\Clock\CalendarClockInterface


Class: \RedMatter\Chrono\Clock\SteadyClock

Wraps hrtime to get monotonic time.

NOTE: Proper implementation of \hrtime is only available in php >= 7.3; without it the accuracy of SteadyClock will be affected by clock adjustments, either due to NTP sync or manual changes to system time.

Visibility Function
public elapse(\RedMatter\Chrono\Duration\Duration $duration) : bool
When the function returns, the clock would have elapsed by the specified duration.

Returns false on failure or interruptions; otherwise true.

public now() : \RedMatter\Chrono\Time\SteadyTime
Get current monotonic-time.

When used with PHP 7.3 or later, the time readings returned are free from changes due to clock synchronization. Even though older versions of PHP, which uses microtime under the hood, are affected by clock synchronization, it is still the best available option.

This class implements \RedMatter\Chrono\Clock\SteadyClockInterface


Class: \RedMatter\Chrono\Clock\SynchronisedClock

SynchronisedClock wraps both calendar-clock and monotonic-clock for use-cases that require both the clocks.

Visibility Function
public __construct() : void
public elapse(\RedMatter\Chrono\Duration\Duration $duration) : bool
When the function returns, the clock would have elapsed by the specified duration.

Returns false on failure or interruptions; otherwise true.

public getCalendarTime() : \RedMatter\Chrono\Time\CalendarTime
Get time from the embedded Clock.
public getSteadyTime() : \RedMatter\Chrono\Time\SteadyTime
Get time from the embedded SteadyClock.
public perform(\callable $f, \RedMatter\Chrono\Clock\Duration/null $d=null) : mixed
Perform $f

When the function returns, the clock would have elapsed by the duration it took for $f to finish.

NOTE: This is meant for time sensitive use cases (from unit-testing perspective) where both the clocks should absolutely be in sync.

Returns the return value from $f

This class implements \RedMatter\Chrono\Clock\SynchronisedClockInterface


Interface: \RedMatter\Chrono\Clock\SteadyClockInterface

Visibility Function
public elapse(\RedMatter\Chrono\Duration\Duration $duration) : bool
When the function returns, the clock would have elapsed by the specified duration.

Returns false on failure or interruptions; otherwise true.

public now() : \RedMatter\Chrono\Time\SteadyTime
Get current monotonic-time.

When used with PHP 7.3 or later, the time readings returned are free from changes due to clock synchronization. Even though older versions of PHP, which uses microtime under the hood, are affected by clock synchronization, it is still the best available option.


Class: \RedMatter\Chrono\Clock\Mock\CalendarClock

Mock Clock that models calendar-clock.

Visibility Function
public elapse(\RedMatter\Chrono\Duration\Duration $duration) : bool
public now() : \RedMatter\Chrono\Time\CalendarTime
public setDateTime(\DateTime $time) : void
Set the time from DateTime
public setTime(\RedMatter\Chrono\Time\CalendarTime $time) : void
Set the time

This class implements \RedMatter\Chrono\Clock\CalendarClockInterface


Class: \RedMatter\Chrono\Clock\Mock\SteadyClock

Mock Clock that models monotonic-clock.

Visibility Function
public elapse(\RedMatter\Chrono\Duration\Duration $duration) : bool
public now() : \RedMatter\Chrono\Time\SteadyTime
public setTime(\RedMatter\Chrono\Time\SteadyTime $time) : void
Set the time

This class implements \RedMatter\Chrono\Clock\SteadyClockInterface


Class: \RedMatter\Chrono\Clock\Mock\SynchronisedClock

A utility class to keep the two clocks in sync.

Visibility Function
public __construct(\RedMatter\Chrono\Clock\Mock\DateTime/null/\DateTime $startTime=null) : void
Construct clocks, optionally set to a specific time.
public elapse(\RedMatter\Chrono\Duration\Duration $duration) : bool
public getCalendarTime() : \RedMatter\Chrono\Time\CalendarTime
public getSteadyTime() : \RedMatter\Chrono\Time\SteadyTime
public perform(\callable $f, \RedMatter\Chrono\Clock\Mock\Duration/null $d=null) : mixed

This class implements \RedMatter\Chrono\Clock\SynchronisedClockInterface


Class: \RedMatter\Chrono\Duration\Days

Visibility Function
public __construct(mixed $value) : void

This class extends \RedMatter\Chrono\Duration\Duration


Class: \RedMatter\Chrono\Duration\Unit

Duration unit constants and utilities.

Visibility Function
public static customUnitToString(float $unit) : string
Get string representation for a custom unit. If the unit matches one of the predefined ones, then that will be chosen.
public static toString(float $unit, bool $plural=false) : string/null
Get unit name from constants; returns null if not a predefined unit

Class: \RedMatter\Chrono\Duration\MilliSeconds

Visibility Function
public __construct(mixed $value) : void

This class extends \RedMatter\Chrono\Duration\Duration


Class: \RedMatter\Chrono\Duration\NanoSeconds

Visibility Function
public __construct(mixed $value) : void

This class extends \RedMatter\Chrono\Duration\Duration


Class: \RedMatter\Chrono\Duration\Duration

Models time-duration and facilitates its manipulation and comparison.

Example
$oneSecond = new Seconds(1);
// it is equivalent to
$oneSecond = new Duration(1, Unit::SECONDS);
Visibility Function
public __construct(float $value, float $unit) : void
Duration constructor.
public __toString() : string
public add(\RedMatter\Chrono\Duration\Duration $other) : \RedMatter\Chrono\Duration$this
Add durations and return a new object; $this is left unmodified
public compare(\RedMatter\Chrono\Duration\Duration $other) : int
Compare $this against $other and return an integer. The return value will be

< 0 if $this is smaller than $other

0 if $this is equal to $other

> 0 if $this is bigger than $other

public static createFrom(\RedMatter\Chrono\Duration\Duration $other) : \RedMatter\Chrono\Duration\Duration/\RedMatter\Chrono\Duration\static
Create one kind of duration from other.
public divideFloat(\RedMatter\Chrono\Duration\Duration $other) : float
Get how many time $other can go into $this
public divideInt(\RedMatter\Chrono\Duration\Duration $other) : int
Get how many time $other can go into $this, in full
public intValue(float $unit=-1) : int
Get integer part of the value.
public isEqual(\RedMatter\Chrono\Duration\Duration $other) : bool
Check if $other is equal to this, after normalising the units
public isZero() : bool
Is a zero duration?
public modulo(\RedMatter\Chrono\Duration\Duration $other) : \RedMatter\Chrono\Duration\static
Calculate remainder for dividing $this by $other.
public slice(int/float $count) : \RedMatter\Chrono\Duration\Duration
Slice $this into $count equal intervals and get one.
public subtract(\RedMatter\Chrono\Duration\Duration $other) : \RedMatter\Chrono\Duration$this
Subtract $other from $this and return a new object; $this is left unmodified
public value(float $unit=-1) : float
Get duration value in given unit. If no unit is given, the object's own unit is assumed.
Examples of Duration::__toString()
$oneSecond = new Seconds(1);
$twoSeconds = new Seconds(2);
echo (string)$oneSecond; // prints "1 second"
echo (string)$twoSeconds; // prints "2 seconds"
Examples of Duration::add()
$d1 = new Seconds(10);
$res = $d1->add(new Seconds(3));  // $res will be "13 seconds"
$res = $d1->add(new Seconds(-3)); // $res will be "7 seconds"
Examples of Duration::compare()
$oneSecond = new Seconds(1);
$twoSeconds = new Seconds(1);
$oneThousandMSeconds = new MilliSeconds(1000);
echo $oneSecond->compare($oneThousandMSeconds); // prints "0"
echo $oneSecond->compare($twoSeconds); // prints "-1"
Examples of Duration::createFrom()
$tenSeconds = new Seconds(10);
$tenE6Microseconds = MicroSeconds::createFrom($d1);
Examples of Duration::divideFloat()
$d1 = new Seconds(100);
$res = $d1->divideFloat(new Seconds(400)); // $res will be 0.25
Examples of Duration::divideInt()
$d1 = new Seconds(400);
$res = $d1->divideInt(new Seconds(100)); // $res will be 4
Examples of Duration::intValue()
$d1 = new Seconds(10);
$res = $d1->slice(4)->intValue(); // $res will be 2
Examples of Duration::modulo()
$d1 = new Seconds(11);
$modulo = $d1->modulo(new Seconds(10)); // $modulo will be "1 second"
Examples of Duration::slice()
$d1 = new Seconds(100);
$res = $d1->slice(4); // $res will be "25 seconds"
Examples of Duration::subtract()
$d1 = new Seconds(10);
$res = $d1->subtract(new Seconds(3));  // $res will be "7 seconds"
$res = $d1->subtract(new Seconds(-3)); // $res will be "13 seconds"
Examples of Duration::value()
$d1 = new Seconds(10);
$res = $d1->slice(4)->value(); // $res will be 2.5
$res = $d1->slice(4)->value(Unit::MILLISECONDS); // $res will be 2500

Class: \RedMatter\Chrono\Duration\Minutes

Visibility Function
public __construct(mixed $value) : void

This class extends \RedMatter\Chrono\Duration\Duration


Class: \RedMatter\Chrono\Duration\MicroSeconds

Visibility Function
public __construct(mixed $value) : void

This class extends \RedMatter\Chrono\Duration\Duration


Class: \RedMatter\Chrono\Duration\Hours

Visibility Function
public __construct(mixed $value) : void

This class extends \RedMatter\Chrono\Duration\Duration


Class: \RedMatter\Chrono\Duration\Seconds

Visibility Function
public __construct(mixed $value) : void

This class extends \RedMatter\Chrono\Duration\Duration


Class: \RedMatter\Chrono\Time\SteadyTime

Visibility Function
public __construct(float $sinceEpoch) : void
time constructor
public __toString() : string
Convert to string
public after(\RedMatter\Chrono\Duration\Duration $duration) : \RedMatter\Chrono\Time\static
Get time that is after $this by $duration
public before(\RedMatter\Chrono\Duration\Duration $duration) : \RedMatter\Chrono\Time\static
Get time that is before $this by $duration
public diff(\RedMatter\Chrono\Time\TimeInterface $other) : \RedMatter\Chrono\Time\Duration
Get difference between times
public static fromTime(\RedMatter\Chrono\Time\CalendarTime $t) : \RedMatter\Chrono\Time\SteadyTime
Convert calendar-time to monotonic-time.

NOTE: Accuracy will be affected by the PHP version; php >= 7.3 preferred

public isAfter(\RedMatter\Chrono\Time\TimeInterface $other) : bool
Check if $this falls after $other.
public isBefore(\RedMatter\Chrono\Time\TimeInterface $other) : bool
Check if $this falls before $other.
public isEqual(\RedMatter\Chrono\Time\TimeInterface $other) : bool
Check if $other time is equal to $this; if they differ in type, it will be false.
public secondsSinceEpoch() : \RedMatter\Chrono\Duration\Seconds
Get seconds since epoch

This class implements \RedMatter\Chrono\Time\TimeInterface


Class: \RedMatter\Chrono\Time\CalendarTime

Visibility Function
public __construct(float $sinceEpoch) : void
time constructor
public __toString() : string
public after(\RedMatter\Chrono\Duration\Duration $duration) : \RedMatter\Chrono\Time\static
Get time that is after $this by $duration
public before(\RedMatter\Chrono\Duration\Duration $duration) : \RedMatter\Chrono\Time\static
Get time that is before $this by $duration
public diff(\RedMatter\Chrono\Time\TimeInterface $other) : \RedMatter\Chrono\Time\Duration
Get difference between times
public format(string $fmt='Y-m-d\TH:i:s.uP', \RedMatter\Chrono\Time\DateTimeZone/null/\DateTimeZone $timeZone=null) : string
Format time using DateTime::format
public static fromDateTime(\DateTime $time) : \RedMatter\Chrono\Time\CalendarTime
Convert DateTime to calendar-time.
public static fromSteadyTime(\RedMatter\Chrono\Time\SteadyTime $t) : \RedMatter\Chrono\Time\CalendarTime
Convert monotonic-time to calendar-time

NOTE: Accuracy will be affected by the PHP version; php >= 7.3 preferred

public getDateTime(\RedMatter\Chrono\Time\DateTimeZone/null/\DateTimeZone $timeZone=null) : \RedMatter\Chrono\Time\DateTime
Get DateTime

If no timezone is specified, it is set to '+00:00' except for PHP < 5.5.10. For PHP < 5.5.10 it is set to 'UTC'.

public getUnixTime(bool $float=false) : int/float
Get unixtime
public isAfter(\RedMatter\Chrono\Time\TimeInterface $other) : bool
Check if $this falls after $other.
public isBefore(\RedMatter\Chrono\Time\TimeInterface $other) : bool
Check if $this falls before $other.
public isEqual(\RedMatter\Chrono\Time\TimeInterface $other) : bool
Check if $other time is equal to $this; if they differ in type, it will be false.
public secondsSinceEpoch() : \RedMatter\Chrono\Duration\Seconds
Get seconds since epoch

This class implements \RedMatter\Chrono\Time\TimeInterface, \RedMatter\Chrono\Time\CalendarTimeInterface


Interface: \RedMatter\Chrono\Time\CalendarTimeInterface

Visibility Function
public getDateTime(\RedMatter\Chrono\Time\DateTimeZone/null/\DateTimeZone $timeZone=null) : \RedMatter\Chrono\Time\DateTime
Get DateTime

If no timezone is specified, it is set to '+00:00' except for PHP < 5.5.10. For PHP < 5.5.10 it is set to 'UTC'.

public getUnixTime(bool $float=false) : int/float
Get unixtime

Interface: \RedMatter\Chrono\Time\TimeInterface

Visibility Function
public after(\RedMatter\Chrono\Duration\Duration $duration) : \RedMatter\Chrono\Time\static
Get time that is after $this by $duration
public before(\RedMatter\Chrono\Duration\Duration $duration) : \RedMatter\Chrono\Time\static
Get time that is before $this by $duration
public diff(\RedMatter\Chrono\Time\TimeInterface $other) : \RedMatter\Chrono\Duration\Duration
Get difference between times
public isAfter(\RedMatter\Chrono\Time\TimeInterface $other) : bool
Check if $this falls after $other.
public isBefore(\RedMatter\Chrono\Time\TimeInterface $other) : bool
Check if $this falls before $other.
public isEqual(\RedMatter\Chrono\Time\TimeInterface $other) : bool
Check if $other time is equal to $this; if they differ in type, it will be false.
public secondsSinceEpoch() : \RedMatter\Chrono\Duration\Seconds
Get seconds since epoch