]> git.mxchange.org Git - friendica.git/commitdiff
Add support for more datetime formats
authorHypolite Petovan <hypolite@mrpetovan.com>
Thu, 13 Mar 2025 02:46:36 +0000 (22:46 -0400)
committerHypolite Petovan <hypolite@mrpetovan.com>
Tue, 18 Mar 2025 23:01:42 +0000 (19:01 -0400)
- Unix Timestamp
- RFC 3339 extended + timezone name in square brackets
- Address https://github.com/friendica/friendica/issues/14647#issuecomment-2719430131

src/Util/DateTimeFormat.php
tests/src/Util/DateTimeFormatTest.php

index 42b49cbf7db3327484b2fc03beea476e0d7769b0..830b50a2fe7d9509e43a3e2361045c7d7e610f99 100644 (file)
@@ -117,7 +117,7 @@ class DateTimeFormat
                        $tz_to = 'UTC';
                }
 
-               if (($s === '') || (!is_string($s))) {
+               if ($s === '') {
                        $s = 'now';
                }
 
@@ -135,7 +135,8 @@ class DateTimeFormat
                }
 
                try {
-                       $d = new DateTime($s, $from_obj);
+                       $d = DateTime::createFromFormat('U', $s, $from_obj)
+                               ?: new DateTime($s, $from_obj);
                } catch (Exception $e) {
                        try {
                                $d = new DateTime(self::fix($s), $from_obj);
@@ -176,6 +177,7 @@ class DateTimeFormat
                $pregPatterns = [
                        ['#(\w+), (\d+ \w+ \d+) (\d+:\d+:\d+) (.+)#', '$2 $3 $4'],
                        ['#(\d+:\d+) (\w+), (\w+) (\d+), (\d+)#', '$1 $2 $3 $4 $5'],
+                       ['#\[[^\]]*\]#', ''], // 2025-03-07T08:54:14.341+01:00[Europe/Berlin]
                ];
 
                foreach ($pregPatterns as $pattern) {
index 9137659ba83ceb28a092cc8b3aca4761b3fdccd9..186741a67c8822e64bd31636f4f29fba62108d6c 100644 (file)
@@ -125,6 +125,10 @@ class DateTimeFormatTest extends MockedTestCase
                                'expectedDate' => '2023-04-02T17:22:42+05:30',
                                'dateString' => '2023-04-02\T17:22:42+05:30'
                        ],
+                       '2025-03-07T08:54:14.341+01:00[Europe/Berlin]' => [
+                               'expectedDate' => '2025-03-07T08:54:14+01:00',
+                               'dateString' => '2025-03-07T08:54:14.341+01:00[Europe/Berlin]'
+                       ],
                ];
        }
 
@@ -156,4 +160,81 @@ class DateTimeFormatTest extends MockedTestCase
 
                $this->assertEquals(259200, $now - $date);
        }
+
+       public function dataConvert() {
+               return [
+                       'unix timestamp' => [
+                               'expected' => '2025-03-12 16:18:27',
+                               's' => '1741796307',
+                       ],
+                       'ATOM' => [
+                               'expected' => '2022-06-02 15:58:35',
+                               's' => '2022-06-02T16:58:35+01:00',
+                       ],
+                       'COOKIE' => [
+                               'expected' => '2022-06-02 14:58:35',
+                               's' => 'Thursday, 02-Jun-2022 16:58:35 Africa/Cairo',
+                       ],
+                       'ISO 8601/RFC 3339' => [
+                               'expected' => '2022-06-02 13:58:35',
+                               's' => '2022-06-02T16:58:35+0300',
+                       ],
+                       'RFC 822/RFC 1036' => [
+                               'expected' => '2022-06-02 12:58:35',
+                               's' => 'Thu, 02 Jun 22 16:58:35 +0400',
+                       ],
+                       'RFC 850' => [
+                               'expected' => '2022-06-02 11:58:35',
+                               's' => 'Thursday, 02-Jun-22 16:58:35 Indian/Kerguelen',
+                       ],
+                       'RFC 1123/RFC 2822/RSS' => [
+                               'expected' => '2022-06-02 10:58:35',
+                               's' => 'Thu, 02 Jun 2022 16:58:35 +0600',
+                       ],
+                       'RFC 3339/W3C' => [
+                               'expected' => '2025-03-07 01:54:14',
+                               's' => '2025-03-07T08:54:14+07:00',
+                       ],
+                       'RFC 3339 extended' => [
+                               'expected' => '2025-03-07 00:54:14',
+                               's' => '2025-03-07T08:54:14.341+08:00',
+                       ],
+                       'RFC 7231' => [
+                               'expected' => '2022-06-02 07:58:35',
+                               's' => 'Thu, 02 Jun 2022 16:58:35 Asia/Tokyo',
+                       ],
+               ];
+       }
+
+       /**
+        * @dataProvider dataConvert
+        */
+       public function testConvert($expected, string $s = 'now', string $tz_to = 'UTC', string $tz_from = 'UTC', string $format = DateTimeFormat::MYSQL)
+       {
+               $this->assertSame($expected, DateTimeFormat::convert($s, $tz_to, $tz_from, $format));
+       }
+
+       public function dataConvertNow()
+       {
+               return [
+                       'now missing' => [
+                       ],
+                       'now empty' => [
+                               's' => '',
+                       ],
+                       'now now' => [
+                               's' => 'now',
+                       ],
+               ];
+       }
+
+       /**
+        * @dataProvider dataConvertNow
+        */
+       public function testConvertNow(string $s = 'now', string $tz_to = 'UTC', string $tz_from = 'UTC', string $format = DateTimeFormat::MYSQL)
+       {
+               $this->assertSame(date(DateTimeFormat::MYSQL), DateTimeFormat::convert($s, $tz_to, $tz_from, $format));
+       }
+
+
 }