Skip to content

Commit

Permalink
feat: datetime supports format
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Nov 24, 2023
1 parent b31402b commit efc036b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
4 changes: 3 additions & 1 deletion system/DataConverter/Cast/DatetimeCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public static function fromDataSource(mixed $value, array $params = []): Time
self::invalidTypeValueError($value);
}

return Time::parse($value);
$format = $params[0];

return Time::createFromFormat($format, $value);

Check failure on line 34 in system/DataConverter/Cast/DatetimeCast.php

View workflow job for this annotation

GitHub Actions / Psalm Analysis

NoValue

system/DataConverter/Cast/DatetimeCast.php:34:48: NoValue: All possible types for this argument were invalidated - This may be dead code (see https://psalm.dev/179)
}

public static function toDataSource(mixed $value, array $params = []): string
Expand Down
19 changes: 19 additions & 0 deletions tests/system/DataConverter/DataConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,25 @@ public function testDateTimeConvertDataFromDB(): void
$this->assertSame($expectedDate->getTimestamp(), $data['date']->getTimestamp());
}

public function testDateTimeConvertDataFromDBWithFormat(): void
{
$types = [
'id' => 'int',
'date' => 'datetime[j-M-Y]',
];
$converter = $this->createDataConverter($types);

$dbData = [
'id' => '1',
'date' => '15-Feb-2009',
];
$data = $converter->fromDataSource($dbData);

$this->assertInstanceOf(Time::class, $data['date']);
$expectedDate = Time::createFromFormat('j-M-Y', '15-Feb-2009');
$this->assertSame($expectedDate->getTimestamp(), $data['date']->getTimestamp());
}

public function testDateTimeConvertDataToDB(): void
{
$types = [
Expand Down

0 comments on commit efc036b

Please sign in to comment.