mirror of
https://git.friendi.ca/friendica/friendica.git
synced 2025-06-07 22:58:07 +02:00
Add support for more datetime formats
- Unix Timestamp - RFC 3339 extended + timezone name in square brackets - Address https://github.com/friendica/friendica/issues/14647#issuecomment-2719430131
This commit is contained in:
parent
0aa350f1c8
commit
6f159e69be
2 changed files with 85 additions and 2 deletions
|
@ -117,7 +117,7 @@ class DateTimeFormat
|
||||||
$tz_to = 'UTC';
|
$tz_to = 'UTC';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (($s === '') || (!is_string($s))) {
|
if ($s === '') {
|
||||||
$s = 'now';
|
$s = 'now';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,7 +135,8 @@ class DateTimeFormat
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$d = new DateTime($s, $from_obj);
|
$d = DateTime::createFromFormat('U', $s, $from_obj)
|
||||||
|
?: new DateTime($s, $from_obj);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
try {
|
try {
|
||||||
$d = new DateTime(self::fix($s), $from_obj);
|
$d = new DateTime(self::fix($s), $from_obj);
|
||||||
|
@ -176,6 +177,7 @@ class DateTimeFormat
|
||||||
$pregPatterns = [
|
$pregPatterns = [
|
||||||
['#(\w+), (\d+ \w+ \d+) (\d+:\d+:\d+) (.+)#', '$2 $3 $4'],
|
['#(\w+), (\d+ \w+ \d+) (\d+:\d+:\d+) (.+)#', '$2 $3 $4'],
|
||||||
['#(\d+:\d+) (\w+), (\w+) (\d+), (\d+)#', '$1 $2 $3 $4 $5'],
|
['#(\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) {
|
foreach ($pregPatterns as $pattern) {
|
||||||
|
|
|
@ -125,6 +125,10 @@ class DateTimeFormatTest extends MockedTestCase
|
||||||
'expectedDate' => '2023-04-02T17:22:42+05:30',
|
'expectedDate' => '2023-04-02T17:22:42+05:30',
|
||||||
'dateString' => '2023-04-02\T17: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);
|
$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));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue