fixed failing tests for time values

This commit is contained in:
Joby 2023-09-10 05:06:23 +00:00
parent 1a12390c71
commit 7e87f6d618
6 changed files with 25 additions and 24 deletions

View file

@ -4,9 +4,6 @@ namespace ByJoby\HTML\Html5\InlineTextSemantics;
use ByJoby\HTML\Html5\InlineTextSemantics\TimeTag\DatetimeValue;
use ByJoby\HTML\Tags\AbstractContainerTag;
use DateInterval;
use DateTimeInterface;
use Stringable;
/**
* The <time> HTML element represents a specific period in time. It may include

View file

@ -55,7 +55,7 @@ abstract class DatetimeValue implements StringableValue
* Matches any integer from 00 to 59, leading zero optional. Optionally
* followed by a three-digit decimal portion.
*/
const REGEX_SECOND = '((?<second>0?[0-9]|[1-5][0-9])(\.(?<microsecond>[0-9]{3}))?)';
const REGEX_SECOND = '((?<second>0?[0-9]|[1-5][0-9])(\.(?<millisecond>[0-9]{3}))?)';
/**
* Tries parsing with all subclasses and returns the first one that

View file

@ -14,9 +14,6 @@ use Stringable;
*/
class DatetimeValue_datetime_local extends DatetimeValue
{
/** @var DateTime */
public $datetime;
public static function fromString(string|Stringable|null $string): null|self
{
// null string returns null
@ -38,24 +35,26 @@ class DatetimeValue_datetime_local extends DatetimeValue
)
) {
return new self(
intval($matches['year']),
intval($matches['month']),
intval($matches['day']),
intval($matches['hour']),
intval($matches['minute']),
intval(@$matches['second']),
intval(@$matches['millisecond'])
(new DateTime())
->setDate(
intval($matches['year']),
intval($matches['month']),
intval($matches['day'])
)
->setTime(
intval($matches['hour']),
intval($matches['minute']),
intval(@$matches['second']),
intval(@$matches['millisecond']) * 1000
)
);
}
// return null if nothing found
return null;
}
public function __construct(int $year, int $month, int $day, int $hour, int $minute, int $second = 0, int $millisecond = 0)
public function __construct(public DateTime $datetime)
{
$this->datetime = (new DateTime())
->setDate($year, $month, $day)
->setTime($hour, $minute, $second, $millisecond * 1000);
}
public function __toString()

View file

@ -37,7 +37,7 @@ class DatetimeValue_time extends DatetimeValue
intval($matches['hour']),
intval($matches['minute']),
intval(@$matches['second']),
intval(@$matches['microsecond'])
intval(@$matches['millisecond'])
);
}
// return null if nothing found
@ -47,7 +47,12 @@ class DatetimeValue_time extends DatetimeValue
public function __construct(int $hour, int $minute, int $second = 0, int $millisecond = 0)
{
$this->datetime = (new DateTime())
->setTime($hour, $minute, $second, $millisecond*1000);
->setTime(
$hour,
$minute,
$second,
$millisecond * 1000
);
}
public function __toString()

View file

@ -41,7 +41,7 @@ class DatetimeValue_week extends DatetimeValue
public function __toString()
{
return sprintf(
'%s%04d-%02d',
'%s%04d-W%02d',
$this->year < 0 ? '-' : '',
abs($this->year),
$this->week

View file

@ -26,10 +26,10 @@ class DatetimeValue_week_Test extends TestCase
DatetimeValue_week::fromString("99999-W53")
?->__toString()
);
// negative year
// negative year less than 4 digits
$this->assertEquals(
"-1500-06",
DatetimeValue_week::fromString("-1500-06")
"-0150-W06",
DatetimeValue_week::fromString("-0150-W06")
?->__toString()
);
}