Skip to content

Commit

Permalink
Time: add micro, time, date
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna committed Nov 7, 2023
1 parent 75e70c1 commit 4c3eacc
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
28 changes: 26 additions & 2 deletions src/Date/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@ final class Time
{
use StaticClass;

public static function micro(): float
{
return microtime(true);
}


/**
* @return ($dateTime is DateTimeImmutable ? DateTimeImmutable : DateTime)
*/
public static function set(
public static function time(
DateTime|DateTimeImmutable $dateTime,
?int $hour = null,
?int $minutes = null,
?int $seconds = null,
?int $microseconds = 0
?int $microseconds = null,
): DateTime|DateTimeImmutable
{
return $dateTime->setTime(
Expand All @@ -28,4 +34,22 @@ public static function set(
$microseconds ?? (int) $dateTime->format('u'),
);
}


/**
* @return ($dateTime is DateTimeImmutable ? DateTimeImmutable : DateTime)
*/
public static function date(
DateTime|DateTimeImmutable $dateTime,
?int $year = null,
?int $month = null,
?int $day = null,
): DateTime|DateTimeImmutable
{
return $dateTime->setDate(
$year ?? (int) $dateTime->format('Y'),
$month ?? (int) $dateTime->format('n'),
$day ?? (int) $dateTime->format('j'),
);
}
}
12 changes: 9 additions & 3 deletions tests/src/Unit/Date/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function provideMakeFromString(): array
['expected' => self::format(new DateTime()), 'input' => 'now'],
['expected' => '2023-06-11 07:00:00', 'input' => '0'],
['expected' => '2023-06-11 07:30:00', 'input' => '0.5'],
// // decimal number has same behavior with or without +
// // decimal number has same behavior with or without +
['expected' => '2023-06-11 07:30:00', 'input' => '+0.5'],
['expected' => '2023-06-11 07:30:00', 'input' => '+0,5'],
['expected' => '2023-06-11 08:45:00', 'input' => '+1,75'],
Expand Down Expand Up @@ -62,9 +62,15 @@ public function testMakeFromString(string $expected, string $input): void
}


public function testEmpty(): void
public function testTime(): void
{
Assert::equal(Date\Time::set(new DateTimeImmutable()), Date\Time::set(Date\Parser::fromString('')));
Assert::equal(Date\Time::time(new DateTimeImmutable(), microseconds: 0), Date\Time::time(Date\Parser::fromString(''), microseconds: 0));
}


public function testDate(): void
{
Assert::equal(Date\Time::date(new DateTimeImmutable())->format('Y-m-d'), Date\Time::date(Date\Parser::fromString(''))->format('Y-m-d'));
}


Expand Down

0 comments on commit 4c3eacc

Please sign in to comment.