Skip to content

Commit

Permalink
fix for dates without month #192
Browse files Browse the repository at this point in the history
  • Loading branch information
irfan-dahir committed Sep 25, 2018
1 parent 09f8c34 commit 119bbac
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Helper/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,15 @@ public static function parseForumDate(string $date): ?\DateTimeImmutable
public static function parseDate(string $date): ?\DateTimeImmutable
{
if (preg_match('/^\d{4}$/', $date)) {
return \DateTimeImmutable::createFromFormat('Y-m-d', $date.'-01-01', new \DateTimeZone('UTC'));
return \DateTimeImmutable::createFromFormat('!Y-m-d', $date.'-01-01', new \DateTimeZone('UTC'));
}

if (preg_match('/(\w{3}), (\d{4})/', $date, $matches)) {
return \DateTimeImmutable::createFromFormat(
'!M d, Y',
"{$matches[1]} 01, {$matches[2]}",
new \DateTimeZone('UTC')
);
}

try {
Expand Down
4 changes: 4 additions & 0 deletions test/JikanTest/Helper/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public function it_gets_dates()
self::assertInstanceOf(\DateTimeImmutable::class, $date);
self::assertEquals('2004-12-01', $date->format('Y-m-d'));

$date = Parser::parseDate('Dec, 2004');
self::assertInstanceOf(\DateTimeImmutable::class, $date);
self::assertEquals('2004-12-01', $date->format('Y-m-d'));

$date = Parser::parseDate('?');
self::assertNull($date);
}
Expand Down

0 comments on commit 119bbac

Please sign in to comment.