From bee6506d330b687debcdeab583c10c1069e7e372 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sun, 9 Oct 2022 00:23:09 -0400 Subject: [PATCH 1/2] Only call DateTimeFormat::fix() when there's an Exception - This prevents valid date/time strings to be mangled by fix() --- src/Util/DateTimeFormat.php | 32 +++++++++++++-------------- tests/src/Util/DateTimeFormatTest.php | 14 ++++++++++++ 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/Util/DateTimeFormat.php b/src/Util/DateTimeFormat.php index 862ce6e9a..18f6cba40 100644 --- a/src/Util/DateTimeFormat.php +++ b/src/Util/DateTimeFormat.php @@ -119,7 +119,7 @@ class DateTimeFormat * @return string Formatted date according to given format * @throws Exception */ - public static function convert($s = 'now', $tz_to = 'UTC', $tz_from = 'UTC', $format = self::MYSQL) + public static function convert(string $s = 'now', string $tz_to = 'UTC', string $tz_from = 'UTC', string $format = self::MYSQL): string { // Defaults to UTC if nothing is set, but throws an exception if set to empty string. // Provide some sane defaults regardless. @@ -135,20 +135,11 @@ class DateTimeFormat $s = 'now'; } - $s = self::fix($s); - - /* - * Slight hackish adjustment so that 'zero' datetime actually returns what is intended - * otherwise we end up with -0001-11-30 ... - * add 32 days so that we at least get year 00, and then hack around the fact that - * months and days always start with 1. - */ + // Lowest possible datetime value if (substr($s, 0, 10) <= '0001-01-01') { - if ($s < '0000-00-00') { - $s = '0000-00-00'; - } - $d = new DateTime($s . ' + 32 days', new DateTimeZone('UTC')); - return str_replace('1', '0', $d->format($format)); + $d = new DateTime('now', new DateTimeZone('UTC')); + $d->setDate(1, 1, 1)->setTime(0, 0); + return $d->format($format); } try { @@ -160,8 +151,12 @@ class DateTimeFormat try { $d = new DateTime($s, $from_obj); } catch (Exception $e) { - Logger::warning('DateTimeFormat::convert: exception: ' . $e->getMessage()); - $d = new DateTime('now', $from_obj); + try { + $d = new DateTime(self::fix($s), $from_obj); + } catch (\Throwable $e) { + Logger::warning('DateTimeFormat::convert: exception: ' . $e->getMessage()); + $d = new DateTime('now', $from_obj); + } } try { @@ -176,7 +171,10 @@ class DateTimeFormat } /** - * Fix weird date formats + * Fix weird date formats. + * + * Note: This method isn't meant to sanitize valid date/time strings, for example it will mangle relative date + * strings like "now - 3 days". * * @see \Friendica\Test\src\Util\DateTimeFormatTest::dataFix() for a list of examples handled by this method. * @param string $dateString diff --git a/tests/src/Util/DateTimeFormatTest.php b/tests/src/Util/DateTimeFormatTest.php index e6f43af5a..ae2e04bdd 100644 --- a/tests/src/Util/DateTimeFormatTest.php +++ b/tests/src/Util/DateTimeFormatTest.php @@ -141,4 +141,18 @@ class DateTimeFormatTest extends MockedTest { $this->assertEquals($expected, DateTimeFormat::fix($dateString)); } + + /** + * This test is meant to ensure DateTimeFormat::fix() isn't called on relative date/time strings + * + * @return void + * @throws \Exception + */ + public function testConvertRelative() + { + $now = DateTimeFormat::utcNow('U'); + $date = DateTimeFormat::utc('now - 3 days', 'U'); + + $this->assertEquals(259200, $now - $date); + } } From a4b0ebf160d4ffb8b023185556938a9cfba47779 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sun, 9 Oct 2022 09:24:06 -0400 Subject: [PATCH 2/2] Add a couple of cases to DateTimeFormat::fix() - Reworked method to perform more string replacements and fewer regular expression matches --- src/Util/DateTimeFormat.php | 14 +++++----- tests/src/Util/DateTimeFormatTest.php | 39 ++++++++++++++------------- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/src/Util/DateTimeFormat.php b/src/Util/DateTimeFormat.php index 18f6cba40..8233a4f5b 100644 --- a/src/Util/DateTimeFormat.php +++ b/src/Util/DateTimeFormat.php @@ -182,17 +182,17 @@ class DateTimeFormat */ public static function fix(string $dateString): string { - $patterns = [ - ['#(\w+), (\d+/\d+/\d+) - (\d+:\d+)#', '$1, $2 $3'], - ['#(\d+-\d+-\d+)T(\d+:\d+:\d+)ZZ#', '$1T$2Z'], - ['#(\d+-\d+-\d+)T(\d+:\d+:\d+\.\d+)ZZ#', '$1T$2Z'], + $search = ['Mär', 'März', 'Mai', 'Juni', 'Juli', 'Okt', 'Dez', 'ET' , 'ZZ', ' - ', '+']; + $replace = ['Mar', 'Mar' , 'May', 'Jun' , 'Jul' , 'Oct', 'Dec', 'EST', 'Z' , ', ' , '+' ]; + + $dateString = str_replace($search, $replace, $dateString); + + $pregPatterns = [ ['#(\w+), (\d+ \w+ \d+) (\d+:\d+:\d+) (.+)#', '$2 $3 $4'], ['#(\d+:\d+) (\w+), (\w+) (\d+), (\d+)#', '$1 $2 $3 $4 $5'], - ['#(\w+ \d+, \d+) - (\d+:\d+)#', '$1, $2'], - ['~(\d+-\d+-\d+)T(\d+:\d+:\d+)+(\d+:\d+)~', '$1T$2+$3'], ]; - foreach ($patterns as $pattern) { + foreach ($pregPatterns as $pattern) { $dateString = preg_replace($pattern[0], $pattern[1], $dateString); } diff --git a/tests/src/Util/DateTimeFormatTest.php b/tests/src/Util/DateTimeFormatTest.php index ae2e04bdd..994ca3a11 100644 --- a/tests/src/Util/DateTimeFormatTest.php +++ b/tests/src/Util/DateTimeFormatTest.php @@ -92,54 +92,57 @@ class DateTimeFormatTest extends MockedTest { return [ 'Mo, 19 Sep 2022 14:51:00 +0200' => [ - 'expected' => '19 Sep 2022 14:51:00 +0200', + 'expectedDate' => '2022-09-19T14:51:00+02:00', 'dateString' => 'Mo, 19 Sep 2022 14:51:00 +0200', ], '2020-11-21T12:00:13.745339ZZ' => [ - 'expected' => '2020-11-21T12:00:13.745339Z', + 'expectedDate' => '2020-11-21T12:00:13+00:00', 'dateString' => '2020-11-21T12:00:13.745339ZZ', ], '2016-09-09T13:32:00ZZ' => [ - 'expected' => '2016-09-09T13:32:00Z', + 'expectedDate' => '2016-09-09T13:32:00+00:00', 'dateString' => '2016-09-09T13:32:00ZZ', ], - '2021-09-09T16:19:00ZZ' => [ - 'expected' => '2021-09-09T16:19:00Z', - 'dateString' => '2021-09-09T16:19:00ZZ', - ], 'Sun, 10/03/2021 - 12:41' => [ - 'expected' => 'Sun, 10/03/2021 12:41', + 'expectedDate' => '2021-10-03T12:41:00+00:00', 'dateString' => 'Sun, 10/03/2021 - 12:41', ], - 'Mon, 09/12/2022 - 09:02' => [ - 'expected' => 'Mon, 09/12/2022 09:02', - 'dateString' => 'Mon, 09/12/2022 - 09:02', - ], '4:30 PM, Sep 13, 2022' => [ - 'expected' => '4:30 PM Sep 13 2022', + 'expectedDate' => '2022-09-13T16:30:00+00:00', 'dateString' => '4:30 PM, Sep 13, 2022', ], 'August 27, 2022 - 21:00' => [ - 'expected' => 'August 27, 2022, 21:00', + 'expectedDate' => '2022-08-27T21:00:00+00:00', 'dateString' => 'August 27, 2022 - 21:00', ], '2021-09-19T14:06:03+00:00' => [ - 'expected' => '2021-09-19T14:06:03+00:00', + 'expectedDate' => '2021-09-19T14:06:03+00:00', 'dateString' => '2021-09-19T14:06:03+00:00', ], + 'Eastern Time timezone' => [ + 'expectedDate' => '2022-09-30T00:00:00-05:00', + 'dateString' => 'September 30, 2022, 12:00 a.m. ET', + ], + 'German date time string' => [ + 'expectedDate' => '2022-10-05T16:34:00+02:00', + 'dateString' => '05 Okt 2022 16:34:00 +0200', + ] ]; } /** * @dataProvider dataFix * - * @param $expected + * @param $expectedDate * @param $dateString * @return void + * @throws \Exception */ - public function testFix($expected, $dateString) + public function testFix($expectedDate, $dateString) { - $this->assertEquals($expected, DateTimeFormat::fix($dateString)); + $fixed = DateTimeFormat::fix($dateString); + + $this->assertEquals($expectedDate, (new \DateTime($fixed))->format('c')); } /**