Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Put env variables defaults here
# Override locally in gitignored .env.local
PHP_IMAGE_VERSION=8.3
PHP_IMAGE_VERSION=8.4
6 changes: 3 additions & 3 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;
use PHPyh\CodingStandard\PhpCsFixerCodingStandard;

$config = (new Config())
$config = new Config()
->setFinder(
Finder::create()
->in(__DIR__ . '/src')
Expand All @@ -19,8 +19,8 @@
->setParallelConfig(ParallelConfigFactory::detect())
->setCacheFile(__DIR__ . '/var/' . basename(__FILE__) . '.cache');

(new PhpCsFixerCodingStandard())->applyTo($config, [
// 'rule' => ['overridden' => 'config'],
new PhpCsFixerCodingStandard()->applyTo($config, [
'final_class' => false,
]);

return $config;
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
}
],
"require": {
"php-64bit": "^8.3"
"php-64bit": "^8.4"
},
"require-dev": {
"phpunit/phpunit": "^12.1.4",
Expand All @@ -25,14 +25,15 @@
"Thesis\\": "src/"
},
"files": [
"src/Duration/toInt.php",
"src/Time/TimeSpan.php",
"src/TimeSpan/Internal/calculateFormatReplacements.php",
"src/TimeSpan/Internal/isCastableToInt.php"
]
},
"autoload-dev": {
"psr-4": {
"Thesis\\": "tests/"
"Thesis\\Time\\": "tests/"
}
},
"config": {
Expand Down
18 changes: 18 additions & 0 deletions src/Duration/Microseconds.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Thesis\Duration;

class Microseconds extends Nanoseconds
{
public function __construct(
int|float $microseconds = 0,
?\RoundingMode $roundingMode = null,
) {
parent::__construct(
nanoseconds: $microseconds * 1_000,
roundingMode: $roundingMode,
);
}
}
18 changes: 18 additions & 0 deletions src/Duration/Milliseconds.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Thesis\Duration;

class Milliseconds extends Microseconds
{
public function __construct(
int|float $milliseconds = 0,
?\RoundingMode $roundingMode = null,
) {
parent::__construct(
microseconds: $milliseconds * 1_000,
roundingMode: $roundingMode,
);
}
}
126 changes: 126 additions & 0 deletions src/Duration/Nanoseconds.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

declare(strict_types=1);

namespace Thesis\Duration;

use Thesis\TimeSpan\Internal\Unit;

/**
* @phpstan-consistent-constructor
*/
class Nanoseconds
{
private int $nanoseconds;

public function __construct(int|float $nanoseconds = 0, ?\RoundingMode $roundingMode = null)
{
$this->nanoseconds = toInt($nanoseconds, $roundingMode);
}

final public function abs(): static
{
if ($this->nanoseconds >= 0) {
return $this;
}

return new static(-$this->nanoseconds);
}

final public function negated(): static
{
return new static(-$this->nanoseconds);
}

final public function toMicroseconds(?\RoundingMode $roundingMode = null): Microseconds
{
if ($this instanceof Microseconds) {
return $this;
}

return new Microseconds($this->nanoseconds / Unit::MICROSECONDS, $roundingMode);
}

final public function toMilliseconds(?\RoundingMode $roundingMode = null): Milliseconds
{
if ($this instanceof Milliseconds) {
return $this;
}

return new Milliseconds($this->nanoseconds / Unit::MILLISECONDS, $roundingMode);
}

final public function totalNanoseconds(): int
{
return $this->nanoseconds;
}

final public function totalMicroseconds(\RoundingMode $roundingMode = \RoundingMode::HalfAwayFromZero): int
{
return (int) round($this->nanoseconds / Unit::MICROSECONDS, mode: $roundingMode);
}

final public function totalMilliseconds(\RoundingMode $roundingMode = \RoundingMode::HalfAwayFromZero): int
{
return (int) round($this->nanoseconds / Unit::MILLISECONDS, mode: $roundingMode);
}

/**
* @template T of self
* @param T $duration
* @return ($duration is static ? static : T)
*/
final public function add(self $duration): self
{
$sum = $this->nanoseconds + $duration->nanoseconds;

if ($duration instanceof $this) {
return $this->with($sum);
}

return $duration->with($sum);
}

/**
* @template T of self
* @param T $duration
* @return ($duration is static ? static : T)
*/
final public function sub(self $duration): self
{
$diff = $this->nanoseconds - $duration->nanoseconds;

if ($this instanceof $duration) {
return $duration->with($diff);
}

return $this->with($diff);
}

/**
* @return ($times is int ? static : self)
*/
final public function mul(int|float $times): self
{
$product = $this->nanoseconds * $times;

if (\is_int($times)) {
return $this->with($product);
}

return new self($this->nanoseconds * $times);
}

final public function div(int|float $factor): self
{
return new self($this->nanoseconds / $factor);
}

private function with(int|float $nanoseconds): static
{
$copy = clone $this;
$copy->nanoseconds = toInt($nanoseconds, \RoundingMode::HalfAwayFromZero);

return $copy;
}
}
29 changes: 29 additions & 0 deletions src/Duration/toInt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Thesis\Duration;

/**
* @internal
*/
function toInt(int|float $value, ?\RoundingMode $roundingMode): int
{
if (\is_int($value)) {
return $value;
}

if (!(is_finite($value) && $value >= PHP_INT_MIN && $value < PHP_INT_MAX)) {
throw new \OverflowException();
}

if (fmod($value, 1.0) !== 0.0) {
if ($roundingMode === null) {
throw new \LogicException('Rounding mode required');
}

$value = round($value, mode: $roundingMode);
}

return (int) $value;
}
Loading