This is a simple library for interacting with time durations. You can write your code to expect seconds:
function sleep(\Withinboredom\Time\AnyTime $time): void {
\sleep($time->inSeconds());
}
sleep(\Withinboredom\Time\Minutes(5)->inSeconds());
All values of the same time are always strongly equaled to each other:
\Withinboredom\Time\Minutes(60) === \Withinboredom\Time\Hours(1)
There are also a few utility methods:
->add(AnyTime)->subtract(AnyTime): AnyTime
Add and subtract durations.
->toDateInterval(): DateInterval
Creates a date interval for use in other things.
If you need to change the conversion rates, create a new standard:
class Mars implements \Withinboredom\Time\TimeAndSpaceInterface {
/// ... fill in the blanks
}
This can be (mis)used to allow for very short times in unit tests (to make a week pass by in literally microseconds).
- Nanoseconds
- Microseconds
- Milliseconds
- Minutes
- Hours
- Days
- Weeks
Why not months/years?
There's no set days in a month/year, so it's better to use DateInterval
for those types of measures.
Why does this exist?
I don't like magic numbers.
Can I convert between standards?
Not yet.
How performant is this?
The main overhead is in autoloading and function-call overhead. Thus, if realtime performance is a concern, you might want to stick to magic numbers.
If you wish to create a PR or update the code here:
- Clone the repo
composer install
to install test dependenciesyarn
to install git hooks for formatting- Open in favorite IDE.
Per coding styles are followed.