Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Interval): add Interval count seconds
Browse files Browse the repository at this point in the history
h4kuna committed Apr 22, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 1666ff3 commit 989a929
Showing 3 changed files with 76 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/Date/Interval.php
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;
}
}
2 changes: 0 additions & 2 deletions src/Date/Sleep.php
Original file line number Diff line number Diff line change
@@ -2,8 +2,6 @@

namespace h4kuna\DataType\Date;

use h4kuna\DataType\Exceptions\InvalidArgumentsException;

final class Sleep
{
/**
59 changes: 59 additions & 0 deletions tests/src/Unit/Date/IntervalTest.php
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();

0 comments on commit 989a929

Please sign in to comment.