Skip to content

Commit

Permalink
Parser: fix float to time
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna committed Oct 2, 2023
1 parent f91e6e6 commit f722b7a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
12 changes: 2 additions & 10 deletions src/Date/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ final class Parser
/** @var array<string> */
public static array $formats = ['d.m. H:i', 'm-d H:i', 'd.m.Y H:i', 'Y-m-d H:i', 'd.m.Y H:i:s', 'Y-m-d H:i:s'];


/**
* @param string $any
* @param DateTime|DateTimeImmutable|null $dateTime
Expand Down Expand Up @@ -47,16 +48,7 @@ public static function fromString(

private static function modifierFromFloat(float $value): string
{
$way = $value < 0 ? -1 : 1;
[$hour, $minute] = explode('.', (string) $value);

$minute = intval($minute);
$hour = intval($hour);
if ($minute !== 0) {
$minute = (int) round(6 / (1 / $minute));
}

return self::modifier($hour, $minute * $way);
return self::modifier(0, 0, (int) ($value * 3600));
}


Expand Down
10 changes: 6 additions & 4 deletions tests/src/Unit/Date/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ final class ParserTest extends TestCase
public function provideMakeFromString(): array
{
return [
// ['expected' => '2023-06-11 07:00:00', 'input' => ''],
// ['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'],
['expected' => '2023-06-11 07:00:00', 'input' => ''],
['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 +
['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'],
['expected' => '2023-06-11 06:30:00', 'input' => '-0,5'],
['expected' => '2023-06-11 05:30:00', 'input' => '-1,5'],
['expected' => '2023-06-11 05:34:48', 'input' => '-1,42'],
['expected' => '2023-06-11 08:00:00', 'input' => '+1'],
['expected' => '2023-06-11 17:00:00', 'input' => '+10'],
['expected' => '2023-06-11 10:00:00', 'input' => '10'],
Expand Down

0 comments on commit f722b7a

Please sign in to comment.