-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
feat(Interval): add Interval count seconds
Showing
3 changed files
with
76 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace h4kuna\DataType\Date; | ||
|
||
use DateInterval; | ||
use Nette\Utils\DateTime; | ||
|
||
final class Interval | ||
{ | ||
public static function toSeconds(DateInterval $dateInterval): int | ||
{ | ||
return $dateInterval->days * DateTime::DAY | ||
+ $dateInterval->h * DateTime::HOUR | ||
+ $dateInterval->i * DateTime::MINUTE | ||
+ $dateInterval->s; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace h4kuna\DataType\Tests\Unit\Date; | ||
|
||
require __DIR__ . '/../../../bootstrap.php'; | ||
|
||
use Closure; | ||
use DateInterval; | ||
use DateTime; | ||
use h4kuna\DataType\Date\Interval; | ||
use Tester\Assert; | ||
use Tester\TestCase; | ||
|
||
final class IntervalTest extends TestCase | ||
{ | ||
/** | ||
* @return array<string|int, array{0: Closure(static):void}> | ||
*/ | ||
public static function dataToSeconds(): array | ||
{ | ||
return [ | ||
'29. february exists' => [ | ||
static function (self $self) { | ||
$interval = (new DateTime('2024-06-01 12:13:14'))->diff(new DateTime('2023-06-01 00:00:00')); | ||
$self->assertToSeconds(31666394, $interval); | ||
}, | ||
], | ||
'only 28. february' => [ | ||
static function (self $self) { | ||
$interval = (new DateTime('2023-06-01 12:13:14'))->diff(new DateTime('2022-06-01 00:00:00')); | ||
$self->assertToSeconds(31579994, $interval); | ||
}, | ||
], | ||
]; | ||
} | ||
|
||
|
||
/** | ||
* @param Closure(static):void $assert | ||
* @dataProvider dataToSeconds | ||
*/ | ||
public function testToSeconds(Closure $assert): void | ||
{ | ||
$assert($this); | ||
} | ||
|
||
|
||
public function assertToSeconds( | ||
int $expected, | ||
DateInterval $dateInterval, | ||
): void | ||
{ | ||
Assert::same($expected, Interval::toSeconds($dateInterval)); | ||
} | ||
} | ||
|
||
(new IntervalTest())->run(); |