diff --git a/OPDS_renderer.php b/OPDS_renderer.php index c460978..14d1f33 100644 --- a/OPDS_renderer.php +++ b/OPDS_renderer.php @@ -173,6 +173,16 @@ class OPDSRenderer self::getXmlStream ()->endElement (); } + private function getPublicationDate($book) { + $dateYmd = substr($book->pubdate, 0, 10); + $pubdate = \DateTime::createFromFormat('Y-m-d', $dateYmd); + if ($pubdate === false || + $pubdate->format ("Y") == "0101" || + $pubdate->format ("Y") == "0100") { + return ""; + } + return $pubdate->format("Y-m-d"); + } private function renderEntry ($entry) { self::getXmlStream ()->startElement ("title"); @@ -218,10 +228,10 @@ class OPDSRenderer } if ($entry->book->getPubDate () != "") { self::getXmlStream ()->startElement ("dcterms:issued"); - self::getXmlStream ()->text (date ("Y-m-d", $entry->book->pubdate)); + self::getXmlStream ()->text (self::getPublicationDate($entry->book)); self::getXmlStream ()->endElement (); self::getXmlStream ()->startElement ("published"); - self::getXmlStream ()->text (date ("Y-m-d", $entry->book->pubdate) . "T08:08:08Z"); + self::getXmlStream ()->text (self::getPublicationDate($entry->book) . "T08:08:08Z"); self::getXmlStream ()->endElement (); } diff --git a/book.php b/book.php index a8cf6b7..63a4636 100644 --- a/book.php +++ b/book.php @@ -94,7 +94,7 @@ class Book extends Base { $this->id = $line->id; $this->title = $line->title; $this->timestamp = strtotime ($line->timestamp); - $this->pubdate = strtotime ($line->pubdate); + $this->pubdate = $line->pubdate; $this->path = Base::getDbDirectory () . $line->path; $this->relativePath = $line->path; $this->seriesIndex = $line->series_index; @@ -265,12 +265,14 @@ class Book extends Base { } public function getPubDate () { - if (is_null ($this->pubdate) || ($this->pubdate <= -58979923200)) { + if (empty ($this->pubdate)) { return ""; } - else { - return date ("Y", $this->pubdate); + $dateY = (int) substr($this->pubdate, 0, 4); + if ($dateY > 102) { + return str_pad($dateY, 4, "0", STR_PAD_LEFT); } + return ""; } public function getComment ($withSerie = true) { diff --git a/test/bookTest.php b/test/bookTest.php index 7427f0a..b470da7 100644 --- a/test/bookTest.php +++ b/test/bookTest.php @@ -173,6 +173,27 @@ class BookTest extends PHPUnit_Framework_TestCase $this->assertCount (15, $entryArray); } + /** + * @dataProvider providerPublicationDate + */ + public function testGetPubDate ($pubdate, $expectedYear) + { + $book = Book::getBookById(2); + $book->pubdate = $pubdate; + $this->assertEquals($expectedYear, $book->getPubDate()); + } + + public function providerPublicationDate() { + return array( + array('2010-10-05 22:00:00+00:00', '2010'), + array('1982-11-15 13:05:29.908657+00:00', '1982'), + array('1562-10-05 00:00:00+00:00', '1562'), + array('0100-12-31 23:00:00+00:00', ''), + array('', ''), + array(NULL, '') + ); + } + public function testGetBookById () { // also check most of book's class methods