diff --git a/NEWS.md b/NEWS.md index aabe73dfab..5eff109e3e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -85,6 +85,7 @@ * Swedish `sv` - Wallabag sharer now targets Wallabag 2 by default. This is potentially breaking change but hopefully, no one uses Wallabag 1 any more. ([#1261](https://github.com/fossar/selfoss/pull/1261)) - `defaults.ini` file is no longer used, it is only provided for convenience under a new name `config-example.ini` ([#1261](https://github.com/fossar/selfoss/pull/1261), [#1267](https://github.com/fossar/selfoss/pull/1267)) +- `spout::getDate()` method now returns `DateTimeInterface` instead of a string. ([#1341](https://github.com/fossar/selfoss/pull/1341)) ### Other changes - The front-end has been modernized using React framework, this will greatly simplify future development. ([#1216](https://github.com/fossar/selfoss/pull/1216)) diff --git a/src/helpers/ContentLoader.php b/src/helpers/ContentLoader.php index 381a2bf276..71cbecdf94 100644 --- a/src/helpers/ContentLoader.php +++ b/src/helpers/ContentLoader.php @@ -159,18 +159,14 @@ public function fetch($source) { } // test date: continue with next if item too old - $itemDate = new \DateTime($item->getDate()); - // if date cannot be parsed it will default to epoch. Change to current time. - if ($itemDate->getTimestamp() == 0) { - $itemDate = new \DateTime(); - } + $itemDate = $item->getDate(); if ($itemDate < $minDate) { $this->logger->debug('item "' . $item->getTitle() . '" (' . $item->getDate() . ') older than ' . $this->configuration->itemsLifetime . ' days'); continue; } // date in future? Set current date - $now = new \DateTime(); + $now = new \DateTimeImmutable(); if ($itemDate > $now) { $itemDate = $now; } diff --git a/src/spouts/facebook/page.php b/src/spouts/facebook/page.php index 4388154355..c13fb8aa01 100644 --- a/src/spouts/facebook/page.php +++ b/src/spouts/facebook/page.php @@ -64,6 +64,7 @@ public function __construct(WebClient $webClient) { } public function load(array $params) { + // https://developers.facebook.com/docs/graph-api/reference/user $http = $this->webClient->getHttpClient(); $url = new Uri('https://graph.facebook.com/' . urlencode($params['user'])); $url = $url->withQueryValues($url, [ @@ -75,6 +76,7 @@ public function load(array $params) { $this->spoutTitle = $data['name']; $this->pagePicture = $data['picture']['data']['url']; $this->pageLink = $data['picture']['link']; + // https://developers.facebook.com/docs/graph-api/reference/user/feed/ $this->items = new ArrayIterator($data['feed']['data']); } @@ -150,10 +152,12 @@ public function getLink() { public function getDate() { if ($this->valid()) { $item = $this->items->current(); - - return $item['created_time']; + // The docs say UNIX timestamp but appears to be ISO 8601. + // https://developers.facebook.com/docs/graph-api/reference/post/ + // https://stackoverflow.com/questions/14516792/what-is-the-time-format-used-in-facebook-created-date + return new \DateTimeImmutable($item['created_time']); } - return false; + return new \DateTimeImmutable(); } } diff --git a/src/spouts/github/commits.php b/src/spouts/github/commits.php index a8092e1422..e1bf754dcf 100644 --- a/src/spouts/github/commits.php +++ b/src/spouts/github/commits.php @@ -67,6 +67,7 @@ public function __construct(WebClient $webClient) { public function load(array $params) { $this->htmlUrl = 'https://github.com/' . urlencode($params['owner']) . '/' . urlencode($params['repo']) . '/' . urlencode($params['branch']); + // https://docs.github.com/en/rest/commits/commits#list-commits $jsonUrl = 'https://api.github.com/repos/' . urlencode($params['owner']) . '/' . urlencode($params['repo']) . '/commits?sha=' . urlencode($params['branch']); $http = $this->webClient->getHttpClient(); @@ -123,13 +124,11 @@ public function getLink() { public function getDate() { if ($this->valid()) { - $date = date('Y-m-d H:i:s', strtotime($this->items->current()['commit']['author']['date'])); - } - if (strlen($date) === 0) { - $date = date('Y-m-d H:i:s'); + // Appears to be ISO 8601. + return new \DateTimeImmutable($this->items->current()['commit']['author']['date']); } - return $date; + return new \DateTimeImmutable(); } public function destroy() { diff --git a/src/spouts/reddit/reddit2.php b/src/spouts/reddit/reddit2.php index 87bd77d59d..0b7fe16627 100644 --- a/src/spouts/reddit/reddit2.php +++ b/src/spouts/reddit/reddit2.php @@ -187,10 +187,14 @@ public function getThumbnail() { public function getdate() { if ($this->valid()) { - $date = date('Y-m-d H:i:s', $this->items->current()['data']['created_utc']); + // UNIX timestamp + // https://www.reddit.com/r/redditdev/comments/3qsv97/whats_the_time_unit_for_created_utc_and_what_time/ + $timestamp = (string) $this->items->current()['data']['created_utc']; + + return new \DateTimeImmutable('@' . $timestamp); } - return $date; + return new \DateTimeImmutable(); } public function destroy() { diff --git a/src/spouts/rss/feed.php b/src/spouts/rss/feed.php index 69372c64ff..649dd45fa8 100644 --- a/src/spouts/rss/feed.php +++ b/src/spouts/rss/feed.php @@ -160,13 +160,12 @@ public function getLink() { public function getDate() { if ($this->valid()) { - $date = $this->items->current()->get_date('Y-m-d H:i:s'); - } - if (strlen($date) === 0) { - $date = date('Y-m-d H:i:s'); + $timestamp = (string) $this->items->current()->get_date('U'); + + return new \DateTimeImmutable('@' . $timestamp); } - return $date; + return new \DateTimeImmutable(); } public function getAuthor() { diff --git a/src/spouts/spout.php b/src/spouts/spout.php index d514358820..57c2128d50 100644 --- a/src/spouts/spout.php +++ b/src/spouts/spout.php @@ -149,7 +149,7 @@ abstract public function getLink(); /** * returns the date of this item * - * @return string date + * @return \DateTimeInterface date */ abstract public function getDate(); diff --git a/src/spouts/twitter/usertimeline.php b/src/spouts/twitter/usertimeline.php index 9936b68983..76eb933911 100644 --- a/src/spouts/twitter/usertimeline.php +++ b/src/spouts/twitter/usertimeline.php @@ -280,13 +280,12 @@ public function getThumbnail() { public function getDate() { if ($this->items !== null) { - $date = date('Y-m-d H:i:s', strtotime($this->items->current()->created_at)); - } - if (strlen($date) === 0) { - $date = date('Y-m-d H:i:s'); + // Format of `created_at` field not specified, looks US-centric. + // https://developer.twitter.com/en/docs/twitter-api/v1/data-dictionary/object-model/tweet + return new \DateTimeImmutable($this->items->current()->created_at); } - return $date; + return new \DateTimeImmutable(); } public function destroy() {